Determinism problem

Post Reply
Phong13
Posts: 4
Joined: Tue Apr 12, 2016 7:21 pm

Determinism problem

Post by Phong13 »

I have a main bullet simulation loop in my game. A ball is thrown against a wall, bounces down some stairs and rolls across a floor. I would like to predict the simulation ahead of time. To do this I am creating a second DiscreteDynamicsWorld, creating duplicates of all my RigidBodies with the same position, orientation, linear and angular momentum as well as other settings. I step the second physics world several hundred steps.

The problem is that after a few collisions, the simulations start to diverge. The positions of all rigid bodies match exactly for the first hundred or so steps.

If I run the main simulation with a script, it completes the same way every time with no divergence.

Is this to be expected? It is possible that rigidbodies are being added in the second simulation in a different order. I am using Bullet 2.83
inzombiak
Posts: 16
Joined: Wed Nov 25, 2015 3:34 pm

Re: Determinism problem

Post by inzombiak »

Are you using a fixed timestep?
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Determinism problem

Post by benelot »

Can you follow the pages I proposed in this post and check what you missed? There are things you can mess up especially related to stepSimulation that can make your simulation run stochastically.

http://www.bulletphysics.org/Bullet/php ... 688#p37688
Phong13
Posts: 4
Joined: Tue Apr 12, 2016 7:21 pm

Re: Determinism problem

Post by Phong13 »

inzombiak wrote:Are you using a fixed timestep?
Thanks for the responses. I am using a fixed timestep. To simplify the problem I am stepping both worlds exactly the same way:

Code: Select all

for (int i = 0; i < 500; i++){
world.StepSimulation(1/60f, 10, 1/60f);
}
I have read the wiki pages on StepSimulation and Cannonical Game Loop. And am 99% certain that I am implementing them correctly. Can the simulation be affected if the rigidbodies are added to the second physics world in a different order? I came across a post yesterday (can't find it today) suggesting that in bullet 2.x some of the lists in the solver are not sorted which can result in different results because the collisions can be resolved in a different order. Apparently bullet 3 sorts these lists which resolves this problem. Can anyone confirm if this is the case?
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Determinism problem

Post by benelot »

That sounds about right regarding the time step.

Can you make both worlds add the rigidbodies in the same order? I have no idea if this breaks determinism, I would hope not, but you can easily test this.
Phong13
Posts: 4
Joined: Tue Apr 12, 2016 7:21 pm

Re: Determinism problem

Post by Phong13 »

I finally had some more time to do some further testing.

It appears that the order that rigidbodies have been added to the world does affect the simulation. If I run the simulation with rigidbodies added in the same order then the results are the same. If I add them in reverse order then the results are different.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Determinism problem

Post by benelot »

Thanks for testing. Well, always learn something new every day! Can yoh make your simulation add the objects in the same order? I mean, could you solve your problem with this new insight?
Post Reply