cam11: how to tell what is in 'frame'

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
togFox
Party member
Posts: 774
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

cam11: how to tell what is in 'frame'

Post by togFox »

I haven't thought too hard about this - but big fan of cam11 but here's my next challenge.

My whole world (all tiles) can be seen when zoom = 1 (normal zoom) and half the world can be seen when zoomed in at zoom = 2 (approximately). I need to play audio (sound effects) only for the things in the frame at that time. I need to know what is in frame, or on screen, and what is not, and play different audio accordingly.

My brain started thinking about screen width / zoom factor and then measure each entity from the current screen focal point etc etc but then thought the author of cam11 might happen upon this and give me a clue. :) Perhaps some funky world co-ordinate or screen co-ordinate check or ... idk - it's late. lolz. I'll sleep now!
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: cam11: how to tell what is in 'frame'

Post by GVovkiv »

togFox wrote: Mon May 23, 2022 11:10 am I haven't thought too hard about this - but big fan of cam11 but here's my next challenge.

My whole world (all tiles) can be seen when zoom = 1 (normal zoom) and half the world can be seen when zoomed in at zoom = 2 (approximately). I need to play audio (sound effects) only for the things in the frame at that time. I need to know what is in frame, or on screen, and what is not, and play different audio accordingly.

My brain started thinking about screen width / zoom factor and then measure each entity from the current screen focal point etc etc but then thought the author of cam11 might happen upon this and give me a clue. :) Perhaps some funky world co-ordinate or screen co-ordinate check or ... idk - it's late. lolz. I'll sleep now!
https://github.com/kikito/gamera/blob/master/gamera.lua line 39, gamera have this kind function. It will return visible area, accounting for scale and zoom.
Cam11 should have something similar, but if it didn't, then perhaps you can port it to cam11?
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: cam11: how to tell what is in 'frame'

Post by pgimeno »

Convert the corners of the viewport from screen to world coordinates, then you can check the objects in your world against that rectangle.

For example, if your viewport is the whole screen, then you would convert (0,0) to world and obtain (w1x,w1y), then (love.graphics.getWidth(), love.graphics.getHeight()) to obtain (w4x, w4y). If you also use rotation, convert (love.graphics.getWidth(), 0) to world and obtain (w2x, w2y), then (0, love.graphics.getHeight()) and obtain (w3x, w3y).

If you don't use rotation, and your objects are axis-aligned bounding boxes, you only need a simple AABB intersection check of your objects against (w1x, w1y, w4x, w4y). In other case, it's a polygon vs polygon intersection check; let me know if that's the case and I'll try to lend a hand.

If your objects are points instead of rectangles, things are simpler. But without these details I can't give better advice:

- Are you using rotation?
- Are your objects represented by points? By axis-aligned boxes? By convex polygons? By non-convex polygons?
User avatar
togFox
Party member
Posts: 774
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: cam11: how to tell what is in 'frame'

Post by togFox »

No rotation.
Points.

The AABB thing is super easy with x/y points so I'll look into understanding how the viewport can be mapped to world co-ordinates.

Example:
My monitor is 640x480 so my world is 640x480 by design (world = monitor size).

The camera is centred in the middle and zoom = 2 so I can now see only 320x240 around the middle of the world.

How to compute the point at 100,100 is outside the viewport?
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: cam11: how to tell what is in 'frame'

Post by pgimeno »

Okay, easy peasy.

Imagine how the viewport looks like in the map. By converting (0, 0) from screen to world coordinates, and (640, 480) from screen to world coordinates, you have the corners of the viewport rectangle as projected in the world. Now just check which points are within that rectangle. For your example, check if (100, 100) is within that rectangle.
User avatar
togFox
Party member
Posts: 774
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: cam11: how to tell what is in 'frame'

Post by togFox »

I see. Simple enough!

I shall try. Thanks!
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 35 guests