Render enteties visible on the screen

Started by
4 comments, last by ferrous 10 years ago

I need a formula for my 2D game that calculates whether or not a game object/entity should be rendered.

This means, if an entity is not visible on the screen(the translated view), skip the rendering it.

Info:

- The entity stores its position in X and Y(which is the top-left corner of its image). It also stores its width and height.

- The size of the game window/screen is 800x600. The total size of the stage is stageWidth and stageHeight.

- The translation is set not to film areas outside the stage's boundaries.

Advertisement

1. Take your entitys coordinates and size and transform them into the coordinate space of the screen. If an entity is in the middle of the screen, it should be at the origin of this coordinate system (assuming you use the screen center as the origin of the screen coordinate system; you could use a corner too i guess)

If your camera/entity cannot rotate, this is simply:

localPos = globalPos - cameraPos

to get the camera/screen local coordinate of the entity. If you have zooming or such you need to scale the size of the entity, otherwise you dont need to touch it.

2. Use this screen local coordinate to make some trivial comparisons to see if the entity is within the screen. To make this simpler, you can calculate the lower right corner by adding the size of the entity to its top left corner which you have.

-Make sure top left corner is not below bottom of screen or right of the right screen edge

-Similarly make sure bottom right corner is not above top of screen or left of the left screen edge

o3o

Look into spatial indexing using quad trees.

http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves

They allow you to make super fast look-ups on renderable entities, allowing you to cull them in almost no time at all. Doing checks on every entity probably produces the same overhead as just rendering them anyway.

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty


Doing checks on every entity probably produces the same overhead as just rendering them anyway.

That's probably incorrect in most instances unless thousands of calcs per-entity is needed to cull them. Though culling calcs are likely done on the CPU, anything that is rendered is going to go through a culling calc somewhere. From the OP's description, maybe 6 or 8 point calculations per-entity in that case.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I need a formula for my 2D game that calculates whether or not a game object/entity should be rendered.
This means, if an entity is not visible on the screen(the translated view), skip the rendering it.


This is called "culling." Knowing the right terminology will help you in your efforts to Google an answer.

Note also that you often don't actually need to do this culling in many 2D games. Batch all your sprites together (don't change state or make a draw call for every sprite) and the GPU is generally capable of culling out the sprite geometry faster than you can on the CPU, unless you have a truly huge game stage area with a very, very large number of sprites (in which case apatial hierarchies as TheComent recommends is the way to go).

Sean Middleditch – Game Systems Engineer – Join my team!

The test for rect/rect overlap is pretty minimal, it might be worth it to implement it, and see if it actually nets you any speedup over just drawing everything.

You have the viewport's x,y, width, height (or you should compute it) and every object's x,y,width,height, and then just iterate and test.

for example if you are currently doing:

foreach gameObject go in gameObjects

go.draw()

just changing it to

foreach gameObject go in gameObjects

if(go.rect.overlaps(viewportRect))

go.draw()

should be minimally invasive and quick to implement.

This topic is closed to new replies.

Advertisement