I have two cannon worlds using three.js meshes, where all factors like offsets, rolling forces, gravity, etc., are identical. I drop two bodies into each world with equal angular velocities and identical properties. Despite this, the collisions in the two worlds yield different results. Why is this happening, and how can I ensure that the collisions in both worlds produce exactly the same results?
It depends on the usage context, is this something socket based? Eg… you need one physics environment for multiple socket clients to evaluate the same visual / physical result?
Physics engines running with variable timesteps aren’t deterministic.
Both because the math resolved differently with different deltaT…
And if the engine loops internally by fixed timestep, you don’t have a guarantee that your user input events are occurring on the same frame…
and in some cases also because of randomness injected in the interpenetration resolution when it’s unclear what the right way is to resolve the interpenetration, some engines do a dice roll.
But if you call your physics with a fixed timestep, with inputs on the exact same frames, I think with Rapier, Ammo, and Havok, and maybe Cannon too, you can get deterministic-ish results.
I’ve heard anecdotally that Rapier physics has “better” determinism.
To truly run with a fixed timestep, you basically have to journal all the user input events that will happen in the sim, and play them back on the exact same frame w same values etc.
And manage this all within your animation loop.
Making a deterministic networked simulation is Really hard, and Desync is almost guaranteed to happen, so really… what you end up trying to do is retconn as much as possible, and just try to constantly adjust/sync things up when you can… and use “latency hiding”, to allow events to happen at similar times on multiple clients.
Also… determinism is often overrated. If you run 2 copies of many different games side by side networked, you’ll probably notice that things aren’t as deterministic as you might think.
Like… just broadcasting all the player inputs, and maybe 10x a second also broadcasting the players position can get you something that works ok. No it’s not secure and yes its hackable, and its also the dirty secret of a lot of networked games.
@Lawrence3DPK , yes that is what I am trying to achieve.