schteppe / p2.js

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

Calling World.step with timeSinceLastCalled === 0 produces undesired results #334

Open Grimeh opened 6 years ago

Grimeh commented 6 years ago

Hi, I've run into an issue when trying to pause the game (by setting the global time scale to 0) will not pause the physics simulation as well (as desired) but instead make it revert back to the regular fixed time steps.

This is because World.step uses 0 as the timeSinceLastCalled default value, and therefore the value that indicates that the caller wants fixed time steps:

if(timeSinceLastCalled === 0){ // Fixed, simple stepping

I'm currently working around this by calling World.step like:

this.world.step(1 / 60, Math.max(dt, Number.EPSILON), this.maxSubSteps);

Preferably I would like to be able to call World.step with timeSinceLastCalled === 0, I think this could be accomplished by replacing

timeSinceLastCalled = timeSinceLastCalled || 0;

if(timeSinceLastCalled === 0){ // Fixed, simple stepping

with

if(timeSinceLastCalled === undefined){ // Fixed, simple stepping

Does that seem reasonable?

schteppe commented 5 years ago

Yea, it sounds like good idea. I guess I didn't think of this case when I first wrote the code...

Grimeh commented 5 years ago

I've submitted a PR (#337) with the mentioned changes.