Drawing only what is on the screen

Topics: Developer Forum, User Forum
Jul 10, 2013 at 3:54 AM
Hey everyone,

Just a quick question here.


I have been using farseer for a very long time, mainly hobbies and other playthings. I am not working on a serious title, and before I get to deep into coding, I like to plan my game out first.

So here is my question:
How or what are the techniques do you guys use to only draw physics objects that are in your view port/rectangle? I have a 2D Shooter that uses polygons for ground and terrain objects. I obviously do not wish to draw something that's happening off the screen (like other people shooting each other or boxes falling down). What are the tricks you guys use?
Cheers
Jul 11, 2013 at 8:48 AM
You don't necessarily need to not draw what is offscreen.

In my game there was never a vast amount of activity offscreen, but it would easily be a few hundred textures in normal play.

What I found was that my performance was already fine so there was no need to optimise by culling the draw calls I did not need. I found the cost with Draw calls lies more in how many SpriteBatch calls you make rather than the number of textures. I have 300 to 400 textures and maybe 15 or so SpriteBatches and draw performance is fine.

A friend of mine did some very deep analysis (on Win Phone) and found that in his use case, the routine he wrote to cull non visible textures actually made the game slower! He was dealing with a few 100 textures. His algorithm was a simple "is object in this box" check.

Of course if you're going to have many 1000s of textures, then an algorithm may well help. I think the algorithms here are called view graphs, scene graphs, something like that but I've never researched them.

Last point I'd make is that the hardware might already be doing the culling for you. I seem to remember the xbox360 did do texture culling itself, and the Win Phone 7 did not. I've released the same Farseer based game on 360, Win Phone and Android and never had to do any texture culling.
Coordinator
Jul 16, 2013 at 3:55 AM
The 'tricks' depend very much on the performance problem at hand. Do you use vast amounts of memory? Do you calculate the same thing again and again? are drawing calls too expensive? and so on.

The most important 'trick' I can give you, is to use the profiler. Then profile some more, and then profile - perhaps profile a little more. You get the idéa ;)

If you would like to know more about optimization, I wrote a lot of articles on the subject. You can find them on my blog.