Collision Issues

Official forum for the Chipmunk2D Physics Library.
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Collision Issues

Post by Buschmaster »

Hey guys, not sure what my problem is here.

Working on my iPhone Game (Getting pretty close, too! :) ) and I have some collisions working beautifully but for some reason this one is leading to crashes.

Basically, it's a cpCircle colliding with a cpPolyShape and when they collide I'm just removing the cpPolyShape.

Here's the code for creating the poly shape and setting the collision up:

Code: Select all

		stopwatchToken = cpPolyShapeNew(shelvesBody, 4, stopwatchVects, cpvzero);
		stopwatchToken->collision_type = 3;
		cpSpaceAddCollisionPairFunc(space, 1, 3, &hitStopwatch, space);
And then when they collide here's the hitStopWatch:

Code: Select all

static int hitStopwatch(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
	cpShapeFree(stopwatchToken);
	cpSpaceRemoveShape(space, stopwatchToken);
	cpSpaceRemoveCollisionPairFunc(space, 1, 3);
	return 1;
}
If I make this the hitStopwatch I'm fine:

Code: Select all

static int hitStopwatch(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
	//cpShapeFree(stopwatchToken);
	//cpSpaceRemoveShape(space, stopwatchToken);
	cpSpaceRemoveCollisionPairFunc(space, 1, 3);
	return 1;
}
Seems weird that I'd have this crash, right? Hopefully someone here can offer some insight as to why this might be happening. Thanks. :)
Last edited by Buschmaster on Sun May 10, 2009 4:27 pm, edited 1 time in total.
coolman
Posts: 45
Joined: Fri Feb 27, 2009 7:59 am
Contact:

Re: Crashing on collisions

Post by coolman »

Hi,

Chipmunk doesn't fully support removing shapes /bodies inside a Collision Callback.

Read this thread:

http://www.slembcke.net/forums/viewtopic.php?f=1&t=445
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Crashing on collisions

Post by slembcke »

I only recently fixed it so that you can remove shapes from within collision callback, but it's still severely limited in it's usefulness as you still cannot free the shape until after the callback finishes.

Add the shape to a list or something that you iterate after the call to cpSpaceStep() has returned. It's annoying I know, I've had to deal with it as well.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Crashing on collisions

Post by Buschmaster »

Ah, of course! I actually remember reading that now.

Thanks. :)

(P.S. I'm glad I didn't screw things up too bad for once! ;) )
coolman
Posts: 45
Joined: Fri Feb 27, 2009 7:59 am
Contact:

Re: Crashing on collisions

Post by coolman »

slembcke wrote:Add the shape to a list or something that you iterate after the call to cpSpaceStep() has returned.
I'm doing in this way, adding to a C Array and works pretty well.
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Crashing on collisions

Post by Buschmaster »

coolman wrote:
slembcke wrote:Add the shape to a list or something that you iterate after the call to cpSpaceStep() has returned.
I'm doing in this way, adding to a C Array and works pretty well.
So do you pass it in as "data" and how would you do that? I have other ways I could figure out which shape it is and such, but it would be nice to just add it to a list and then when I get back to my drawer just say hey by the way get rid of this.
coolman
Posts: 45
Joined: Fri Feb 27, 2009 7:59 am
Contact:

Re: Crashing on collisions

Post by coolman »

Yes ,, it's a struc array like

typedef struct st_Sprites {
cpShape *Shape;
unsigned int sprite;
} st_Sprites;

st_SpritesBorrar misSpritesToDelete[MAX_SPRITES];
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Crashing on collisions

Post by Buschmaster »

coolman wrote:Yes ,, it's a struc array like

typedef struct st_Sprites {
cpShape *Shape;
unsigned int sprite;
} st_Sprites;

st_SpritesBorrar misSpritesToDelete[MAX_SPRITES];
Disregard my question, I was being very thick. Apparently you can get up too early and start programming. ;)
coolman
Posts: 45
Joined: Fri Feb 27, 2009 7:59 am
Contact:

Re: Crashing on collisions

Post by coolman »

;)

You are welcome
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Crashing on collisions

Post by Buschmaster »

coolman wrote:;)

You are welcome
I either need more or less Mountain Dew. It would also help if I didn't have 30 ideas to implement all at once. ;)

Anyway, thanks for all the help both of you.
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests