Page 1 of 2
Physics repeatable?
Posted: Wed Feb 24, 2010 6:03 am
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!
Re: Physics repeatable?
Posted: Wed Feb 24, 2010 6:12 am
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.
Re: Physics repeatable?
Posted: Wed Feb 24, 2010 10:31 am
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.
Re: Physics repeatable?
Posted: Thu Feb 25, 2010 3:38 am
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.
Re: Physics repeatable?
Posted: Thu Feb 25, 2010 10:29 am
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.
Re: Physics repeatable?
Posted: Thu Feb 25, 2010 12:43 pm
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?
Re: Physics repeatable?
Posted: Thu Feb 25, 2010 1:57 pm
by slembcke
Given the exact same starting conditions yes.
Re: Physics repeatable?
Posted: Fri Feb 26, 2010 5:39 am
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.
Re: Physics repeatable?
Posted: Sat Sep 08, 2012 3:44 am
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

Re: Physics repeatable?
Posted: Sat Sep 08, 2012 9:45 am
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.