p2.js icon indicating copy to clipboard operation
p2.js copied to clipboard

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

Open Grimeh opened this issue 5 years ago • 2 comments

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?

Grimeh avatar Sep 27 '18 04:09 Grimeh

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

schteppe avatar Oct 07 '18 14:10 schteppe

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

Grimeh avatar Oct 17 '18 03:10 Grimeh