Fast determination of whether objects are onscreen in 2D

Posted by Ben Ezard on Game Development See other posts from Game Development or by Ben Ezard
Published on 2014-08-22T10:52:19Z Indexed on 2014/08/22 16:37 UTC
Read the original article Hit count: 243

Filed under:
|
|
|
|

So currently, I have this in each object's renderer's update method:

        float a = transform.position.x * Main.scale;
        float b = transform.position.y * Main.scale;
        float c = Camera.main.transform.position.x * Main.scale;
        float d = Camera.main.transform.position.y * Main.scale;
        onscreen = a + width - c > 0 &&
                a - c < GameView.width &&
                b + height - d > 0 &&
                b - d < GameView.height;

transform.position is a 2D vector containing the game engine's definition of where the object is - this is then multiplied by Main.scale to translate that coordinate into actual screen space Similarly, Camera.main.transform.position is the in-engine representation of where the main camera is, and this is also multiplied by Main.scale

The problem is, as my game is tile-based, thousands of these updates get called every frame, just to determine whether or not each object should be drawn - how can I improve this please?

© Game Development or respective owner

Related posts about 2d

Related posts about engine