A way to save the current state of the world.

Hello,
I was about to code a little editor for my game.
At first I thought of just coding it in Ruby and output the c++ source.
But if there´s a way to serialize a box2d world into a file and load it later that
would be better, also useful for save and load features.

So, has anybody done that before?
I was also thinking along these lines - it would be ideal, to me, if we could try to agree on some sort of a specification for this type of thing so that physics could be fairly portable both between the various ports of Box2D and between games/apps that use the engine. This would encourage sharing of interesting constructions, and would seem much simpler than cutting and pasting code snippets back and forth (which often cannot be combined because of name conflicts, etc.).

Would it make sense to adopt some sort of XML scheme, or maybe a binary dump of all the objects? I suppose there's a slight difference between setting the initial conditions (easy) and setting the detailed current state of the engine (hard), but a good way of recording the latter could handle the former as long as there was a way to get hooks to all the created objects in a reasonable way (if, for instance, you need to keep track of a particular body as the player).

Does Bullet have anything clever going on in this regard?
It should be possible to do this with boost.serialization, but that is not quite as light weight as box2d...
An easy way would be to have 'save' and 'load' static members for each class with a pointer to a stream. That way, we can save individual parts of our world, and it also makes it easier to alter the routines after they have been written. The save and load functions in b2World would first save all the data only relevent for the entire world, then would call save/load for each joint/body/contact/etc. This would also allow us to add our own data to the beginning without it affecting Box2D 😉 A binary or text choice would be nice too!
Diggsey - that's right along the lines of what I was thinking. Care to propose a specific format? I like plaintext, XML or not, because it's easier to parse (or for binary, zipped plaintext, just because it's so easy in Java) and editable by hand, but that's not a huge deal.

Erin, can you think of anything that wouldn't need to be stored and recreated in order to match the state of the world? I know that due to warm-starting it's not enough to merely save the positions and velocities of bodies, but is it necessary, for instance, to store away any of the broadphase stuff, or will that find its way back to the same state by itself once all the bodies are created?
A system to truly save the state of the world would be quite invasive and probably not necessary. This is a very application specific problem. We should also distinguish two issues:

1. Data to create a world (loading a level).
2. Data to save the game.

In my experience, usually the physics data for case 1 is built into the other game data. For example, I typically store the physics data with the object model data (with the animation bones, etc). If you try to store the physics data separately from your models, you will have to create some sort of mapping and also have to deal with the model and physics data getting out of sync.

So IMO, the best way to address 1 is to store the Box2D definitions with your game object models. You can use whatever format you like, as long as you can fill the defs.

My solution for 2 would be similar. It should be enough just to save the position and velocity of the bodies. Yeah, you'll miss the warm starting, but this shouldn't be a deal breaker in most game scenarios. So basically you have your defs for 1, and your body states for 2.

Another thing to keep in mind is that a generic save format usually wouldn't work for real games. The game needs to store it's own specific information about the state of the world. And the game will have dedicated code to drive the state of the physics systems (procedural motors, forces, etc). So you wouldn't be able to recreate a physics scene just by loading the physics data into another game engine.
ewjordan wrote:Does Bullet have anything clever going on in this regard?


Bullet supports COLLADA Physics import, which is XML based. There are plans to add snapshot export support, that allow to dump the state of the physics into COLLADA Physics .dae. However, you would still need to link the snapshot to external datastructures (graphics objects etc), and it is not lightweight. COLLADA Physics only specifies the general collision detection, rigid body and constraint data structures, so physics engine specific data still needs to be added in <extra> elements.

Erwin
Bullet supports COLLADA Physics import, which is XML based.

Overall, have you found that this adds enough functionality to make it worth the trouble of supporting it?

In another thread we were discussing networking - the ability to return to an exact prior simulation state can be very useful there, which is probably the main reason I am interested in this (another is that it would be extremely convenient to have a game recorder "built in," even if it was necessary to fiddle with it to store game specific info). CommanderKeith is working out some of this in Java, but I think his solution is extremely Java specific. I suppose it might not actually be necessary to have perfect state recreation for networking rollback, since each client is really just running an approximation of the server code, but complete recreation smooths things a little bit because you can get away with being lazier about state checking (simplest example: the server doesn't need to send a recalc command to my client if I'm the one that caused the recalc to take place, since I've already done the simulation locally; if warm-starting information is lost, I need to do a roll-back just like everyone else unless the engine is modified to clear out the warm-starting info every time I send an event).

@Erin: You're right about all of that, it's probably not worth adding just for making things cross-application; I'm still largely stuck in playing-with-the-engine phase, so I haven't started thinking too much about actual uses yet, which will be more complicated.

Even if this stuff isn't in the engine, when doing things like setting up level recording it would be useful to know: is the broadphase deterministic given the objects, or is it only deterministic given its past state? If I cleared it out and re-added all the objects, would the result definitely be the same on the next frame, or could things end up evaluated in a different order? I ask because I've had some really difficult recorder bugs with physics engines in the past thanks to broadphase issues, to the point where the only feasible option was to save every frame (whereas the ideal would be to record only user input changes and a randomization seed if you've randomized anything).
You would need to add shapes and bodies to the world in the same order and at the same time steps to reproduce the results. The reason is not so much the broad-phase, but the order of the contact constraints. The solver is sensitive to the order of constraints and the island builder does a depth-first search of the contact graph. So you can track the shape/body creation sequence to get identical results. Of course, other inputs like forces, etc, have to be identical too.
I see, the problem is more complex than I thougth.
Also, I guess it would be necessary to use handles instead of pointers for bodies, joints, etc... rigth? That would completely change the interface, not sure if that's worth it...
As for keeping track of the "user data", I cannot think of anything.

Original thread: https://box2d.org/forum/viewtopic.php?t=120