MCPcopy Create free account
hub / github.com/axmolengine/axmol / update

Function update

tests/cpp-tests/Source/ChipmunkTestBed/demo/Convex.cpp:31–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

29static cpShape* shape;
30
31static 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
71static cpSpace* init(void)
72{

Callers

nothing calls this directly

Calls 15

cpShapePointQueryFunction · 0.85
cpShapeGetBodyFunction · 0.85
cpPolyShapeGetCountFunction · 0.85
cpPolyShapeGetVertFunction · 0.85
cpBodyWorldToLocalFunction · 0.85
cpConvexHullFunction · 0.85
cpCentroidForPolyFunction · 0.85
cpAreaForPolyFunction · 0.85
cpBodySetMassFunction · 0.85
cpBodySetMomentFunction · 0.85
cpMomentForPolyFunction · 0.85
cpvnegFunction · 0.85

Tested by

no test coverage detected