chipmunk sensor question

Sage
Posts: 1,199
Joined: 2004.10
Post: #1
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?
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #3
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...
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
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.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #5
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!

EDIT:

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

Except...

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

Here's the trace:
Code:
#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.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #6
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

FOLLOWUP:

It's crashing in shapeQueryHelper:
Code:
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.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #7
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!
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #8
Ah, I was just about to ask that.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Motion Sensor Query for 3D game iphone-query 1 4,336 Sep 2, 2011 09:26 PM
Last Post: PoseMotion
  Adding lots of static shapes in Chipmunk - performance question TomorrowPlusX 3 7,008 Jul 11, 2011 01:39 PM
Last Post: Skorche