Remove all bodies

Official forum for the Chipmunk2D Physics Library.
Post Reply
Setrino
Posts: 7
Joined: Thu Jul 19, 2012 9:22 am
Contact:

Remove all bodies

Post 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.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Remove all bodies

Post 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
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Setrino
Posts: 7
Joined: Thu Jul 19, 2012 9:22 am
Contact:

Re: Remove all bodies

Post 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...
dnldd
Posts: 32
Joined: Mon Jan 21, 2013 4:57 am
Contact:

Re: Remove all bodies

Post 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.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Remove all bodies

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Setrino
Posts: 7
Joined: Thu Jul 19, 2012 9:22 am
Contact:

Re: Remove all bodies

Post 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.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Remove all bodies

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Setrino
Posts: 7
Joined: Thu Jul 19, 2012 9:22 am
Contact:

Re: Remove all bodies

Post 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 :)
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests