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.
Remove all bodies
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Remove all bodies
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
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 7
- Joined: Thu Jul 19, 2012 9:22 am
- Contact:
Re: Remove all bodies
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...
-
- Posts: 32
- Joined: Mon Jan 21, 2013 4:57 am
- Contact:
Re: Remove all bodies
Nope. You should be able to call them.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...
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Remove all bodies
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.
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 7
- Joined: Thu Jul 19, 2012 9:22 am
- Contact:
Re: Remove all bodies
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.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.
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Remove all bodies
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.
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 7
- Joined: Thu Jul 19, 2012 9:22 am
- Contact:
Re: Remove all bodies
Yeap, was hoping to save some space. :/ Cleaning everything up. But if yes say no point, I guess you know much better than meslembcke 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.
Who is online
Users browsing this forum: No registered users and 22 guests