Page 1 of 1

Is there a way to copy a space safely?

PostPosted: Sun Oct 23, 2011 9:47 pm
by fanhe
As title, I have not found such api. Is there a plan to add a similar api?
Thanks.

Re: Is there a way to copy a space safely?

PostPosted: Mon Oct 24, 2011 9:23 am
by slembcke
Currently no. That's actually a lot more difficult than it sounds.

Adding serialization is one of my higher priority features to add in the future and that would allow for copying. Not sure when it will be done though.

Re: Is there a way to copy a space safely?

PostPosted: Tue Oct 25, 2011 10:26 pm
by fanhe
Thanks for reply.
I will try other methods.

Re: Is there a way to copy a space safely?

PostPosted: Fri Jan 13, 2012 12:53 pm
by ShiftZ
slembcke wrote:Currently no. That's actually a lot more difficult than it sounds.

Adding serialization is one of my higher priority features to add in the future and that would allow for copying. Not sure when it will be done though.


With that cool feature you can get hardest pain in ass you ever had with chipmunk. You'll have to reflect all internal structural changes you made in serialization and desrealization code. That might become a real problem when planning new feature.

Re: Is there a way to copy a space safely?

PostPosted: Fri Jan 13, 2012 3:19 pm
by slembcke
Yeah, I know. It will be a pain, but I think it will open up a lot of opportunities. I'll probably develop the serialization stuff in a separate branch so that any other Chipmunk changes don't have to be developed in lockstep.

Re: Is there a way to copy a space safely?

PostPosted: Mon Jan 27, 2014 2:34 pm
by nikki
Actually, because of the nice property listings in cpShape.h and cpBody.h, this isn't too bad. I support save/load in my game engine and the save/load of chipmunk bodies and shapes works out nicely. The code is around here.

What is the other data that should be save/load'd? I'm guessing 'global properties' such as iterations or gravity, and this would all be in cpSpace.

Re: Is there a way to copy a space safely?

PostPosted: Fri Jan 31, 2014 2:36 pm
by slembcke
Saving out a list of the bodies/shapes/constraints gets you most of the way there in practice, and that's pretty easy with the exception of supporting *all* of the constraint types. The problem is that it doesn't store the persistent contact information, or the internal state of the collision detection structures. This is necessary for collision events (begin, separate, etc) to span across serialization calls correctly and for the collision detection to be deterministic. Ideally if you save a simulation and play it back again it shouldn't give you a different result. For that to work, it really has to recreate all of the data structures verbatim, and that's where it gets hard.

Re: Is there a way to copy a space safely?

PostPosted: Sun Feb 15, 2015 12:22 am
by log_2
fanhe wrote:Is there a way to copy a space safely?


Depends what you mean by copy. If you want to keep states of all spaces and switch between these states in memory in the current process (i.e. no write/load to disk between processes), then it isn't hard to already do this. All you need is to create custom allocator functions and define cpcalloc, cpfree etc to your functions. Then keep a global memory arena which your allocator uses, which is large enough to hold your spaces. When you want to save your spaces, just memcpy your arena to some other block of memory, and when you want to restore your space just memcpy it back.

I tried to do this with Casey Muratori's looped live reloading, but unfortunately chipmunk litters function pointer in with its data, so once the dll is reloaded the function pointers inside the memory arena are often no longer correct. I'm not sure how to fix this problem. Perhaps resetting the function pointers after the dll is loaded will work, but I will need to patch chipmunk to expose these functions so that the pointers can be reset.