Can JBox2d be deterministic?

Discuss issues specific the Java port of Box2D
Driscoll
Posts: 17
Joined: Mon May 11, 2009 9:57 am

Can JBox2d be deterministic?

Postby Driscoll » Thu Jun 02, 2011 6:19 pm

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.

Driscoll
Posts: 17
Joined: Mon May 11, 2009 9:57 am

Re: Can JBox2d be deterministic?

Postby Driscoll » Thu Jun 02, 2011 7:30 pm

Okay, I went ahead and added "strictfp" to all the classes in the library. So far, it seems to have worked. Here's the command I used to do most of the work:

run this in the "src/" folder of your library:
grep -rl "" ./ | xargs sed -i "s/class/strictfp class/"


Note, I am using this version of the library:
http://code.google.com/p/jbox2d/source/browse/#svn%2Ftrunk%2Fupdating%2Fjbox2d-library%2Fsrc%2Fmain%2Fjava

And then I had to clean up some stuff that got garbled, but it only took about 5 minutes.

Does anyone see anything wrong with this?

Driscoll
Posts: 17
Joined: Mon May 11, 2009 9:57 am

Re: Can JBox2d be deterministic?

Postby Driscoll » Thu Jun 02, 2011 7:34 pm

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?

Driscoll
Posts: 17
Joined: Mon May 11, 2009 9:57 am

Re: Can JBox2d be deterministic?

Postby Driscoll » Thu Jun 02, 2011 8:15 pm

Okay, actually it's only working some of the time. I have no idea.

irresistible force
Posts: 1988
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Can JBox2d be deterministic?

Postby irresistible force » Thu Jun 02, 2011 11:01 pm

ah, my favorite topic... kind of.
You might find this thread an interesting read: viewtopic.php?f=3&t=5453
The link in my post is down but I will put it back up if you like.

Driscoll
Posts: 17
Joined: Mon May 11, 2009 9:57 am

Re: Can JBox2d be deterministic?

Postby Driscoll » Fri Jun 03, 2011 7:47 pm

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?

irresistible force
Posts: 1988
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Can JBox2d be deterministic?

Postby irresistible force » Fri Jun 03, 2011 9:14 pm

I'm afraid the best way to find the reason is probably what I said in the thread above. Every frame, dump a textual representation of the world into a file, then do two runs like that, running until you visually see a difference between them as they are running. Compare the files for the two runs with a diff tool (the tortoise svn tool on Windows is great for this) to see what the first variable to differ was.

An important point to note here is that dumping human readable floating point values (eg. 17.46678) will not be good enough to catch a very tiny difference between two floats because this representation can get rounded for printing. In C you can do this to print the hex representation of a 32-bit float, which will catch even one bit of difference between two floats. In Java... ymmv.

Code: Select all

printf("%08X", *((int*)(&myfloat)) );

Driscoll
Posts: 17
Joined: Mon May 11, 2009 9:57 am

Re: Can JBox2d be deterministic?

Postby Driscoll » Sat Jun 04, 2011 12:19 am

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?

progers7
Posts: 3
Joined: Wed Apr 13, 2011 5:58 pm

Re: Can JBox2d be deterministic?

Postby progers7 » Sat Jun 04, 2011 9:25 pm

Have you read my post on this page?
viewtopic.php?f=9&t=5320&start=20

Driscoll
Posts: 17
Joined: Mon May 11, 2009 9:57 am

Re: Can JBox2d be deterministic?

Postby Driscoll » Sat Jun 04, 2011 11:00 pm

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!


Return to “Java”



Who is online

Users browsing this forum: No registered users and 2 guests