| 29 | static cpShape* shape; |
| 30 | |
| 31 | static void update(cpSpace* space, double dt) |
| 32 | { |
| 33 | cpFloat tolerance = 2.0; |
| 34 | |
| 35 | if (ChipmunkDemoRightClick && cpShapePointQuery(shape, ChipmunkDemoMouse, NULL) > tolerance) |
| 36 | { |
| 37 | cpBody* body = cpShapeGetBody(shape); |
| 38 | int count = cpPolyShapeGetCount(shape); |
| 39 | |
| 40 | // Allocate the space for the new vertexes on the stack. |
| 41 | cpVect* verts = (cpVect*)alloca((count + 1) * sizeof(cpVect)); |
| 42 | |
| 43 | for (int i = 0; i < count; i++) |
| 44 | { |
| 45 | verts[i] = cpPolyShapeGetVert(shape, i); |
| 46 | } |
| 47 | |
| 48 | verts[count] = cpBodyWorldToLocal(body, ChipmunkDemoMouse); |
| 49 | |
| 50 | // This function builds a convex hull for the vertexes. |
| 51 | // Because the result array is the same as verts, it will reduce it in place. |
| 52 | int hullCount = cpConvexHull(count + 1, verts, verts, NULL, tolerance); |
| 53 | |
| 54 | // Figure out how much to shift the body by. |
| 55 | cpVect centroid = cpCentroidForPoly(hullCount, verts); |
| 56 | |
| 57 | // Recalculate the body properties to match the updated shape. |
| 58 | cpFloat mass = cpAreaForPoly(hullCount, verts, 0.0f) * DENSITY; |
| 59 | cpBodySetMass(body, mass); |
| 60 | cpBodySetMoment(body, cpMomentForPoly(mass, hullCount, verts, cpvneg(centroid), 0.0f)); |
| 61 | cpBodySetPosition(body, cpBodyLocalToWorld(body, centroid)); |
| 62 | |
| 63 | // Use the setter function from chipmunk_unsafe.h. |
| 64 | // You could also remove and recreate the shape if you wanted. |
| 65 | cpPolyShapeSetVerts(shape, hullCount, verts, cpTransformTranslate(cpvneg(centroid))); |
| 66 | } |
| 67 | |
| 68 | cpSpaceStep(space, dt); |
| 69 | } |
| 70 | |
| 71 | static cpSpace* init(void) |
| 72 | { |
nothing calls this directly
no test coverage detected