same simulation - different outcomes?

dc443 wrote:
deterministic behavior ... across machines and architectures and platforms

I managed to do this previously with the ODE physics library (like a 3D version of Box2D), at least for standard float variables across win32/linux. I did not try it with double precision floats.
Looking back over my notes, it seems that "On windows use /Op and _controlfp to set the precision to 24 bits. On linux/gcc compile with -ffloat-store" was what I came up with. And if you want to have the same results across debug and release builds, you will need to disable optimizations in the release build. Dunno if that helps anyone using Flash though...
irresistible force wrote:
dc443 wrote:
deterministic behavior ... across machines and architectures and platforms

I managed to do this previously with the ODE physics library (like a 3D version of Box2D), at least for standard float variables across win32/linux. I did not try it with double precision floats.
Looking back over my notes, it seems that "On windows use /Op and _controlfp to set the precision to 24 bits. On linux/gcc compile with -ffloat-store" was what I came up with. And if you want to have the same results across debug and release builds, you will need to disable optimizations in the release build. Dunno if that helps anyone using Flash though...


That's actually pretty neat. I wonder if there could be a patch made for box2d to switch into "deterministic mode" where all results are guaranteed to be the same given the same input.
dc443 it seems your right, tried this on a mac and results were totally different to anything I saw on the 2 pc's i tested. I guess I'll have to go a different way for this project, as its flash people will be viewing it on all sorts of systems so its unrealistic to assume everyones on a pc and so on, a shame 🙁

Thanks for all the input guys
Deterministic physics allows for a couple interesting gameplay capabilities, the simplest being the ability to record a replay of any kind of action as long as an initial state and all of the input is captured accurately. Without that, to obtain the same quality, a snapshot of the simulation must be taken at every physics step.

Beyond that, there are many kinds of games where you can set up contraptions or modify some initial parameters and run the simulation to see if you can accomplish some sort of task; obviously if the engine wasn't deterministic (at the very least, having identical behavior on the same machine), it would make things a lot worse because the set of solutions on one machine would be different from time to time and from machine to machine.

I think box2d is very well suited for these games because it's probably the best overall 2d realtime physics simulator out there. It would be made even more awesome by allowing a switch (somewhere in b2Settings.h would be just dandy) that we could throw to put it in "deterministic mode".

This is just an idea: once the particular computations that are causing the different outcomes are identified, i think an easy way to go about doing this is to just perform those particular calculations using higher precision, and then cast results down to float32 at the crucial point? I'm not sure if this would only serve to reduce the chaos rather than eliminate it altogether.
dc443 wrote:
Deterministic physics allows for a couple interesting gameplay capabilities, the simplest being the ability to record a replay of any kind of action as long as an initial state and all of the input is captured accurately.

We do exactly this in our current game, and it seems to work properly, regardless of the platform it's run on! Not sure what we're doing different, but it works. (We are currently using v2.0 of the Box2D Flash port.)
I haven't looked at the Flash version of Box2D (I'm using the C++ version), but certainly so far the engine appears to be completely deterministic if you run with a fixed timestep. I ran your demo, resetting it a few times, and saw at least half a dozen different outcomes.

I've worked on games that have demanded determinism before, and to debug nondeterministic behaviour, really your best weapon is a completely thorough logging system. Run the game, and log the state of every meaningful variable every frame, to the highest accuracy you can manage. Do it a few times, and get different logs from runs which end up with different outcomes. Then go through the logs (some kind of diff tool is handy for this) to find the first frame where a variable is different, and then try to work out why that happened. Could be a rounding error, or a timing error of some sort. In some cases, the difference might have happened a few frames before the logging picks up on it (so add more logging!). It's tricky, but it's certainly possible to ensure determinism in all areas of your code if you're ruthless enough about tracking the bugs down.

I didn't try it, but shaktool said that it ran the same for them every time when they refresh the page, but it runs differently when they hit the reset button. I'd agree with them that this strongly suggests you should take another look at your reset function and make sure that it sets every single initial condition in exactly the same way to how it does when you first start. If your code doesn't do this already, just destroy the physics world and then recreate it using the same routine as you do on the first run.
Yeah and I also seemed to miss the fact that box2d does appear to have completely deterministic behavior at least on my system but I wouldn't know what would happen if I ported my program or ran it on another box with different hardware, however, I have set it to use constant timesteps and so far I can count on the exact same scene happening every time I run my program as long as I don't interfere with anything.
Hm... I just tested our Box2DFlash game on a PPC machine, and the replay fails! On all Intel machines it works, but on PPC the simulation is different.

This is trouble, for us! Is the only solution to go to a fixed-point version of Box2D? Would that even guarantee determinism across processor architectures?
ElectroDruid wrote:
... to debug nondeterministic behaviour, really your best weapon is a completely thorough logging system. Run the game, and log the state of every meaningful variable every frame, to the highest accuracy you can manage. Do it a few times, and get different logs from runs which end up with different outcomes. Then go through the logs (some kind of diff tool is handy for this) to find the first frame where a variable is different, and then try to work out why that happened.

Just wanted to add my two cents on this. I used a very similar system where I generated a crc number for every frame of the physics stepping, then checked if that number was the same on a subsequent run with the same input. This reduces the amount of data needed to pinpoint the frame where the two simulations first differ.
Then I made a textual dump of the world state from each run, and used tortoise svn's diff (just put the two files in the same folder, select them both, right click and choose 'Diff', gives you a very nice visual diff.
Something to be aware of is that floating point numbers with very small differences will not be visible in the textual dump if you do it using for example printf("%f", myfloat);
You will probably need to convert the float to its hex representation to make sure that even one bit of difference will show up.
Oh, it's worth mentioning - if you really want determinism, the best solution is to use the fixed point version of the library, though I realize that only helps C++ Box2d users. Even across processors, integer arithmetic should always be the same (except perhaps due to different overflow handling).

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