Skip to content

Instantly share code, notes, and snippets.

@slembcke
Forked from andykorth/cpConvexMoment.c
Created May 17, 2012 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slembcke/2719765 to your computer and use it in GitHub Desktop.
Save slembcke/2719765 to your computer and use it in GitHub Desktop.
Convex Moment
// This is a little annoying:
cpVect *hullVerts = (cpVect *)calloc(NUM_VERTS, sizeof(cpVect));
int hullCount = cpConvexHull(NUM_VERTS, verts, hullVerts, NULL, 0.0);
cpFloat mass = 1.0;
cpFloat moment = cpMomentForPoly(mass, hullCount, hullVerts, cpvzero);
body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
shape = cpSpaceAddShape(space, cpPolyShapeNew(body, hullCount, hullVerts, cpvzero));
free(hullVerts);
// This isn't so bad:
CP_MAKE_CONVEX(NUM_VERTS, verts, hullCount, hullVerts);
cpFloat mass = 1.0;
cpFloat moment = cpMomentForPoly(mass, hullCount, hullVerts, cpvzero);
body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
shape = cpSpaceAddShape(space, cpPolyShapeNew(body, hullCount, hullVerts, cpvzero));
// Using the following macro:
#define CP_MAKE_CONVEX(__count__, __verts__, __count_var__, __verts_var__) \
cpVect *__verts_var__ = (cpVect *)alloca(__count__*sizeof(cpVect)); \
int __count_var__ = cpConvexHull(__count__, __verts__, __verts_var__, NULL, 0.0); \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment