Iterating through shapes

Official forum for the Chipmunk2D Physics Library.
Post Reply
divisator
Posts: 6
Joined: Tue Jun 16, 2009 11:49 am
Contact:

Iterating through shapes

Post by divisator »

Hey everybody,

I've got a method that checks a given point for a specific cpBody by iterating through all bodies of the space and comparing the positions. Unfortunately I found out that I have to call cpSpaceRemoveShape and cpShapeFree to get rid of the Sprite and the assigned shape.

Could anyone tell me how to iterate through the shapes of a space (space->activeShapes I guess) so I could compare it's body property to the given body at the specific position to find out which space belongs to that body?

Thank you very much.
Have a nice day.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Iterating through shapes

Post by slembcke »

Code: Select all

cpSpaceHashEach(cpSpaceHash *hash, cpSpaceHashIterator func, void *data);
That function will call func() with each cpShape it contains. A faster and more robust solution would be to use the data pointer on cpBody to point to your own game object/struct. Keep a list of the attached shapes there.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
divisator
Posts: 6
Joined: Tue Jun 16, 2009 11:49 am
Contact:

Re: Iterating through shapes

Post by divisator »

Hey - thank you very much for your fast answer. I just tried it the way you suggested and it works - I get the shape that I want to remove ... unfortunately it crashes (BAD_ACCESS @ cpSpaceHashRemove) if I want to remove the shape...

I found this way to do it somewhere on the net:

Code: Select all

			
cpShape *b = body->data;
cpSpaceRemoveBody(space, body); 
cpBodyFree(body);
			
cpSpaceRemoveShape(space, b); 
cpShapeFree(b);
Do you have any idea, why I just can't call cpSpaceRemoveShape anyway?

Thank you very much!
divisator
Posts: 6
Joined: Tue Jun 16, 2009 11:49 am
Contact:

Re: Iterating through shapes

Post by divisator »

Hmm ... I just tried it without the cpSpaceRemoveShape - only the cpShapeFree - that seems to work ... but I have no idea what it does :-D
divisator
Posts: 6
Joined: Tue Jun 16, 2009 11:49 am
Contact:

Re: Iterating through shapes

Post by divisator »

oh... oh oh ... :D ... forget it ... it just didn't crash directly ... ehm ... ouch ... any further suggestions how I could remove this? :-D
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Iterating through shapes

Post by slembcke »

You can't remove or free items from the same list while you are iterating it. This is a no-no with most any data structure in any language. You'll have to make a list of the objects you want to remove and remove them separately.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
divisator
Posts: 6
Joined: Tue Jun 16, 2009 11:49 am
Contact:

Re: Iterating through shapes

Post by divisator »

I'm sorry - you're great ;-D ... it works (even though it's in a loop ;-) ...

Everything works fine - I just got two methods of creating shapes - and made that

body->data = shape

change only in the wrong method :-D ... I'm an idiot :-D ... but now it works great - I'm so happy :-D thx
divisator
Posts: 6
Joined: Tue Jun 16, 2009 11:49 am
Contact:

Re: Iterating through shapes

Post by divisator »

Oh ... correction ;-) ... for anyone who tries that ;-D ... it's not in a loop ... after that ->data change I don't need the loop anymore ;-)
Rawlyn
Posts: 33
Joined: Mon Mar 30, 2009 3:06 am
Contact:

Re: Iterating through shapes

Post by Rawlyn »

slembcke, you should give Chipmunk a slogan:
"Don't try and delete pointers that you're still iterating through, and maintain your own goddamn lists!"
Or, at least, whatever that would be in Latin ;)

You have the patience of a saint my friend! :)
[url=http://www.xboxlc.com/profile/Rawlyn][img]http://www.xboxlc.com/cards/sig/black/Rawlyn.jpg[/img][/url]
Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests