Page 1 of 1

Collision is not detected !!!!

Posted: Fri Jan 23, 2009 12:56 am
by uhotspot
Hi,
In the following piece of code the polygonal object passes the bottom stationary line !!! and chipmunk does not detect the collision.
Surprisingly if we change the second dimension of the last item in vertices array to 7 then it works fine !!!!
cpVect vt[4] = {cpv(-19,5), cpv(-15,-3), cpv(10, -5), cpv(10, 5)}; ==> cpVect vt[4] = {cpv(-19,5), cpv(-15,-3), cpv(10, -5), cpv(10, 7)};

I would appreciate if someone can tell me what is wrong with my code.

Thank you

uhs

Code: Select all

staticBody = cpBodyNew(INFINITY, INFINITY);

cpResetShapeIdCounter();

space = cpSpaceNew();
cpSpaceResizeStaticHash(space, 100.0, 999);
cpSpaceResizeActiveHash(space, 100.0, 999);
space->gravity = cpv(0, -100);

cpBody *body;
cpShape *shape;

shape = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f);
shape->e = 1.0; shape->u = 1.0;
cpSpaceAddStaticShape(space, shape);

shape = cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f);
shape->e = 1.0; shape->u = 1.0;
cpSpaceAddStaticShape(space, shape);

shape = cpSegmentShapeNew(staticBody,cpv(-320,-240), cpv(320,-240), 0.0f);
shape->e = 1.0; shape->u = 1.0;
cpSpaceAddStaticShape(space, shape);

cpVect vt[4] = {cpv(-19,5), cpv(-15,-3), cpv(10, -5), cpv(10, 5)}; //if we change the last 5 to 7 then it works fine !!!
body = cpBodyNew(60, cpMomentForPoly(60, 4, vt, cpvzero));
body->p = cpv(140.0,40.8);
cpSpaceAddBody(space, body);
shape = cpPolyShapeNew(body, 4, vt, cpvzero);
cpSpaceAddShape(space, shape);


Re: Collision is not detected !!!!

Posted: Fri Jan 23, 2009 1:12 pm
by slembcke
Without actually plotting it out to check, I'd assume that you have a concave polygon. If you want to do that, you'll need to split it into multiple convex polygons.

(Also, please refrain from double posting.)

Re: Collision is not detected !!!!

Posted: Fri Jan 23, 2009 3:54 pm
by uhotspot
No actually it is a convex polygon !!
And sorry about the double posting. I though I have posted the first one in a wrong section.

Thanks.

Re: Collision is not detected !!!!

Posted: Wed Jan 28, 2009 1:08 pm
by uhotspot
I think I found the answer myself. If you define the verts in a counter clock wise order starting from the third quarter.
I mean you have to define the points in the following order:

(-a, -b), (-c, d), (e, f), (g, -h)

and then it works. At least it did for mine !

Re: Collision is not detected !!!!

Posted: Wed Jan 28, 2009 1:15 pm
by uhotspot
Sorry let me correct myself. The ponts have to be define in a clock wise order and it is not important which vert you start at.