iDevGames Forums
chipmunk sensor question - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: chipmunk sensor question (/thread-9079.html)

chipmunk sensor question - TomorrowPlusX - Jun 20, 2011 01:46 PM

Hey everybody,

I'm using chipmunk (5.3.4) and have a number of sensors ( cpShape with sensor set to true ). The sensors correctly "sense" dynamic physics shapes.

But they don't sense static shapes! I know it sounds odd to use sensors to detect static shapes, but I have procedurally generated static geometry and it needs to be detected by my sensors, too, for pathfinding reasons.

Any idea what's going on here?

RE: chipmunk sensor question - Skorche - Jun 20, 2011 02:56 PM

Static shapes don't collide with other static shapes to save on CPU time. So if your sensors are static, they don't detect collisions with other static shapes.

If you attach the sensors to a rogue body instead of a static one, they will be checked against static shapes each frame. If you have a lot of sensors, this could get very expensive though.

RE: chipmunk sensor question - TomorrowPlusX - Jun 20, 2011 07:53 PM

That makes sense.

Now, if I make my sensors use a single rogue body, would it be possible to do a single sweep, and then switch back to the global cpSpace::staticBody instance? Or will I have to destroy them all and re-create them again as statics?

The thing is, all I really need is the expensive check once, when the level is loaded. My static shapes don't ever move, but I do want to know where they are. I can switch to static sensors after the initial level load because once the level's loaded, the only moving bodies I care about will be dynamic.

Basically, I have a 2d grid of sensors I'm using for pathfinding...

RE: chipmunk sensor question - Skorche - Jun 20, 2011 09:31 PM

So I was about to suggest using cpSpaceShapeQuery(), but then I looked at the code and realized that there is a bug that will make it not work. It explicitly ignores queries that hit sensors and I have absolutely no idea why I would have added that.

* Skorche facepalm

I committed the fix to trunk. Run the queries as you add the sensors to the space. Queries don't do collision types though, just groups and layers.

RE: chipmunk sensor question - TomorrowPlusX - Jun 21, 2011 04:33 AM

Unfortunately, I can't get chipmunk from trunk to build. I'm going to dive in to figure out what you did to fix this bug and apply it to 5.3.4.

Thanks for the help though - your fix to cpSpaceShapeQuery is going to make things a LOT easier for me!


I found the change to cpSpaceQuery - changing (a-> sensor || b->sensor) to ( a == b ), and it makes sense.

The cpSpaceShapeQuery function now does work beautifully, I run a query against the static geometry of the level ( testing for the collision_type in my callback ) and it works wonderfully! Rasp


Now, when I remove the sensors using cpSpaceRemoveStaticShape, I get a crash. When I create the sensors I use cpSpaceAddStaticShape.

Here's the trace:
#0    0x00285bd1 in shapeQueryHelper at /Users/zakariya/Dropbox/Code/Projects/Surfacer/vendor/Chipmunk/Chipmunk-5.3.4/src/cpSpaceQuery.c:211
#1    0x00283daa in query ()
#2    0x00283f28 in cpSpaceHashQuery ()
#3    0x00285ac5 in cpSpaceShapeQuery ()
#4    0x00282f03 in cpSpaceActivateShapesTouchingShape ()
#5    0x00281785 in cpSpaceRemoveStaticShape ()
#6    0x002814ef in cpSpaceRemoveShape ()
#7    0x0001cfe2 in game::pathfinding::Pathfinder::~Pathfinder() ()
#8    0x0001cf6f in game::pathfinding::Pathfinder::~Pathfinder() ()
#9    0x0000f3cc in game::Level::~Level() ()
#10    0x0000f355 in game::Level::~Level() ()
#11    0x00018ea5 in MonsterTestScenario::shutdown() ()
#12    0x00002dd5 in SurfacerApp::shutdown() ()
#13    0x00021170 in cinder::app::App::privateShutdown__() ()

For what it's worth, my sensors are using the space's staticBody instance.

My guess is that there were more changes/ramifications to the fix to spSpaceShapeQuery than that single one I found via diff. It might be best if I got your newest Chipmunk build building.

RE: chipmunk sensor question - TomorrowPlusX - Jun 22, 2011 04:40 AM

So it was my dumb mistake that the trunk version of Chipmunk wouldn't build. I'm still kind of screwy with Xcode 4 ... it was using the headers for 5.3.4, and of course, that wasn't playing well with trunk's source files.

Anyway, I'm using trunk, and cpSpaceShapeQuery works correctly. But the crash bug persists. When removing the sensors I get the crash listed above. I'm going to investigate a bit further, see if I can't help you with more detailed information instead of just crying and whining Smile


It's crashing in shapeQueryHelper:
if(a->klass->type <= b->klass->type){ // <!-- HERE!
        numContacts = cpCollideShapes(a, b, contacts);
    } else {
        numContacts = cpCollideShapes(b, a, contacts);
        for(int i=0; i<numContacts; i++) contacts[i].n = cpvneg(contacts[i].n);

Looking at it in the debugger, it's clear that 'a' is my sensor, and that 'b' is garbage. I think b is some object from my scene that has been deleted before my code is run. I'm looking through my code to see if anything is deleting chipmunk objects without removing them from their space, but so far it all seems above board.

RE: chipmunk sensor question - TomorrowPlusX - Jun 22, 2011 05:27 AM

It was all my fault. Smile

One of my classes wasn't removing its shapes or bodies from the space before deleting them. Sorry for all the trouble, but thanks for the bugfix to cpSpaceShapeQuery!

RE: chipmunk sensor question - Skorche - Jun 22, 2011 06:40 AM

Ah, I was just about to ask that.