matter-js icon indicating copy to clipboard operation
matter-js copied to clipboard

Changing Time Scale Affects Physics

Open Inve1951 opened this issue 8 years ago • 10 comments

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:

  • a stack of resting rects will bounce
  • all bodies will lose all momentum and drop straight down

I found these discussions which seem related to me:

  • #148
  • #280
  • #240

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 avatar Oct 12 '16 16:10 Inve1951

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

Inve1951 avatar Oct 14 '16 20:10 Inve1951

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

Technostalgic avatar Dec 15 '16 02:12 Technostalgic

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.

hexus avatar Mar 24 '18 14:03 hexus

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.

jared83 avatar Apr 05 '18 12:04 jared83

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?

Nathan-Devery avatar Dec 22 '18 03:12 Nathan-Devery

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.

Inve1951 avatar Dec 23 '18 06:12 Inve1951

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);

Nathan-Devery avatar Dec 23 '18 06:12 Nathan-Devery

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

Inve1951 avatar Dec 26 '18 14:12 Inve1951

Any updates on this?

tzvc avatar May 11 '19 07:05 tzvc

If you are using MatterJS browser and need deterministic:

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

SUCHiDEV avatar Jan 24 '24 12:01 SUCHiDEV