Is Box2D perfectly deterministic?

I am writing an Android game using LibGDX and Box2D. I am planning to add multiplayer feature to it in turn.

Now if on both clients I enter the Box2D world at the same speed with the same time steps, and I run the simulation on both clients with the same initial parameters, when the simulations are finished, will the final state of both simulations be exactly the same? In other words, is Box2D modeling perfectly deterministic?

If this is not the case, it means that every time the simulation ends, one client acting as the host will have to tell the other to discard the final simulation results and use it instead.

+4


source to share


3 answers


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



+3


source


Official frequency quote

There is now a quote in the official FAQ that confirms that you pulled out https://github.com/erincatto/Box2D/wiki/FAQ/933830ba42bce329a6697212050da00c383f1e79 :



#Determinism ## Is Box2D deterministic? For the same input and the same binary, Box2D will play any simulation. Box2D does not use random numbers and does not base any calculations on random events (like timers, etc.).

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

+1


source


I managed to make Box2D deterministic for an experiment, but it wasn't pretty. The way b2Body :: GetTransform () / SetTransform () works does not allow you to read the transformation and then set it back to exactly the same values. I also had to delete and re-create the contact list for each body of each frame. One could fix this neatly and more efficiently, but that would add enough overhead, it would be difficult to merge the change.

0


source







All Articles