Is Box2D perfectly deterministic?
Asked Answered
G

3

5

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.

Gondola answered 5/6, 2015 at 19:31 Comment(2)
possible duplicate of Does Box2D Physics rely on the framerate?Electrotype
@Electrotype The question on that post has the same concern but from the perspective of different frame rates. The answer to that is "No". My question already establishes that a fixed frame rate will be used. Will the simulations be deterministic then?Gondola
G
3

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.

Gondola answered 5/6, 2015 at 20:27 Comment(0)
H
6

Official FAQ quote

The official FAQ had a quote that confirms what you deduced http://web.archive.org/web/20160131050402/https://github.com/erincatto/Box2D/wiki/FAQ#is-box2d-deterministic:

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

Or in other words: Fixed-size floating point types

Why the wiki was deleted, I do not know. Humans. I'm glad he lowercased the project name though.

Hospodar answered 23/12, 2017 at 12:12 Comment(0)
G
3

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.

Gondola answered 5/6, 2015 at 20:27 Comment(0)
F
0

I managed to make Box2D deterministic for an experiment but it was not pretty. The way b2Body::GetTransform()/SetTransform() works does not allow reading the transform and then setting it back to the exact same values. I also had to delete and re-create the contact list for each body every frame. It would be possible to fix these cleanly and more efficiently but it would add enough overhead it would be hard to get the change merged.

Frodine answered 18/6, 2019 at 1:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.