liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.79k stars 1.96k forks source link

Changing Time Scale Affects Physics #303

Open Inve1951 opened 8 years ago

Inve1951 commented 8 years ago

Changing engine.timing.timeScale significantly affects physics. (even for resting bodies) It's well observable on the demo page when moving the timeScale slider.

I want to animate pausing the scene by gradually moving timeScale from 1 to 0 and from 0 to 1 on unpause but the change this causes to the physics is unbearable. So I crestfallenly went to just toggle timeScale between 1 and 0.

After fixing the weird gravity behavior I experienced I came to notice that even with toggling timeScale as described there are errors in simulation.

After pausing+unpausing the scene that way a quick look shows me the following:

I found these discussions which seem related to me:

As I investigated I got the impression that values from the previous update call get used which would explain why incorrectnesses build up as you time-scale the simulation.

This pretty much renders varying time-scaling/slow-mo useless...

Inve1951 commented 8 years ago

It seems like the same effects occur when the frame rate is low and the engine tries to correct the lag.

Running my simulation on the phone bodies sink into each other towards gravity and stretch back to where they should be after a few seconds just to start sinking again. It looks like the engine is breathing. o_O

Technostalgic commented 7 years ago

I have noticed this issue too, it seems that the only timestep that calculates correct physics is 1/60th of a second.

hexus commented 6 years ago

I hope this gets looked into, it'd be really great to use this time scaling feature reliably when applying the same forces at the same time step.

jared83 commented 6 years ago

Confirmed. At this point the "slo-mo" effect doesn't work at all when forces are applied to time-scaled bodies. In my case it is the Body.setVelocity() that makes bodies misbehave Would love to see this fixed.

Nathan-Devery commented 5 years ago

I'm having the same issue. My evolutionary algorithm needs a way to speed up the simulations when evaluating the fitness of individuals in the population - the physics changes, when increasing the timescale, stops this.

Is there a way around this? like making the simulation near instant?

Inve1951 commented 5 years ago

To my knowledge, no. This was a blocker for me and I won't do another project with matter-js until this was fixed. I even could have lived without time-scaling, but low FPS have the same effect.

Nathan-Devery commented 5 years ago

I was able to get it working by just increasing the speed of the game loop:

setInterval(function() { Engine.update(engine, 1000 / 60); }, 1000 / 60);

Inve1951 commented 5 years ago

Beware that the issue will likely resurface for you when your simulation becomes too extensive to run the speed-up in realtime, @Nathan-Devery.

tzvc commented 5 years ago

Any updates on this?

SUCHiDEV commented 9 months ago

If you are using MatterJS browser and need deterministic:

https://stackoverflow.com/a/77872867/4421162