Roxside / jbox2d

Automatically exported from code.google.com/p/jbox2d
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Simulation is not deterministic #7

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Start TestBed
2. Choose any enough complicated test (dominos is good for this)
3. Observe simulation result (for example how domion blocks felt on the ground)
4. Restart simulation (or TestBed) and repeat step 3

What is the expected output? What do you see instead?

Every simulation should be exactly the same (as long as initial condition are 
the same). For example in dominos test block should always fall on ground in 
the same way.

What version of the product are you using? On what operating system?

Replicable on both "jbox2d-testbed-2.1.2-alpha-v2.jar" release from download or 
latest version of SVN. System: Windows XP SP3, java 1.6.0_15.

Please provide any additional information below.

Problem is caused by use of "Random" in dynamic tree.
Class "org.jbox2d.collision.broadphase.DynamicTree":
- line 68:
    private final Random rand = new Random();
- line 401, method "allocateNode()":
    node.key = rand.nextInt();

Adding constant seed to random generator is enough to fix problem and make 
simulation deterministic. But I find it strange to use random numbers there - 
setting "node.key" to constant value seems to work well. I also checked 
original Box2D code and random number are not used there (at least not in 
dynamic tree).

Original issue reported on code.google.com by master.z...@gmail.com on 12 Jun 2011 at 12:07

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
These keys are a work-around for the sorting method using by box2d.  They are 
only used to find duplicate contacts in the contact list, so the usage of 
random that you specify shouldn't affect anything.  I'll check it out.

Original comment by toucansa...@gmail.com on 6 Jul 2011 at 1:35

GoogleCodeExporter commented 9 years ago
Whoops, looks like that code is no longer in the updated project, and you're 
right.  I changed the tree implementation to an incremental counter, seems to 
do the trick

Original comment by toucansa...@gmail.com on 6 Jul 2011 at 1:43