Which is the best way to detect whenever an object or tile is inside the Camera Range so I can draw it? With "best" I mean more efficient, and quicker.
Thanks
You are talking about tiles, therefore i'm assuming you are talking about a 2d game. You need to have the camera position in the world like so:
You also need to have the camera size, which is usually the same as your viewport size:
Then you can find out if a quad is inside the camera by using simple AABB collision check:
NOTE: The above example only works if the quad is completly inside camera. For proper AABB intersection, try here: What is the fastest way to work out 2D bounding box intersection? |
|||||||||||||||||
|
If the tiles are stored in a linear way( sorted so that the top left tile is first and the bottom right is last, or some other reasonable order ), you can access the array / list by an index that describes tiles that only lay within the screen bounds. This will be a bit faster than doing a bounds check every tile.
We can now access the tiles in this way:
I believe this may be the fastest way we could do it. If the tiles aren't in an array, however, you can use the bounds checking method described by Lasse. If that's not fast enough, you could use a quadtree. This data structure allows use to quickly check where things are in the world. Read about it here: http://en.wikipedia.org/wiki/Quadtree It's a pretty simple structure to create and helps in far more than just object culling. |
|||
|