How to optimize a box2d simulation in action game?

Posted by nathan on Game Development See other posts from Game Development or by nathan
Published on 2014-05-26T20:46:56Z Indexed on 2014/05/26 22:05 UTC
Read the original article Hit count: 215

Filed under:
|

I'm working on an action game and i use box2d for physics. The game use a tiled map.

I have different types of body:

  • Static ones used for tiles
  • Dynamic ones for player and enemies

Actually i tested my game with ~150 bodies and i have a 60fps constantly on my computer but not on my mobile (android). The FPS drop as the number of body increase.

After having profiled the android application, i saw that the World.step took around 8ms in CPU time to execute.

Here are few things to note:

  1. Not all the world is visible on screen, i use a scrolling system
  2. Enemies are constantly moving toward the player so there is alaways to force applied to their body
  3. Enemies need to collide between each others
  4. Enemies collide with tiles

I also now that i can active/desactive or sleep/awake bodies.

Considering the fact that only a part of the enemies are possibly displayed on screen, is there any optimizations i can do to reduce the execution time of box2d simulation?

I found a guy trying an optimization based on distance of enemies from the player (link). But i seems like he just desactives far bodies (in my case, i could desactive bodies that are not visible). But my enemies need to move even when they are not visible on screen, and applying forces will not workd on inactive bodies. Should i play with sleeping bodies here?

Also, enemies are composed by two fixtures and are constantly colliding with each others and with tiles but i really never need to get notified about that. Is there anything i can do to optimize this kind of scenario?

Finally, am i wrong to try to run simulation at 60FPS on mobile and should i try to make it run at 30FPS?

© Game Development or respective owner

Related posts about box2d

Related posts about optimization