Box2D 是完全确定的吗?

Is Box2D perfectly deterministic?

我正在使用 LibGDX 和 Box2D 编写 Android 游戏。我正计划为其添加基于回合制的多人游戏功能。

现在,如果我在两个客户端上以相同的速度和相同的时间步长进入 Box2D 世界,并且我在两个客户端上使用完全相同的初始参数开始模拟,当模拟结束时,最终状态将是两个模拟完全一样?换句话说,Box2D 模拟是完全确定的吗?

如果不是,则意味着每次模拟结束时,作为主机的一个客户端将不得不告诉另一个客户端丢弃其最终模拟结果并使用其代替。

环顾四周,答案是"No",即使使用相同的时间步长!这个答案的原因与浮点数学在许多编译器和处理器中的实现方式有关。每个周期的微小差异加起来会导致显着不同的模拟。

官方常见问题引用

官方常见问题解答中有一段话证实了您的推断 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

或者换句话说:Fixed-size floating point types

为什么wiki被删了,我也不知道。人类。不过,我很高兴他将项目名称小写了。

我设法使 Box2D 成为实验的确定性,但它并不漂亮。 b2Body::GetTransform()/SetTransform() 的工作方式不允许读取转换然后将其设置回完全相同的值。我还必须删除 re-create 每帧 body 的联系人列表。可以更干净、更有效地修复这些问题,但它会增加足够的开销,很难合并更改。