Page 1 of 1

Is it possible to replay a box2d world?

Posted: Mon Dec 21, 2020 11:08 pm
by dezoitodemaio
I'm developing a game and i want to store the keys pressed by the player to use later on some replay system and validation.

Is passing a fixed dt to world:update enough?

Re: Is it possible to replay a box2d world?

Posted: Tue Dec 22, 2020 1:42 am
by Xii
According to the Box2D official website, the engine is deterministic on the same system only, not across systems.

What does this mean? It means you can replay a world on the same computer as it was originally stored. However, any change to that computer (replacing the CPU, installing a different OS, etc.) may desynchronize the replay.

Why? Because it uses floating point numbers, which may differ slightly on different systems and configurations.

In general, on modern systems you cannot rely on input replays anymore (because floating point is so prevalent).
You have to store the positions of all objects every frame.

Re: Is it possible to replay a box2d world?

Posted: Tue Dec 22, 2020 1:53 am
by dezoitodemaio
For the same input, and same binary, Box2D will reproduce any simulation
That means i will get the same result as long as i run the game and the replay on the same platform, right?

The docs says about the floating point problem but only when you mix platforms, like windows and unix

Re: Is it possible to replay a box2d world?

Posted: Tue Dec 22, 2020 10:56 am
by pgimeno
dezoitodemaio wrote: Tue Dec 22, 2020 1:53 am That means i will get the same result as long as i run the game and the replay on the same platform, right?
With the same Löve executable in the same computer, yes. If the dt's you pass to Box2D are always the same, you should be able to replay the same actions. I've done that successfully.

dezoitodemaio wrote: Tue Dec 22, 2020 1:53 am The docs says about the floating point problem but only when you mix platforms, like windows and unix
"Platforms" is a bit ambiguous here. It's not about the OS, but about the combination of CPU, compiler and optimization flags. It's likely that when using the same compiler and optimization flags, the replay is portable between OSes. But it's not guaranteed that e.g. Box2D for Linux ARM and Box2D for Linux x86 behave the same, because Box2D does not necessarily use only operations that are portable according to IEC559. Also, some optimization flags break IEC559 compatibility.