Page 1 of 1
Iterating through shapes
Posted: Tue Jun 16, 2009 11:53 am
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.
Re: Iterating through shapes
Posted: Tue Jun 16, 2009 11:41 pm
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.
Re: Iterating through shapes
Posted: Fri Jun 19, 2009 11:20 am
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!
Re: Iterating through shapes
Posted: Fri Jun 19, 2009 11:22 am
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

Re: Iterating through shapes
Posted: Fri Jun 19, 2009 11:26 am
by divisator
oh... oh oh ...

... forget it ... it just didn't crash directly ... ehm ... ouch ... any further suggestions how I could remove this?

Re: Iterating through shapes
Posted: Fri Jun 19, 2009 11:42 am
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.
Re: Iterating through shapes
Posted: Sat Jun 20, 2009 11:20 am
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

... I'm an idiot

... but now it works great - I'm so happy

thx
Re: Iterating through shapes
Posted: Sat Jun 20, 2009 11:22 am
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

Re: Iterating through shapes
Posted: Mon Jun 22, 2009 9:01 am
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!
