How to optimize a box2d simulation in action game?
- by nathan
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:
Not all the world is visible on screen, i use a scrolling system
Enemies are constantly moving toward the player so there is alaways to force applied to their body
Enemies need to collide between each others
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?