polygon shapes vertices order

Official forum for the Chipmunk2D Physics Library.
Post Reply
sashonk
Posts: 7
Joined: Sun Aug 07, 2011 11:17 am
Contact:

polygon shapes vertices order

Post by sashonk »

hello,
as mentioned in documentation, a polygon must be created with
vertices in clockwize order. But when i do this way, program fails with runtime error in 'cpPolyShapeNew' func.
It only works when pass vertices in counterclockwize order. i.e this works ok:

Code: Select all

    cpBodyStand = cpBodyNewStatic();
    cpVect* standVerts = new cpVect[4];
    standVerts[3].x = 50; standVerts[3].y = 50;
    standVerts[2].x = 100; standVerts[2].y = 50;
    standVerts[1].x = 100; standVerts[1].y = 100;
    standVerts[0].x = 50; standVerts[0].y = 100;

    cpShapeStand = cpPolyShapeNew(cpBodyStand, 4, standVerts, cpvzero);
but this fails

Code: Select all

    cpBodyStand = cpBodyNewStatic();
    cpVect* standVerts = new cpVect[4];
    standVerts[0].x = 50; standVerts[0].y = 50;
    standVerts[1].x = 100; standVerts[1].y = 50;
    standVerts[2].x = 100; standVerts[2].y = 100;
    standVerts[3].x = 50; standVerts[3].y = 100;

    cpShapeStand = cpPolyShapeNew(cpBodyStand, 4, standVerts, cpvzero);
am i wrong about this or it is a bug?
gnasen
Posts: 24
Joined: Wed Mar 16, 2011 12:17 pm
Contact:

Re: polygon shapes vertices order

Post by gnasen »

just a suggestion:
Its pretty easy to check if the given points are clockwise or anticlockwise (if you are interested, just ask, but I guess you know it). So why doesnt chipmunk check it and flips the order automatically if its not the same as normally wanted?
sashonk
Posts: 7
Joined: Sun Aug 07, 2011 11:17 am
Contact:

Re: polygon shapes vertices order

Post by sashonk »

if it is just chipmunk's flipping the vertices order then it is better to be mentioned in docs, because i've spent so much time trying to figure out why my poly fails :)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: polygon shapes vertices order

Post by slembcke »

It has to be clockwise AND convex. You can check just the winding with cpAreaForPoly(). It returns a signed area, so if it's positive then it's clockwise. cpPolyValidateVerts() checks for both convexity and winding.

Chipmunk doesn't reverse them for you because it only validates input like that in debug mode.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
sashonk
Posts: 7
Joined: Sun Aug 07, 2011 11:17 am
Contact:

Re: polygon shapes vertices order

Post by sashonk »

check out my second code post. it is clockwize winding and convex, but it doesn't work
sashonk
Posts: 7
Joined: Sun Aug 07, 2011 11:17 am
Contact:

Re: polygon shapes vertices order

Post by sashonk »

by the way, could you, please, inspect my code: i created 2 polygons, one of them falls on the other one - static. They start collide, but then behave glitchy and stupid. here is code:

Code: Select all

      cpInitChipmunk();
    //create space, apply gravity
    space = cpSpaceNew();
    cpSpaceSetGravity(space, cpv(0, 50));


  /////// FALLING POLY ///////

    cpVect* fallVerts = new cpVect[verts.size()];
    for(int i = 0; i<verts.size();i ++){
        fallVerts[verts.size()-i-1].x = verts[i]->pos.x;
        fallVerts[verts.size()-i-1].y = verts[i]->pos.y;
    }

    cpBodyFall = cpBodyNew(1, 1);
    cpVect centroid =  cpCentroidForPoly(verts.size(), fallVerts);
    for(int i = 0; i<verts.size();i ++){
      fallVerts[i].x = fallVerts[i].x - centroid.x;
      fallVerts[i].y = fallVerts[i].y - centroid.y;
    } 


    cpShapeFall = cpPolyShapeNew(cpBodyFall, verts.size(), fallVerts, cpvzero);
    cpBodySetPos(cpBodyFall, centroid);
    cpShapeSetElasticity(cpShapeFall, .4);
    cpShapeSetFriction(cpShapeFall, .6);


    //////////////////////////////////////////////////////////////////////////
    /////////////////////////  GROUND  POLY/////////////////////////////

    cpVect* grVerts = new cpVect[grounds.size()];
    for(int i = 0; i<grounds.size();i ++){
      grVerts[grounds.size()-i-1].x = grounds[i]->pos.x;
      grVerts[grounds.size()-i-1].y = grounds[i]->pos.y;
    }

    cpVect centr = cpCentroidForPoly(grounds.size(), grVerts);
    for(int i = 0; i<grounds.size(); i++){
      grVerts[i].x = grVerts[i].x - centr.x;
      grVerts[i].y = grVerts[i].y - centr.y;
    }

    cpBodyStand = cpBodyNewStatic();
    cpShapeStand = cpPolyShapeNew(cpBodyStand, grounds.size(), grVerts, cpvzero);
    cpBodySetPos(cpBodyStand, centr);
    cpShapeSetElasticity(cpShapeStand, .4);
        cpShapeSetFriction(cpShapeStand, .6);
  cpSpaceReindexStatic(space) ;
  

//////////////////////////////////////////////////////////////////////////

    cpSpaceAddBody(space, cpBodyFall);
    cpSpaceAddBody(space, cpBodyStand);
    cpSpaceAddShape(space, cpShapeFall);
    cpSpaceAddShape(space, cpShapeStand);
 
    cpVect cpHS;
    cpBB bb = cpShapeGetBB(cpShapeFall);
    cpHS.x = centroid.x - bb.l;
    cpHS.y = centroid.y - bb.b;
    hgeFall->SetHotSpot(hgeVector(cpHS.x , cpHS.y));

    bb = cpShapeGetBB(cpShapeStand);
    cpHS.x = centr.x - bb.l;
    cpHS.y = centr.y - bb.b;
    hgeGround->SetHotSpot(hgeVector(cpHS.x, cpHS.y));

        
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests