Is it possible to replay a box2d world?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
dezoitodemaio
Prole
Posts: 15
Joined: Mon Oct 26, 2020 2:02 pm

Is it possible to replay a box2d world?

Post 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?
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Is it possible to replay a box2d world?

Post 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.
dezoitodemaio
Prole
Posts: 15
Joined: Mon Oct 26, 2020 2:02 pm

Re: Is it possible to replay a box2d world?

Post 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
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Is it possible to replay a box2d world?

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 82 guests