schteppe / p2.js

JavaScript 2D physics library
Other
2.63k stars 329 forks source link

Recommended way to tweak simulation speed? #285

Closed ironchestgames closed 7 years ago

ironchestgames commented 7 years ago

Hello,

I have a platformer using p2, but the game is just a tad bit too slow. If I just want everything to run faster (or slower) what are the ways to go about it?

Kind Regards,

schteppe commented 7 years ago

Hi. One way is to speed things up is to increase gravity (and adjust other velocities/forces accordingly). Note that you probably have to decrease the time step size too, to minimize body overlaps.

Another approach is to literally speed up time - if you instead of ’world.step(dt)’ run ’world.step(2*dt)’ the simulation will run 2x faster. However, the performance will be 2x worse.

Pro tip. Speeding up the physics is generally bad for performance. There is a reason why many physics-heavy games have moon gravity.

ironchestgames commented 7 years ago

Thanks for the reply!

When you say it is bad for performance, is that related to having CCD enabled for certain bodies? or is there a performance hit even without CCD?

schteppe commented 7 years ago

There would be a performance hit even without CCD.

CCD makes each step heavier. If you increase game speed you probably need more steps per second (aka smaller time step).

It's all really about time step size. If you can make the game faster without using a smaller time step, performance will be the same. But it's usually a tricky job.

ironchestgames commented 7 years ago

Ah, ok, so it is basically how much overlap I'm comfortable with vs performance?

schteppe commented 7 years ago

Yes!

ironchestgames commented 7 years ago

okelidokeli! Thanks for taking the time!