Physics repeatable?

Official forum for the Chipmunk2D Physics Library.
gerrit
Posts: 6
Joined: Wed Feb 24, 2010 5:53 am
Contact:

Physics repeatable?

Post by gerrit »

If you run Chipmunk with the same objects, forces, constraints, etc. should you get the same result every time? I seem to be getting wildly different results on every run (although the physics look correct and seem to be working correctly). It's possible I am not reseting something correctly, but I've gone over everything twice.

PS - Chipmunk is a great engine. I really hope people don't take for granted how much work has gone into it. You owe us nothing and give us everything. Thank you!
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: Physics repeatable?

Post by Tam Toucan »

Are you calling cpResetShapeIdCounter? You need to. The other issue could be the delta times you pass to cpSpaceStep not being the same for both passes (depends how you're doing it).

Other than that I assume you've no time based stuff e.g. after 3 seconds create a shape, since the timer won't fire at the same time each pass.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Physics repeatable?

Post by slembcke »

Yup, if you want 100% repeatable physics you need to call that function to reset the IDs when you recreate the space. The reason is that the IDs change the iteration order of the spatial hashes which means collisions are found in slightly different orders.

You should also keep in mind that the physics will run differently depending on the compiler settings you use for floating point code, the processor it's running on, and the version of Chipmunk used. It can be a bad idea to rely 100% on a complicated physics outcome.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ShiftZ
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am
Contact:

Re: Physics repeatable?

Post by ShiftZ »

the processor it's running on
Are you sure? Any clues on why it is so?
Repeatable processor independent physics is very impotent in multiplayer games with physics.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Physics repeatable?

Post by slembcke »

Different processors have instructions that can combine multiple operations, like doing a multiply and an add in one step. Additionally, they often keep different amounts of internal precision. IIRC PPC processors only have instructions that operate on doubles, and x86 CPUs perform more accurate math on single precision floats be keeping a larger than 32 bit result.

If you want to do anything that is perfectly reproducible for running things in lock-step, you basically cannot be using floats.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ShiftZ
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am
Contact:

Re: Physics repeatable?

Post by ShiftZ »

That is really sad for PC platform.
But can i rely on same results on identical pair of devices simulating in different time?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Physics repeatable?

Post by slembcke »

Given the exact same starting conditions yes.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
gerrit
Posts: 6
Joined: Wed Feb 24, 2010 5:53 am
Contact:

Re: Physics repeatable?

Post by gerrit »

Thanks for the quick replies Tom and Scott and Tom.

I'm using a constant frame step and rebuilding the space the same way every time, but I just realized although I manually reset the bodies, I haven't been resetting the internal state of the shapes. I'm gonna fix that and give it a try.

In my game when a level is running, the user is really only watching results of physics, so if they don't change anything in the setup, it helps the illusion if things run consistently every time the run it =)
Repeatable processor independent physics is very impotent in multiplayer games with physics.
If you're running over a network you're going to have to sync the results from time to time anyway. (Unless you require all player interaction to be synced between simulations as it happens... in which case god help you.) Look carefully at the best online games (ie. any popular online shooter, Wii Mario Kart, etc.) and they're all really good at masking the lack of consistency between simulations.
Vico
Posts: 13
Joined: Fri Jul 06, 2012 10:12 am
Contact:

Re: Physics repeatable?

Post by Vico »

Wow... It's a big situation I didn't think about till now (after 2 month of work). I finished the game at 90% with all the graphical / simulation aspects of my shooting game, and I'm just starting the multiplayer part now.

I'm afraid of having different simulations on each device, that would be catastrophic since the players fire bullets by aiming with precision to make ricochets on small objects and walls to touch another player :cry:
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Physics repeatable?

Post by slembcke »

Turn based or real-time? With turn based, you simply need to make the player's machine that plays a particular turn be the authority on how it turns out. With real-time, you have a way bigger problem, latency. You absolutely *must* design around it to be constantly resynchronizing the two machine's state.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests