Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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?

share|improve this answer
    
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. –  Zaphyk Apr 20 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. –  Lasse Apr 20 at 2:19
    
quadtree? I divide the Blocks by 16x16 zones, Like Minecraft chunks –  Zaphyk Apr 20 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. –  Lasse 2 days ago
add comment

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.

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.