Page 1 of 1

Sensors and default collision handlers

Posted: Fri Oct 15, 2010 7:41 am
by gwynne
I'm quite new to Chipmunk, so I apologize if this is a foolish question, but today I ran into what to me seems an odd design decision in the engine.

I set up a sensor in the usual fashion, and added it to a space's static body. I also added a couple of normal bodies and shapes to float around. I set some default collision callbacks, then ran my program. The sensors never registered a collision.

Some investigation and confusion later, I found a line inside the implementation of cpSpace which jumps out of collision handling if either object involved is a sensor and the collision handler is the default for the space.

I eventually realized that my design for the specific situation was flawed and that I could make the sensors work with a specific collision type, but I couldn't make sense of why the code does this. It seems to make it impossible to use a sensor that detects collisions with all shapes in the space, unless I've misunderstood that there seems to be no wildcard value for collision types. Can someone explain to me why this is?

The offending code is in cpSpaceStep.c, line 161 (as of version 5.3.2, though I looked in SVN and it appears to go all the way back to the original implementation of sensors).

Re: Sensors and default collision handlers

Posted: Fri Oct 15, 2010 8:37 am
by slembcke
I guess I didn't think it through very well. Default collision handlers are a pretty rarely used feature from what I can tell. I put them in because I knew that some people would want/need more flexibility than the basic collision handlers provide. When I implemented it, I didn't want sensors wasting CPU time to perform the precise collision checks if they were just going to be ignored by the default (the default default) collision handlers which will ignore the results anyway. It didn't occur to me at the time that I was also preventing the user from handling sensors in a more generic fashion.

With a couple minor changes, I can change it so that it only ignores sensor collisions when the default collision handler set by Chipmunk is used.

Re: Sensors and default collision handlers

Posted: Fri Oct 15, 2010 8:45 am
by slembcke
Made a quick patch. Tested it only lightly though. I think it should make both of us happy.

Re: Sensors and default collision handlers

Posted: Fri Oct 15, 2010 9:46 am
by gwynne
As it turned out, you were right to find default handlers rare; I worked around the issue by fixing a flaw in my original design, after all :). That being said, your patch looks good to me, and solved my test case very neatly. Thanks very much!

Re: Sensors and default collision handlers

Posted: Fri Oct 15, 2010 3:47 pm
by slembcke
Heh, no problem I guess. I'll commit the patch changes to trunk as soon as I do more testing.