schteppe / p2.js

JavaScript 2D physics library
Other
2.64k stars 330 forks source link

A drop from 60 to 45 FPS changes physics interaction at small scales #243

Closed AndrewRayCode closed 7 years ago

AndrewRayCode commented 8 years ago

Here's a gif demonstrating my problem:

fps-throttle

You can see that at 60fps, the player can push the box with no resistance. But at 45fps, the player can barely push the box. The radius of the player is about 0.1 world units, so pretty small, but not tiny.

I set the player velocity directly when keys are pressed:

if( isRightKeyPressed ) {
    playerVelocity[ 0 ] = playerRadius * someFactor;
}

I assumed because I set the velocity directly, it would be FPS-volatile proof. However that doesn't seem to be the case.

One workaround to this is to increase (via hand tweaking) the player mass at smaller scales enough to counteract this, but I'm wondering if there's a generic solution, or if you're aware of what might be underlying this problem.

schteppe commented 8 years ago

Ok so if I got this correctly, it is the rendering FPS that drops. Correct me if I'm wrong.

I'd this is correct then your game isn't frame rate independent. A couple of things need to be done to make this happen:

All if these things are shown in the sample code in the readme: https://github.com/schteppe/p2.js#sample-code

schteppe commented 8 years ago

The reason why the applied velocity is less effective is then because its only applied 45 out of 60 possible times. At high frequency rate you will get 60 out of 60, which adds more velocity over time.

danneu commented 8 years ago

One thing that I didn't register til I read the source was that a postStep callback isn't just run after world.step() finishes, but also after every internal substep that step() might call to catch up with time which is why postStep is important.

Once I realized that, some of my bugs became immediately obvious.