Jump to content

  • Log In with Google      Sign In   
  • Create Account

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.

  • You cannot reply to this topic
5 replies to this topic

#1 P0jahn   Members   -  Reputation: 272

Like
0Likes
Like

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. 



Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2637

Like
0Likes
Like

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


#3 TheComet   Crossbones+   -  Reputation: 1642

Like
0Likes
Like

Posted 25 March 2014 - 10:55 AM

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.


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

YOUR_OPINION >/dev/null

#4 Buckeye   Crossbones+   -  Reputation: 6290

Like
1Likes
Like

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.


#5 SeanMiddleditch   Members   -  Reputation: 7170

Like
0Likes
Like

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.


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

#6 ferrous   Members   -  Reputation: 2146

Like
0Likes
Like

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.



PARTNERS