Page 1 of 1

how to remove a collision handler in v7

Posted: Wed Feb 17, 2016 8:28 pm
by riq
Hi,

How should I remove a collision handler in v7.0?

In v6.2 I was using `cpSpaceRemoveCollisionHandler` but I can't find any similar API in v7.0.
Thanks!

Re: how to remove a collision handler in v7

Posted: Thu Feb 18, 2016 10:15 am
by slembcke
Allowing collision handlers to be removed in older versions was actually sort of a disaster. When a collision began, Chipmunk looked up the handler for it and stored it on the collision pair object. When you deleted the handler, it didn't check all active collisions and remove the handler from any that were using it. This is mostly what people wanted, since begin/events were expected to be matched. What about the user data parameter though? If you freed the memory associated with that when you deleted the handler, now you have a dangling pointer. There were other issues as well, and no obviously correct way to handle them. The only case where it wasn't "broken by design" is when you didn't use the user data pointer, and you wanted separate calls to happen even after you removed the handler.

The change in v7 was to expose the collision handler struct directly. When you call cpSpaceAddCollisionHandler(), it creates or returns the existing handler, and you can change the handler functions at any time. This gives you much more control than the previous behavior, but matching the old behavior exactly would be somewhat difficult.

Re: how to remove a collision handler in v7

Posted: Sat Feb 20, 2016 1:05 am
by riq
Ok. thanks. makes sense. I'll just replace the old handler with the "do nothing handler".