Skip to content

Instantly share code, notes, and snippets.

@slembcke
Last active December 16, 2015 14:20
Show Gist options
  • Save slembcke/5447954 to your computer and use it in GitHub Desktop.
Save slembcke/5447954 to your computer and use it in GitHub Desktop.
// Old slice code:
cpVect centroid = cpCentroidForPoly(clippedCount, clipped);
cpFloat mass = cpAreaForPoly(clippedCount, clipped)*DENSITY;
cpFloat moment = cpMomentForPoly(mass, clippedCount, clipped, cpvneg(centroid));
cpBody *new_body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
cpBodySetPos(new_body, centroid);
cpBodySetVel(new_body, cpBodyGetVelAtWorldPoint(body, centroid));
cpBodySetAngVel(new_body, cpBodyGetAngVel(body));
cpShape *new_shape = cpSpaceAddShape(space, cpPolyShapeNew(new_body, clippedCount, clipped, cpvneg(centroid)));
// Copy whatever properties you have set on the original shape that are important
cpShapeSetFriction(new_shape, cpShapeGetFriction(shape));
// New slice code:
cpBody *new_body = cpSpaceAddBody(space, cpBodyNew(0.0f, 0.0f));
cpBodySetPos(new_body, cpBodyGetPos(body));
cpBodySetAngle(new_body, cpBodyGetAngle(body));
cpShape *new_shape = cpSpaceAddShape(space, cpPolyShapeNew(new_body, clippedCount, clipped, cpvzero));
// Copy whatever properties you have set on the original shape that are important
cpShapeSetFriction(new_shape, cpShapeGetFriction(shape));
// Calculate the mass, moment of inertia, center of gravity, etc for the new body based on the shape.
cpBodyAddMassForShape(new_body, new_shape, DENSITY);
// Set the velocity info
cpVect new_cog = cpBodyLocal2World(new_body, cpBodyGetCOGOffset(new_body));
cpBodySetVel(new_body, cpBodyGetVelAtWorldPoint(body, new_cog));
cpBodySetAngVel(new_body, cpBodyGetAngVel(body));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment