Chipmunk repeatability

Official forum for the Chipmunk2D Physics Library.
B3DY
Posts: 13
Joined: Sun Jun 30, 2013 3:32 am
Contact:

Chipmunk repeatability

Post by B3DY »

I am currently part way through developing my first game with chipmunk2D on the iOS platforms.

One problem I have encountered however is the lack of repeatability within the chipmunk environment. For example, I can have a fairly simple (<20 blocks) setup and yet every time physics is applied to the body's they react in slightly differant ways. The ways they react are never violently differant yet they are differant enough to the point where it is game breaking for me.

When creating the chipmunk bodys/shapes/spaces I am including size, mass, moment, friction, and elasticity. I believe I am including everything there.

Does chipmunk use some randomization in its physics code? If not (or even if it does) What is the best way to fix this randomization
OandO
Posts: 8
Joined: Tue Jun 04, 2013 10:48 am
Contact:

Re: Chipmunk repeatability

Post by OandO »

Are you using a variable timestep perhaps? From a user's point of view, I've not seen any evidence of randomisation, scenes play out the same way every time for me.
B3DY
Posts: 13
Joined: Sun Jun 30, 2013 3:32 am
Contact:

Re: Chipmunk repeatability

Post by B3DY »

That is a possibility but I do not believe so. So far everything is setup as described in this tutorial: http://www.raywenderlich.com/3128/how-t ... s-tutorial
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Chipmunk repeatability

Post by slembcke »

So hidden in the bowels of the documentation is this:
http://chipmunk-physics.net/release/Chi ... Shape-Misc
void cpResetShapeIdCounter(void) – Chipmunk keeps a counter so that every new shape is given a unique hash value to be used in the spatial [index]. Because this affects the order in which the collisions are found and handled, you can reset the shape counter every time you populate a space with new shapes. If you don’t, there might be (very) slight differences in the simulation.
So basically whenever you start a new simulation, if you want it to act like a previous one, you'll need to ensure that things have the same hash values.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
B3DY
Posts: 13
Joined: Sun Jun 30, 2013 3:32 am
Contact:

Re: Chipmunk repeatability

Post by B3DY »

ahhh that seems like it would be the culprit! I am however having problem implementing this function into my code.

Currently I have a CCLayer where the simulation is taking place. I have tried clearing the shape counter with cpResetShapeIdCounter(); in numerous places in the layers init method and yet none seems to completely get rid of the 'random' factor.

What is the correct way to use this function? and where should it be called? I apologize for this but want to thank you for all your help!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Chipmunk repeatability

Post by slembcke »

I guess I'd call it right in the same place where you recreate the space.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
B3DY
Posts: 13
Joined: Sun Jun 30, 2013 3:32 am
Contact:

Re: Chipmunk repeatability

Post by B3DY »

I have tried calling it everywhere I can think and yet I can still not get rid of the 'randomness' :(

Edit:

Should I be setting something equal to the function or just calling it?
B3DY
Posts: 13
Joined: Sun Jun 30, 2013 3:32 am
Contact:

Re: Chipmunk repeatability

Post by B3DY »

I have now tried to run the exact same simulation on two devices, side by side, and both simulations produce different results. As a developer this is extremely frustrating as physics should by nature be 'repeatable'. Does anyone know if box2d is more reliable in this aspect?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Chipmunk repeatability

Post by slembcke »

Ooof. So that is a much bigger can of worms that you realize. I thought you meant on the same device.

It's not a physics engine problem really, it's a floating point number problem. Basically, you cannot expect any code that uses floating point numbers to give you the same results if you use a different compiler, change compiler settings, compile for a different OS, compile for a different ABI, or compile for a different CPU. Floating point numbers are not exact, so any little change to the code or the order of the machine instructions is going to give you different results. It's even worse with something like a physics library because each frame is based on the calculations of the previous frame. So any little error builds up over time. You aren't going to find that any particular "version" of the simulation is more or less correct, but they will all be slightly different. A different library isn't going to fix that.

You *cannot* rely on a simulation to be repeatable unless run within the same exact executable and with the same exact starting conditions. If you need the simulation to be the same across different devices (such as in a networked game) then you need to make sure that you compensate for that in your code. Usually this is done by making one of the simulations the server and constantly resynchronizing all of the other simulations to it. Only what happens on the server's copy of the simulation matters.

If you *really* want deterministic simulations across devices you can force software (integer) based floating point math, or implement fixed precision math. It's not really worth it though in my opinion (I certainly haven't done it). The resulting code would be much much more work and much much slower than using the (lossy) native floating point hardware and dealing with inaccuracies.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
B3DY
Posts: 13
Joined: Sun Jun 30, 2013 3:32 am
Contact:

Re: Chipmunk repeatability

Post by B3DY »

So say I have an app running on ios. If the exact same binary is running on two iphone, and the two iphones sync the exact same initial simulation conditions, they simulations will still not be the same?

I guess once you describe it in the sense that the floating point math can slightly vary from machine to machine makes a lot of sense. This isn't something I took into account simply because I did not originally realize it! I suppose I will now attempt to use the server/host setup as you mentioned, hopefully with very little noticeable lag!

Thanks for all your help, everything is much clearer now when it comes to simulations!
Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests