Remove all bodies

Posted:
Mon Jan 21, 2013 6:36 am
by Setrino
Hi. I want to iterate through the array of shapes->bodies in the space and remove all of them from space.
I am not sure how to make a for loop for the bodies in the space. Can someone please help me.
Re: Remove all bodies

Posted:
Mon Jan 21, 2013 10:40 am
by slembcke
You need to use the cpSpaceEachBody() iterator function and a post-step callback to do that. There is an example here:
https://github.com/slembcke/Chipmunk-Ph ... emo.c#L117
Re: Remove all bodies

Posted:
Mon Jan 21, 2013 12:14 pm
by slembcke
Which functions are you asking about? ChipmunkDemoFreeSpaceChildren() is a function only defined in the demo app, postBodyFree(), etc are static functions that are only defined for ChipmunkDemo.c. If you want to use these functions, you'll have to copy the declarations into your program.
The cpSpace*() functions they call are part of Chipmunk though.
One more thing to keep in mind is that you must track any rogue or static bodies you create in your own code. Since they are never added to a cpSpace, you can't use a Chipmunk space to manage them. Chipmunk spaces aren't meant to be collection classes to help manage memory. They are only meant to perform simulations and queries.
Re: Remove all bodies

Posted:
Mon Jan 21, 2013 6:40 pm
by slembcke
So you are freeing all the objects but trying to save the cpSpace? Any particular reason?
It's not that trying to pool objects in that way is necessarily wrong, but you are much more likely to make bugs or memory leaks doing that. The only advantage of doing that is that it might save maybe a few milliseconds of CPU time. I don't mean tens or hundreds of milliseconds even, just milliseconds.