repeatability

Hi Erin,
I Finally got a chance to do some programming. I knew the code was going to be
a pleasure to work with without even doing any coding just from reading the board.
I did have one question though.
When I restart my program the simulation is exactly the same..excellent!
But when I destroy the world and create a new one with identical parameters
the simulations don't repeat. Just wondering if that's my bug or expected behavior.
Thanks again for a fantastic physics lib!
Mike
Hi Mike,

Are you creating everything in exactly the same order? Box2D is order dependent.

Otherwise, there is a bug in Box2D with some uninitialized memory.

It would help if you can put together a small test program that reproduces the unreproducability. 🙂
Hi Erin,
Actually the easiest thing I did was to use your testbed code so I put the world initialization
in the compound shapes header like this:
public:
CompoundShapes()
{
b2AABB worldAABB;
worldAABB.minVertex.Set(-100.0f, -100.0f);
worldAABB.maxVertex.Set(100.0f, 200.0f);
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
bool doSleep = true;

m_world = new b2World(worldAABB, gravity, doSleep);

Just did that for testing purposes of course..

So each time you run compound shapes the bodies should end up in the same
configurations I would imagine?
Hope that helps! 🙂
Mike
The compound shapes demo randomizes the positions of the shapes, so I wouldn't expect that one to repeat, at least if you're using the original body creation code.

If you want repeatability in that situation, you'll have to replace the calls to b2Random with something non-random, or at least re-seed the random number generator with the same seed every time you re-initialize (the function you want is srand(unsigned int), I think - I'm pretty sure that b2Random uses the usual rand() function).

If you're not getting repeatable results in one of the non-randomized demos, that would definitely be a bug (CompoundShapes and PolyShapes are both randomized, but I think most of the others are not, apart from "the bomb"). I've never noticed any problems in the Java version, but Java makes you be much more careful about initializing things before you use them, so... Other than that possibility, to my knowledge every operation in the engine should be deterministic.

I have no idea how things work across platforms, though - in Java you theoretically have to mark things as strictfp in order to ensure cross-platform determinism, and I don't know how it works in C++. Sometimes people like to do their math in fixed point to make sure things are the same everywhere.
Oh thats great news!
Thank you very much for explaining the seemingly non-deterministic behavior.
Mike
So did you get exact repeatability when the positions are not randomized?
It's funny, there actually is repeatability in that every 2nd run is the same as
every other 2nd run..3rd run etc..so the random numbers would explain that.
So it works..fantastic!
Mike

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