For my application (a game), I need to have multiple instances of JBox2d running on identical input, and giving the same results. In testing, I'm seeding the worlds with identical objects, and running them side by side. But after a few seconds, the simulations stop matching. Objects end up in different places, etc. I'm using JBox2d 2.1.2.
I've seen some mention of "strictfp" around, is that known to solve the problem? And also, where is the 2.1.2 source code? I found an svn, but don't think it was the right one.
Also, I tested my framerate in a world with a few hundred objects, and it was unaffected by the use of strictfp.
Hopefully someone else will find this useful. Any chance of this being implemented in the main trunk of the library?
Okay, actually it's only working some of the time. I have no idea.
I've spent a bunch more time on this. I still haven't gotten to the bottom of it.
Using the vanilla 2.1.2 alpha, the simulations match about 50% of the time.
I was generating the world with the same random seed (0). I also tried generating a world that is just a grid of boxes. In both simulations, I was just launching them all into the center.
There are no random calls in the JBox2d source at all, but could the random results be coming from randomness in the way some collections / hashmaps are being allocated in memory, or the order they're being iterated in?
I've tried a few engines so far, and found none of them to be deterministic. I'd really like to use JBox2d, as it seems like the best maintained, and the fastest, but has anyone know of a Java 2d physics engine that's deterministic?
Yeah. The problem is that I've made the test as simple as possible, and verified that the worlds are identical after they are created. Then they are left to run with no further input, but still diverge 50% of the time. I know the C++ version of Box2d is said to be deterministic, at least on one computer. But it seems that JBox2d is not.
I am a pretty competent programmer, but I have to admit that the library code is beyond me. Could anyone point me at areas that might be getting unsynched?
Thanks so much man, that fixed it. When I searched for all calls to Random in the source code, I only found the ones in MathUtils, which as far as I can tell, aren't ever called in the library itself. I missed the one in DynamicTree, which was the culprit.
And to think I had given up hope on this project. Thanks again!