Problems Removing Collision Callbacks

Official forum for the Chipmunk2D Physics Library.
Post Reply
Gillies
Posts: 6
Joined: Tue Apr 12, 2011 11:04 am
Contact:

Problems Removing Collision Callbacks

Post by Gillies »

Hi there

I am using Chipmunk in a Cocos2D iOS app.
All is running very well, great stuff.

I have, however, just started to try and use the physics system to do some of my initial level set-up.
I am defining 2D trigger volumes in my editor.
I would like these trigger volumes to collect all the items that may be activated within their volume on an initial physics step.
I would then like to remove this callback, and add a callback for the main-character striking the trigger volume.
When the trigger volume is stuck, all the items it touches will be activated.
My code is below:

Code: Select all

// Set up the physics callbacks - the level set-up
cpSpaceAddCollisionHandler( physicsSpace, CT_ACTIVATOR, CT_DYNAMIC, collectActivees, NULL, NULL, NULL, NULL );
cpSpaceAddCollisionHandler( physicsSpace, CT_ACTIVATOR, CT_DYNAMIC_DAMAGE, collectActivees, NULL, NULL, NULL, NULL );
		
// Run an initial physics step and then remove the set-up callbacks, add the ones that count
[self physicsStep:LEVEL_FRAME_UPDATE];
cpSpaceRemoveCollisionHandler( physicsSpace, CT_ACTIVATOR, CT_DYNAMIC );
cpSpaceRemoveCollisionHandler( physicsSpace, CT_ACTIVATOR, CT_DYNAMIC_DAMAGE );
cpSpaceAddCollisionHandler( physicsSpace, CT_PROTAGONIST, CT_ACTIVATOR, activateActivees, NULL, NULL, deactivateActivees, NULL );
Everything works as planned if I remove the two cpSpaceRemoveCollisionHandler calls (apart from it may be called again when items start moving).
When I leave the cpSpaceRemoveCollisionHandler calls in I either get mixed up callback function pointers or crashes in the next physics step.

Am I doing something wrong?
Is there a safe point to remove collision handlers?

I am using Chipmunk v5.3.4 (Debug Enabled).

Thank you for your help.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problems Removing Collision Callbacks

Post by slembcke »

I don't see anything wrong with the code you posted. There aren't any restrictions on when you can remove handlers. Not sure if it's bug in my code or yours then.

Could you be more specific about what you mean by "mixed up function pointers" and where the crashes are occurring? I've never heard of any problems with cpSpaceRemoveCollisionHandlers().

Lastly, there was an addition to Chipmunk a few months ago to provide shape queries, though I realized a couple weeks ago that it was never documented. You probably want to do this because it's a little simpler and doesn't require you to step the simulation to call the callbacks. If there is a bug I'd still like to fix it though.

Code: Select all

static void shapeQueryCallback(cpShape *shape, cpContactPointSet *points, void *data){
  // This shape is overlapping the trigger
}

// You can create a temporary shape, or just reuse your existing sensor shape even if you already added it to the space
cpSpaceShapeQuery(space, triggerShape, (cpSpaceShapeQueryFunc)shapeQueryCallback, data);

Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Gillies
Posts: 6
Joined: Tue Apr 12, 2011 11:04 am
Contact:

Re: Problems Removing Collision Callbacks

Post by Gillies »

Thanks for your reply.

My full code for this section is as below:

Code: Select all

// Set up the physics callbacks - the set-up
cpSpaceAddCollisionHandler( physicsSpace, CT_ACTIVATOR, CT_DYNAMIC, collectActivees, NULL, NULL, NULL, NULL );
cpSpaceAddCollisionHandler( physicsSpace, CT_ACTIVATOR, CT_DYNAMIC_DAMAGE, collectActivees, NULL, NULL, NULL, NULL );
		
// Run an initial physics step and then remove the set-up callbacks, add the ones that count
[self physicsStep:LEVEL_FRAME_UPDATE];
		
// Remove the set-up callbacks
cpSpaceRemoveCollisionHandler( physicsSpace, CT_ACTIVATOR, CT_DYNAMIC );
cpSpaceRemoveCollisionHandler( physicsSpace, CT_ACTIVATOR, CT_DYNAMIC_DAMAGE );
		
cpSpaceAddCollisionHandler( physicsSpace, CT_PROTAGONIST, CT_ACTIVATOR, activateActivees, NULL, NULL, deactivateActivees, NULL );
cpSpaceAddCollisionHandler( physicsSpace, CT_PROTAGONIST, CT_RECHARGE, NULL, NULL, NULL, collectRecharge, NULL );
cpSpaceAddCollisionHandler( physicsSpace, CT_PROTAGONIST, CT_STATIC_DAMAGE, NULL, NULL, NULL, strikeDamage, NULL );
cpSpaceAddCollisionHandler( physicsSpace, CT_PROTAGONIST, CT_DYNAMIC_DAMAGE, NULL, NULL, NULL, strikeDamage, NULL );
cpSpaceAddCollisionHandler( physicsSpace, CT_PROTAGONIST, CT_COMPLETE, levelCompleted, NULL, NULL, NULL, NULL );
cpSpaceAddCollisionHandler( physicsSpace, CT_PROTAGONIST, CT_FORCEFIELD, applyForceField, NULL, NULL, removeForceField, NULL );
If I run the code as above, the collectRecharge function, set for CT_PROTAGONIST and CT_RECHARGE gets called for the pairs of objects I have just removed collision handlers on. This crashes as I am casting to the objects I expect to be passed through.

If I move the cpSpaceRemoveCollisionHandler calls to after ALL the add calls, then I crash in the Chipmunk code...

Code: Select all

contactSetFilter(cpArbiter *arb, cpSpace *space)
...I get a EXC_BAD_ACCESS in the separate call here...

Code: Select all

// was used last frame, but not this one
if(ticks >= 1 && arb->state != cpArbiterStateCached){
	[b]arb->handler->separate(arb, space, arb->handler->data);[/b]
	arb->state = cpArbiterStateCached;
}
I guess it could be an issue with the arbiters?

Please let me know if you need any more info.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problems Removing Collision Callbacks

Post by slembcke »

Oooohhhh. Shoot. I had never thought of that. The problem is that the collision handler is deleted, but the arbiter keeps a dangling pointer to it.

People rarely remove collision handlers, so I guess I'm not terribly surprised that this is the first time it has come up. I guess I'll have to move to reference counting them or something. What a pain.

Well I guess I would definitely go with the shape query then.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Gillies
Posts: 6
Joined: Tue Apr 12, 2011 11:04 am
Contact:

Re: Problems Removing Collision Callbacks

Post by Gillies »

I think I will :)

Thank you very much for your help.
One last thing...
I assume the shape query returns all collisions, there is no way to restrict it to my existing collision-type pairs?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problems Removing Collision Callbacks

Post by slembcke »

No, you can easily filter them inside the callback though I suppose.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests