Box2D Forums

It is currently Wed Sep 17, 2014 2:34 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Thu Jun 02, 2011 6:19 pm 
Offline

Joined: Mon May 11, 2009 9:57 am
Posts: 17
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.


Top
 Profile  
 
PostPosted: Thu Jun 02, 2011 7:30 pm 
Offline

Joined: Mon May 11, 2009 9:57 am
Posts: 17
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:
Quote:
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?


Top
 Profile  
 
PostPosted: Thu Jun 02, 2011 7:34 pm 
Offline

Joined: Mon May 11, 2009 9:57 am
Posts: 17
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?


Top
 Profile  
 
PostPosted: Thu Jun 02, 2011 8:15 pm 
Offline

Joined: Mon May 11, 2009 9:57 am
Posts: 17
Okay, actually it's only working some of the time. I have no idea.


Top
 Profile  
 
PostPosted: Thu Jun 02, 2011 11:01 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1943
Location: Tokyo
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.


Top
 Profile  
 
PostPosted: Fri Jun 03, 2011 7:47 pm 
Offline

Joined: Mon May 11, 2009 9:57 am
Posts: 17
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?


Top
 Profile  
 
PostPosted: Fri Jun 03, 2011 9:14 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1943
Location: Tokyo
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:
printf("%08X", *((int*)(&myfloat)) );


Top
 Profile  
 
PostPosted: Sat Jun 04, 2011 12:19 am 
Offline

Joined: Mon May 11, 2009 9:57 am
Posts: 17
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?


Top
 Profile  
 
PostPosted: Sat Jun 04, 2011 9:25 pm 
Offline

Joined: Wed Apr 13, 2011 5:58 pm
Posts: 3
Have you read my post on this page?
viewtopic.php?f=9&t=5320&start=20


Top
 Profile  
 
PostPosted: Sat Jun 04, 2011 11:00 pm 
Offline

Joined: Mon May 11, 2009 9:57 am
Posts: 17
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group