[pymunk] repeatability

Official forum for the Chipmunk2D Physics Library.
Post Reply
stampson
Posts: 3
Joined: Sun Aug 31, 2008 7:31 pm
Contact:

[pymunk] repeatability

Post by stampson »

I'm working on a simulator in pymunk, and it has an editor. The editor saves the state of the space before the sim begins running, so that it can run, reset, and run again. I would like for it to produce the same results every time after running for the same number of steps of the same interval.

I've managed to achieve what appears to be repeatability on each of my two machines individually - one a mac, one winxp. The same number of steps results in the same ending configuration. However, when the same starting state is used on the mac and the xp machine, the results are in sync for a little bit - perhaps 50 steps or so, but by 100 steps there are subtle variations.

Before the sim starts, everything is static shapes. When the sim starts, I reset the shapeIdCounter and then purge the shapes from the static space and then create them in the active space in the same order. I'm using the same timestep and number of steps on each machine.

Is repeatability even possible with pymunk (or chipmunk) on the same platform/machine, much less cross-platform? I'm interested because of the implications it has for networking - if the server can run the sim with the same outcome as the client, then it is not necessary to stream the updates - the client can compute them (there is no user interference while the sim is running).

-Stampson
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: [pymunk] repeatability

Post by maximile »

Sounds like you're doing everything right. Because of the way different configurations handle floats, it won't necessarily behave the same way. Repeatability on a single configuration is perfectly possible, though, as you've seen. You might be able to make the simulations run more similarly if you defined cpFloat as a double (not sure how to do that in pymunk).

For networking you'll just have to find another way to do it - either do it all on the server, or have the clients do most of the work but synch them every so often with a copy from the server or whatever.
stampson
Posts: 3
Joined: Sun Aug 31, 2008 7:31 pm
Contact:

Re: [pymunk] repeatability

Post by stampson »

My further diagnosing does indicate it is differences in floats, as you said. It might be addressable in chipmunk, but I don't see how it could be in pymunk. At any rate, it's not a showstopper for my project, and I'm still most impressed with chipmunk and the pymunk bindings.

Maybe if chipmunk used ints instead of floats! ;)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] repeatability

Post by slembcke »

Chipmunk uses doubles by default. On both my G5 and Core 2 Duo machine, it benchmarked slightly faster using doubles than floats.

While I did add some things to Chipmunk to aid in determinism, there is no way to do it across processor architectures. I vaguely seem to recall that if you compile the code as -O0 in GCC that it runs the same. This sort of makes sense as it turns off any optimizations that could rearrange or combine the math operation depending on what instructions the CPU has available.

As you say, the only way that you could get absolute cross platform determinism is if you used fixed point math using integers. On a modern machine, this would probably be much slower, and much more of a hassle. Don't expect this to happen from me unless I decide to take up DS programming or something. :p

This is a problem common to just about anything that uses floating point math. If you are recording an animation for in a game, pre-calculate the physics. If you are relying on a precise and intricate path of an object as part of scripted gameplay, don't. If it's just an editor for the fun of it, don't worry so much. Unless it causes something to explode on another platform, nobody is going to notice.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: [pymunk] repeatability

Post by viblo »

Both the version used by pymunk and latest trunk of chipmunk has cpFloat defined to float and not double?
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] repeatability

Post by slembcke »

Deciding that I should probably double check such a statement... It turns out that I never made the switch in trunk, only my own personal projects. (sigh)

It's a simple recompile however. Change the typedef at the top of chipmunk.h and recompile.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jakov
Posts: 8
Joined: Tue Nov 27, 2012 1:19 pm
Contact:

Re: [pymunk] repeatability

Post by jakov »

If Chipmunk was compiled with a C++ compiler, couldn't cpFloat be overloaded to some horrific C++ class?
(Which used fixed point math.)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] repeatability

Post by slembcke »

Well, yes and no.

Chipmunk makes it easy to redefine the floating point type. In C++, you could even use a custom type if you implemented a bunch of the basic (+-*/) operators. I'm sort of guessing that you've never tried using fixed point though. While it's faster than software based floating point on a CPU without an FPU, it's considerably slower when you have an FPU. You also have to be *very* careful of overflows with pretty much every bit of code you write. This would require changing the order of operations on many many lines of math within Chipmunk. Possibly even un-inlining a lot of the vector math. Even after all of that, you'd need to be very careful about what values you pass to Chipmunk so they don't cause overflows.

It's not that it's impossible, but that it's a lot of work and the end result isn't that great. Fixed point has died a pretty fast death in the last few years. Even the cheapest of mobile CPUs have FPUs. I don't really see much of a point anymore. If somebody wanted to step up and make a fork, I certainly wouldn't get in their way though. ;)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jakov
Posts: 8
Joined: Tue Nov 27, 2012 1:19 pm
Contact:

Re: [pymunk] repeatability

Post by jakov »

Ok, I you just saved me from a wild goose chase.

I wanted it not for speed, but for cross platform repeatability, btw.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: [pymunk] repeatability

Post by slembcke »

Well, if that's the case, you are probably better off disabling hardware floating point instead. It would be a lot slower, but that's a much easier limitation to work around than precision issues which would pop up all the time.
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: Heise IT-Markt [Crawler] and 6 guests