This is the code
Code: Select all
space_ = cpSpaceNew();
space_->gravity = cpv(0,9.8);
//create 1st shape (rouge)
create_shape_box(0, 0, 100, 100, true);
//create the box that is affected by physics
create_shape_box(0, 400, 100, 100, false);
//function to create shapes
cpShape* game::create_shape_box(float _x, float _y, float _w, float _h, bool _is_static /* = true*/)
{
//return create_circle(_x, _y, _w);
int first = 0;
cpVect vec[4];
vec[0].x = -_w/2.0f;
vec[0].y = -_h/2.0f;
vec[1].x = -_w/2.0f;
vec[1].y = _h/2.0f;
vec[2].x = _w/2.0f;
vec[2].y = _h/2.0f;
vec[3].x = _w/2.0f;
vec[3].y = -_h/2.0f;
// Create space to store the convex hull.
// An alloca(), or a variable length array would be a better, but not always portable choice.
cpVect *hullVerts = (cpVect *)calloc(4, sizeof(cpVect));
int hullCount = cpConvexHull(4, vec, hullVerts, &first, 0.0);
// hullVerts[0] will be equal to verts[first] here.
// If you don't care, pass NULL instead of the 'first' pointer.
cpBody *body;
cpShape *shape;
if (_is_static == false)
{
body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, hullCount, hullVerts, cpvzero));
body = cpSpaceAddBody(space_, body);
shape = cpPolyShapeNew(body, hullCount, hullVerts, cpvzero);
shape = cpSpaceAddShape(space_, shape);
}
else
{
body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, hullCount, hullVerts, cpvzero));
//body = cpBodyNewStatic();
//shape = cpPolyShapeNew(body, hullCount, hullVerts, cpvzero);
shape = cpBoxShapeNew(body, _w, _h);
//body = cpSpaceAddBody(space_, body);
cpBodySetPos(body, cpv(_x, _y));
shape = cpSpaceAddShape(space_, shape);
}
cpBodySetPos(body, cpv(_x, _y));
cpShapeSetFriction(shape, 0.0f);
free(hullVerts);
return shape;
}