• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Render enteties visible on the screen

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1P0jahn  Members

Posted 25 March 2014 - 07:08 AM

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.

### #2Waterlimon  Members

Posted 25 March 2014 - 07:28 AM

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

### #3TheComet  Members

Posted 25 March 2014 - 10:55 AM

Look into spatial indexing using quad trees.

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.

Edited by TheComet, 25 March 2014 - 10:56 AM.

"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

### #4Buckeye  GDNet+

Posted 25 March 2014 - 12:35 PM

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.

### #5SeanMiddleditch  Members

Posted 25 March 2014 - 01:59 PM

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.

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).

Game Developer, C++ Geek, Dragon Slayer - http://seanmiddleditch.com

C++ SG14 "Games & Low Latency" - Co-chair - public forums

Wargaming Seattle - Lead Server Engineer - We're hiring!

### #6ferrous  Members

Posted 25 March 2014 - 03:37 PM

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.

Edited by ferrous, 25 March 2014 - 03:39 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.