Page 1 of 1

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 11:58 am
by Setrino
slembcke wrote: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

Are these methods only available in the Pro Version? Cause I can't find them...

Re: Remove all bodies

Posted: Mon Jan 21, 2013 12:03 pm
by dnldd
Setrino wrote:
slembcke wrote: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

Are these methods only available in the Pro Version? Cause I can't find them...
Nope. You should be able to call them.

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 2:35 pm
by Setrino
slembcke wrote: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.
Yes I am doing it for memory management - basically in the game at each level I want to remove old shapes from the space, and then add new ones. Redefining functions - yes, I understand that.

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.

Re: Remove all bodies

Posted: Tue Jan 22, 2013 5:27 am
by Setrino
slembcke wrote: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.
Yeap, was hoping to save some space. :/ Cleaning everything up. But if yes say no point, I guess you know much better than me :)