Fast determination of whether objects are onscreen in 2D
- by Ben Ezard
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?