Is Box2D perfectly deterministic?

2019-04-10 09:52发布

问题:

I'm writing an Android game using LibGDX and Box2D. I'm planning on adding a turn-based multiplayer feature to it.

Now, if on both clients I step the Box2D world at the same rate with the same time steps and I start a simulation on both clients with the exact same initial parameters, when the simulations are over, will the final state of both simulations be exactly the same? In other words, is a Box2D simulation perfectly deterministic?

If it's not, then that means every time a simulation is over, one client acting as a host will have to tell the other to throw away its final simulation's results and use its instead.

回答1:

After looking around, the answer is "No", even if the same time steps are used! The reason for this answer has to do with how floating point math is implemented in many compilers and processors. Small discrepancies on each cycle add up resulting in significantly different simulations.



回答2:

Official FAQ quote

The official FAQ now has a quote that confirms what you deduced https://github.com/erincatto/Box2D/wiki/FAQ/933830ba42bce329a6697212050da00c383f1e79 :

#Determinism ##Is Box2D deterministic? For the same input, and same binary, Box2D will reproduce any simulation. Box2D does not use any random numbers nor base any computation on random events (such as timers, etc).

However, people often want more stringent determinism. People often want to know if Box2D can produce identical results on different binaries and on different platforms. The answer is no. The reason for this answer has to do with how floating point math is implemented in many compilers and processors. I recommend reading this article if you are curious: http://www.yosefk.com/blog/consistency-how-to-defeat-the-purpose-of-ieee-floating-point.html