Collision detection advice

Discuss new features and future development.

Collision detection advice

Postby kandkstuart » Thu Jan 19, 2012 10:04 pm

I need some advice on collision detection and how objects should be added for the best performance possible. I've got about 200 bodies that have shapes attached to them. The cpSpaceStep routine is running really slowy when it's finding colliding pairs. It can take up to 8 seconds to process. What would your suggestion be for setting up the objects, should I just add bodies with no shapes for static objects, etc? Any suggestions would be welcomed. Thanks in advance.
kandkstuart
 
Posts: 29
Joined: Thu Jan 12, 2012 10:08 pm

Re: Collision detection advice

Postby slembcke » Thu Jan 19, 2012 11:17 pm

Say what? :shock:

You should be able to 400 or so shapes in a simulation and still run just fine at 60 fps on an iPhone 4. Something is terribly terribly wrong if it's running at 1/1000th the normal speed.

How many shapes do you have? That is more important than the number of bodies. Are your forcing all of the shapes to overlap the same place?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Collision detection advice

Postby kandkstuart » Fri Jan 20, 2012 7:18 am

Ok, so your comments got me thinking, "How am I screwing this up?". I use your query shape function to see what is overlapping and I've got a lot of shapes overlapping. That explains the sluggish performance. So I will address that and go from there. Thanks for adding tools for debugging!!!! I should have looked at that before I bothered you :-) Thanks for the response though!
kandkstuart
 
Posts: 29
Joined: Thu Jan 12, 2012 10:08 pm

Re: Collision detection advice

Postby kandkstuart » Fri Jan 20, 2012 11:20 am

Is there a way to tell chipmunk to ignore overlapping static bodies attached to shapes? Could I do it by putting all static objects in the same group? It seems your doc says that will work. Could you just confirm that? I'll give it a try also.
kandkstuart
 
Posts: 29
Joined: Thu Jan 12, 2012 10:08 pm

Re: Collision detection advice

Postby kandkstuart » Fri Jan 20, 2012 11:42 am

Grouping works... Still a little performance hit somewhere but I'll debug it. Thanks for all the help Scott!
kandkstuart
 
Posts: 29
Joined: Thu Jan 12, 2012 10:08 pm

Re: Collision detection advice

Postby slembcke » Fri Jan 20, 2012 12:26 pm

Static shapes are shapes attached to a static body. (either cpSpace.staticSpace or created by cpBodyNewStatic()). Static shapes don't check collisions against other static shapes, and they don't get updated every frame.

If you are attaching shapes to a body created any other way, it will end up being a dynamic shape. Dynamic shapes check collisions against all other shapes and are updated every frame.

If you are accidentally attaching your static geometry to non-static bodies and making them dynamic shapes, that could make the performance be as bad as you were saying it was.

Setting groups for the shapes allows it to skip doing a primitive (ex: circle-poly check, poly-poly check, etc) but it doesn't allow the spatial index data structure that figures out which shapes are near each other to skip them.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Collision detection advice

Postby kandkstuart » Fri Jan 20, 2012 2:42 pm

hmmm. All my static shapes are attached to static bodies created by cpBodyNewStatic but when I query for overlaps I see overlaps for those static shapes with other static shapes. Not sure what I'm missing here but that must be were my performance hit is... Anything I can check?

I'm using cpPolyShapeNew() to create my shape... Is there a problem with that?
kandkstuart
 
Posts: 29
Joined: Thu Jan 12, 2012 10:08 pm

Re: Collision detection advice

Postby kandkstuart » Fri Jan 20, 2012 3:08 pm

As an example this is what confuses me. This dump out shows static shapes overlapping static shapes:

ShapeQueryFunc() 0x1e454260 static collision_type=7
ShapeQueryFunc() type=7 overlaps at (428, 270) (0, 0) -28.39
ShapeQueryFunc() 0x1d5facb0 static collision_type=4
ShapeQueryFunc() type=4 overlaps at (415, 255) (0, 0) -6.36
ShapeQueryFunc() 0x1d5f2650 static collision_type=4
ShapeQueryFunc() type=4 overlaps at (544, 384) (0, 0) -6.73
ShapeQueryFunc() 0x1e454260 static collision_type=7
ShapeQueryFunc() type=7 overlaps at (529, 371) (0, 0) -30.50
ShapeQueryFunc() type=1 overlaps at (112, 544) (0, -1) -1.00
ShapeQueryFunc() 0x1e454260 static collision_type=7
ShapeQueryFunc() type=7 overlaps at (443, 390) (0, 0) -14.88
ShapeQueryFunc() 0x1e454260 static collision_type=7
ShapeQueryFunc() type=7 overlaps at (478, 395) (0, 0) -22.99
ShapeQueryFunc() 0x1e4dfd50 static collision_type=13
ShapeQueryFunc() type=13 overlaps at (544, 384) (0, 0) -6.73
ShapeQueryFunc() 0x1e454260 static collision_type=7
ShapeQueryFunc() type=7 overlaps at (408, 355) (0, 0) -13.99
ShapeQueryFunc() 0x1e454260 static collision_type=7
ShapeQueryFunc() type=7 overlaps at (404, 321) (1, 0) -21.00
ShapeQueryFunc() 0x1e454260 static collision_type=7
ShapeQueryFunc() type=7 overlaps at (554, 318) (0, 0) -21.97
ShapeQueryFunc() 0x1e452620 static collision_type=10
ShapeQueryFunc() type=10 overlaps at (360, 256) (0, -1) 0.00
ShapeQueryFunc() type=10 overlaps at (369, 256) (0, -1) 0.00
ShapeQueryFunc() 0x1e454260 static collision_type=7
ShapeQueryFunc() type=7 overlaps at (549, 283) (0, 0) -13.53
ShapeQueryFunc() 0x1e452620 static collision_type=10
ShapeQueryFunc() type=10 overlaps at (383, 224) (1, 0) -23.00
ShapeQueryFunc() type=10 overlaps at (370, 225) (1, 0) -23.00
ShapeQueryFunc() type=10 overlaps at (365, 234) (1, 0) -23.00

Shortened the output for readability.
kandkstuart
 
Posts: 29
Joined: Thu Jan 12, 2012 10:08 pm

Re: Collision detection advice

Postby kandkstuart » Fri Jan 20, 2012 4:19 pm

Here is the routine that is creating the static objects. The staticObject flag is set to YES for all the static objects. Do you see anything wrong?

// Define our sprite object.
CCSprite *sprite = [CCSprite spriteWithFile:objSt.fileName];
if (sprite)
{
sprite.visible = visible;
[sprite setPosition:screenPos];
if (!addToTM)
[self addChild:sprite z:z];
else
[self.curTilemap addChild:sprite z:z];

cpBody *body = NULL;
if (objSt.staticObject)
{
body = cpBodyNewStatic();
body->p = screenPos;
}
else
{
body = cpBodyNew(objSt.mass, objSt.inertia);
body->p = screenPos;

// Add the body to the space
cpSpaceAddBody(self.space, body);
}

cpShape *shape = cpPolyShapeNew(body, objSt.numRows, (cpVect *)objSt.objVertex, objSt.offset);
cpSpaceAddShape(self.space, shape);
cpShapeSetElasticity(shape, objSt.elasticity);
cpShapeSetFriction(shape, objSt.friction);
cpShapeSetCollisionType(shape, objSt.collisionType);
//cpShapeSetGroup(shape, objSt.group);
cpShapeSetGroup(shape, CP_NO_GROUP);
cpShapeSetUserData(shape, sprite);
cpShapeSetLayers(shape, objSt.layers);

objSt.shape = shape;
objSt.sprite = sprite;
objSt.hObject = (int)sprite;
objSt.isAddedToTM = addToTM;

// Use the sprite pointer as our handle.
ShapeObject *obj = [[ShapeObject alloc] initWithCPShape: objSt];
[physObjects addObject: obj];
[obj release];

return (int)sprite;
kandkstuart
 
Posts: 29
Joined: Thu Jan 12, 2012 10:08 pm

Re: Collision detection advice

Postby slembcke » Fri Jan 20, 2012 4:39 pm

It's fine (and common) if static shapes overlap. They don't check collisions between themselves anyway. What I meant is that sometimes people write code that stacks inactive objects offscreen all at the same location as a quick and dirty way to hide the sprites. Like they set the position of everything to be (1000, 1000) or something like that. I've had a lot of people run into performance problems with Chipmunk because they were doing this. Instead of having a few dozen colliding objects each frame they had 100's of thousands.

How many dynamic bodies are you adding to the space and how many dynamic shapes do you have on each one?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Next

Return to Features/Development

Who is online

Users browsing this forum: No registered users and 2 guests

cron