Saving State?

Official forum for the Chipmunk2D Physics Library.
Post Reply
chipmunkuser
Posts: 19
Joined: Fri May 09, 2008 4:48 pm
Contact:

Saving State?

Post by chipmunkuser »

Is there a clean way to save and load the state of the simulator? (Saving mid-action to a file to be loaded later and continued form the same spot)
I know I could save the object values (I'm assuming all body/shape parameters) and then re-allocate and init them on loading but what about any intermediate caching/engine data? Does any of this need to be saved to have everything continue where it left off. Do i need to go through and save everything for the space, etc?

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

Re: Saving State?

Post by maximile »

I've been thinking about that too, so that I can cache frames and scrub through them.
User avatar
tartley
Posts: 37
Joined: Thu Jun 12, 2008 5:01 pm
Location: London, England
Contact:

Re: Saving State?

Post by tartley »

I'll be attempting to implement this in a couple of weeks too, so I'd be interested if anyone has any thoughts on the matter.
[color=#808080]Tartley - Jonathan Hartley, [url]http://tartley.com[/url]
Using Pymunk (Chipmunk's Python bindings) for a game project:
[url]http://code.google.com/p/sole-scion[/url][/color]
Stefan
Posts: 4
Joined: Tue Mar 31, 2009 3:56 pm
Contact:

Re: Saving State?

Post by Stefan »

What's the word on this?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Saving State?

Post by slembcke »

Officially, saving of state is not supported. Saving and restoring the full state of a Chipmunk space would be fairly tricky and time consuming to implement. The problem is that I couldn't just make a cpSaveSpaceToFile(space, "neat.txt") API call because it would need to provide some way for you to match up all the shapes, bodies and joints with your own data structures when it's reloaded.

For ScribBall, I implemented save games by just storing the position, rotation, velocity, radius, etc of all of the balls. When the game restarts there are a few frames where everything slips a little before the cached impulses for all the contacts stabilize, but it's not so bad really. I've also seen similar things happen in AAA games, so I'm assuming that I'm not the only one who thinks that this isn't a trivial thing to implement.

Though this has made me think, if the you are responsible for saving and re-adding all of the shapes, bodies, and joints back to the space, then the only thing missing is the cached impulses that are stored internally to the space. These impulses are stored using a pair of shape pointers as a key. It would be possible to save and reload these values if there was some unique value provided for each shape. The shape.id field was added so that the determinacy of the simulation could be controlled, but if stricter requirements were put on it's uniqueness, it could be used for the key instead of the shape's pointer. That significantly reduces the scope of what I would need to implement.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
CapnPolo
Posts: 3
Joined: Tue Dec 09, 2008 5:28 am
Contact:

Re: Saving State?

Post by CapnPolo »

slembcke wrote:Though this has made me think, if the you are responsible for saving and re-adding all of the shapes, bodies, and joints back to the space, then the only thing missing is the cached impulses that are stored internally to the space. These impulses are stored using a pair of shape pointers as a key. It would be possible to save and reload these values if there was some unique value provided for each shape. The shape.id field was added so that the determinacy of the simulation could be controlled, but if stricter requirements were put on it's uniqueness, it could be used for the key instead of the shape's pointer. That significantly reduces the scope of what I would need to implement.
That sounds promising... any chance of it going beyond an idea and into the "roadmap" (if there is one)?
ivan_m
Posts: 2
Joined: Wed May 27, 2009 11:08 am
Contact:

Re: Saving State?

Post by ivan_m »

Hi. I'm using chipmunk library from cocos2d-iphone-0.7.2.
I'm having problems restoring state of simulation.
I've some static shapes and several circles.
I restore each circle like this:

Code: Select all

CGPoint p = {[[dic objectForKey:@"body_p.x"] floatValue], [[dic objectForKey:@"body_p.y"] floatValue]};
cpFloat r = [[dic objectForKey:@"circle_r"] floatValue];
shape_it = [self addBall:p radius:r]; // this allocates body, attaches circle shape and adds to space, just like in 'normal' program operation.

cpShape *s = shape_it->second._s;
cpCircleShape *circle = (cpCircleShape*)s;
circle->c = CGPointMake([[dic objectForKey:@"circle_c.x"] floatValue], [[dic objectForKey:@"circle_c.y"] floatValue]);
circle->tc = CGPointMake([[dic objectForKey:@"circle_tc.x"] floatValue], [[dic objectForKey:@"circle_tc.y"] floatValue]);

cpBody *b = s->body;
cpBodySetMass(b, [[dic objectForKey:@"body_m"] floatValue]);
cpBodySetMoment(b, [[dic objectForKey:@"body_i"] floatValue]);
cpBodySetAngle(b, [[dic objectForKey:@"body_a"] floatValue]);
b->w = [[dic objectForKey:@"body_w"] floatValue];
b->t = [[dic objectForKey:@"body_t"] floatValue];
b->p = CGPointMake([[dic objectForKey:@"body_p.x"] floatValue], [[dic objectForKey:@"body_p.y"] floatValue]);
b->v = CGPointMake([[dic objectForKey:@"body_v.x"] floatValue], [[dic objectForKey:@"body_v.y"] floatValue]);
b->f = CGPointMake([[dic objectForKey:@"body_f.x"] floatValue], [[dic objectForKey:@"body_f.y"] floatValue]);
b->rot = CGPointMake([[dic objectForKey:@"body_rot.x"] floatValue], [[dic objectForKey:@"body_rot.y"] floatValue]);
cpShapeCacheBB(s);
The shapes restore on proper places and behave properly - bouncing from static shapes and other balls, etc,
but collision pair functions fire on improper times for restored shapes so that my app went really crazy after restoring.
How do one save/restore state so that collision pair functions would not break?
Thanks.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Saving State?

Post by slembcke »

You'll have to be more specific than that. The collision pair functions only get called when collisions happen. Are they getting called for the wrong collisions? How are you restoring the collision types and the functions?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ivan_m
Posts: 2
Joined: Wed May 27, 2009 11:08 am
Contact:

Re: Saving State?

Post by ivan_m »

Thanks for prompt reply, really appreciate it!
And I really really sorry for disturbing you because this is purely my fault - I did incorrectly init collision type property for restored shape.
Sorry once more and thanks :)
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Saving State?

Post by Buschmaster »

I'm a bit late to the party, but...

If you're working in Objective-C you could use NSCoder, right?

I'm not certain on this, but I'm fairly sure you could use NSCoder to basically save the bodies as NSData and reload them.

I have pause & resuming almost ready for my next game that will work more like what Scott said. It took just a few quick functions, the only problem I'm having now is making sure it resumes in a happy way without any unexpected weirdness.
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests