liabru / matter-js

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

FPS affects physics! #885

Closed car1ot closed 11 months ago

car1ot commented 4 years ago

Hello. I have simulation (game plinko) - balls falling down from exact coordinates into baskets. Repeating the same simulation over and over, results are different!
Also, when I slow down time the simulation results become very different.
I made this issue, beause I need same results in multiple simulations using same exact fall coordinates.

dclipca commented 4 years ago

Indeed, I experience the same effect.

car1ot commented 4 years ago

Indeed, I experience the same effect.

And what to do? I need the same results on server and client...

wmike1987 commented 4 years ago

There's a PR all about timing inconsistencies. Regardless, you should fix your timestep to achieve determinism.

https://github.com/liabru/matter-js/pull/777

car1ot commented 4 years ago

There's a PR all about timing inconsistencies. Regardless, you should fix your timestep to achieve determinism.

777

help pls, I quoted reply in #777

liabru commented 4 years ago

Pretty much as @wmike1987 says (thanks!)

DzzD commented 3 years ago

I experience the same problem (wich sound to me logical unless you use fixed time step you IMHO cannot get same results specially with float computation), I fixed it by using fixed time (and integer) step (see below), this solution may be added as an option of the Engine.update ?

timeStep = 5;
...
...
lastFrameTime = frameTime;
frameTime =  Date.now();
let frameDuration = frameTime - lastFrameTime;`
let repeat= Math.floor(frameDuration/timeStep); 
let  remainingTime = frameDuration%timeStep;
frameTime -= remainingTime;
for(var n=0;n<repeat;n++)
{
   Matter.Engine.update(engine, timeStep);
}
liabru commented 3 years ago

@DzzD good to know that approach is working for you, it's worth trying out branch #777 too if you get chance.

I've got similar prototypes of adaptive fixed timestep for Matter.Runner but none are yet production ready, it is something I'll prioritise though when I get back to #777.

liabru commented 11 months ago

Closing this thread to refer to updates in #702 on this topic, thanks again for the reports here.