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));