Performance/Stuttering and expense of adding bodies to physics simulation

We are mid way through a project. The game runs mostly at about 30fps. The problem is that we get intermittent stuttering which is very undesirable and affects usability of our game. 

I was wondering how expensive it was to add an object to the box2d physics simulation. If say, we were to add a body  every 2-3 seconds, would this be enough to create stuttering and momentarily drop a 30 fps to about 14 fps?

It will be quite a task to implement proper caching in our game, due to our dependency on a thirdparty TileEngine, so I wanted to get an idea of the expected  gains before we look more deeply at this aspect of performance.

/Steven

Are you able to reuse objects instead of creating/destroying?

Creating a pool of say 10 enemies initially and setting the dead ones to offscreen then resetting/changing their properties and making them alive again is a much faster approach

Appreciated, I wondered how much faster it is ?

The reason I asked is that to eliminate above will be quite a task to implement in our game given our dependencies.

Best,

Steven

Another query i have is:

is the cost of adding bodies to the box2d physics simulation dependent on  amount of existing bodies within simulation and does the  isBodyActive  property affect this?

Best,

Steven

It’s a  lot  faster. This is why:

Creating say 10 enemies at startup and reusing them has no additional performance cost. You are not destroying them/removing them which means the garbage collector doesn’t have to keep kicking in to free memory from deleted objects (this can cause stuttering).

Also creating display objects during gameplay is (in most cases) to be avoided as that can cause stutters also. So your issue isn’t really the cost of adding bodies, it’s the cost of adding/removing display objects during time critical events (ie gameplay)

Note: When I said create 10 enemies, this number may be too high or too low depending on your situation. If your game has say only 4 enemies on screen at any given time, you may get away with creating 6 enemies to use as your pool and re-use those. Experiment with the number and the lower it is, the better.

Hope this helps :slight_smile:

Are you able to reuse objects instead of creating/destroying?

Creating a pool of say 10 enemies initially and setting the dead ones to offscreen then resetting/changing their properties and making them alive again is a much faster approach

Appreciated, I wondered how much faster it is ?

The reason I asked is that to eliminate above will be quite a task to implement in our game given our dependencies.

Best,

Steven

Another query i have is:

is the cost of adding bodies to the box2d physics simulation dependent on  amount of existing bodies within simulation and does the  isBodyActive  property affect this?

Best,

Steven

It’s a  lot  faster. This is why:

Creating say 10 enemies at startup and reusing them has no additional performance cost. You are not destroying them/removing them which means the garbage collector doesn’t have to keep kicking in to free memory from deleted objects (this can cause stuttering).

Also creating display objects during gameplay is (in most cases) to be avoided as that can cause stutters also. So your issue isn’t really the cost of adding bodies, it’s the cost of adding/removing display objects during time critical events (ie gameplay)

Note: When I said create 10 enemies, this number may be too high or too low depending on your situation. If your game has say only 4 enemies on screen at any given time, you may get away with creating 6 enemies to use as your pool and re-use those. Experiment with the number and the lower it is, the better.

Hope this helps :slight_smile: