# How do I efficiently determine what objects are visible to a camera?

I want to call the rendering methods of only the game objects that are visible. How can I efficiently determine which objects or tiles are within the camera's rendered region?

• You haven't mentioned: Is your game 2D or 3D?
– Anko
Commented Oct 17, 2014 at 13:51
• closely related (almost duplicate) gamedev.stackexchange.com/questions/16306/… Commented Mar 16, 2015 at 16:36
• Many games will still run logic for entities if they are off camera, but will only do so for a limited amount of time (a few seconds). This is useful for the case when your camera moves back and forth within a relatively short amount of time - it will seem like things are still happening off screen even though they aren't. Unreal engine does this with skeletal animated models for instance! Commented Jun 14, 2015 at 21:11

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:

int cameraX = 0;
int cameraY = 0;


You also need to have the camera size, which is usually the same as your viewport size:

int cameraWidth = viewportWidth;
int cameraHeight = viewPortHeight;


Then you can find out if a quad is inside the camera by using simple AABB collision check:

public boolean isInsideCamera(int x, int y, int width, int height) {
if(x < cameraX + cameraWidth && x > cameraX) {
if(y < cameraY + cameraHeight && y > cameraY) {
return true;
}
}
return false;
}


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?

• But Calling this every frame checking if its inside the camera range it is not as expensive at drawing the image every Frame, I ask because i have no idea about computational costs. Commented Apr 20, 2014 at 1:58
• The computational costs depends greatly on the amount of items. Usually, it is cheaper to calculate what belongs to screen than just draw everything. And what belongs to screen can be computed for thousands of objects easily if you use quadtree or some other grid structure to store the entities in your world. Commented Apr 20, 2014 at 2:19
• quadtree? I divide the Blocks by 16x16 zones, Like Minecraft chunks Commented Apr 20, 2014 at 3:58
• Minecraft uses quadtree (most likely along with octree for the chunk blocks), yes, but the same structure fits well with 2d games too. You could also use bsp tree or simple grid to store entities. Whatever works for you. Note that this does not work well for the map, only for the entities if there are lots of them. The map works fastest with simple 2d array. Commented Apr 20, 2014 at 11:17
• Don't query if each tile position is inside the viewport. Query the viewport to find its tile extents, then just iterate over those specific tiles. You don't have to do expensive AABB tests for partial tiles, either; just take the floor or ceiling of the minimum and maximum extents. Commented Oct 12, 2015 at 22:45

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.

Tile[,] tiles = new Tile[x,y];


We can now access the tiles in this way:

for( int x = camera.x / tileWidth; x < ( camera.x + camera.width ) / tileWidth; x++ )
{
for( int y = cameray / tileHight; y < ( camera.y + camera.height ) / tileHeight; y++ )
{
// Access the tiles that are in the screen here.
}
}


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.