chipmunk sensor question - Printable Version
+- iDevGames Forums (http://www.idevgames.com/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
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!
Now, when I remove the sensors using cpSpaceRemoveStaticShape, I get a crash. When I create the sensors I use cpSpaceAddStaticShape.
Here's the trace:
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
It's crashing in shapeQueryHelper:
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.
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.