Debian Codegraph @

demo/Convex.c

raw symbols SCIP
1 /* Copyright (c) 2007 Scott Lembcke
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 * SOFTWARE.
20 */
21
22 #include "chipmunk/chipmunk.h"
23 #include "chipmunk/chipmunk_unsafe.h"
24
25 #include "ChipmunkDemo.h"
26
27 #define DENSITY (1.0/10000.0)
28
29 static cpShape *shape;
30
31 static void
32 update(cpSpace *space, double dt)
33 {
34 cpFloat tolerance = 2.0;
35
36 if(ChipmunkDemoRightClick && cpShapePointQuery(shape, ChipmunkDemoMouse, NULL) > tolerance){
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 verts[i] = cpPolyShapeGetVert(shape, i);
45 }
46
47 verts[count] = cpBodyWorldToLocal(body, ChipmunkDemoMouse);
48
49 // This function builds a convex hull for the vertexes.
50 // Because the result array is the same as verts, it will reduce it in place.
51 int hullCount = cpConvexHull(count + 1, verts, verts, NULL, tolerance);
52
53 // Figure out how much to shift the body by.
54 cpVect centroid = cpCentroidForPoly(hullCount, verts);
55
56 // Recalculate the body properties to match the updated shape.
57 cpFloat mass = cpAreaForPoly(hullCount, verts, 0.0f)*DENSITY;
58 cpBodySetMass(body, mass);
59 cpBodySetMoment(body, cpMomentForPoly(mass, hullCount, verts, cpvneg(centroid), 0.0f));
60 cpBodySetPosition(body, cpBodyLocalToWorld(body, centroid));
61
62 // Use the setter function from chipmunk_unsafe.h.
63 // You could also remove and recreate the shape if you wanted.
64 cpPolyShapeSetVerts(shape, hullCount, verts, cpTransformTranslate(cpvneg(centroid)));
65 }
66
67 cpSpaceStep(space, dt);
68 }
69
70 static cpSpace *
71 init(void)
72 {
73 ChipmunkDemoMessageString = "Right click and drag to change the blocks's shape.";
74
75 cpSpace *space = cpSpaceNew();
76 cpSpaceSetIterations(space, 30);
77 cpSpaceSetGravity(space, cpv(0, -500));
78 cpSpaceSetSleepTimeThreshold(space, 0.5f);
79 cpSpaceSetCollisionSlop(space, 0.5f);
80
81 cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
82
83 // Create segments around the edge of the screen.
84 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
85 cpShapeSetElasticity(shape, 1.0f);
86 cpShapeSetFriction(shape, 1.0f);
87 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
88
89 cpFloat width = 50.0f;
90 cpFloat height = 70.0f;
91 cpFloat mass = width*height*DENSITY;
92 cpFloat moment = cpMomentForBox(mass, width, height);
93
94 body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
95
96 shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));
97 cpShapeSetFriction(shape, 0.6f);
98
99 return space;
100 }
101
102 static void
103 destroy(cpSpace *space)
104 {
105 ChipmunkDemoFreeSpaceChildren(space);
106 cpSpaceFree(space);
107 }
108
109 ChipmunkDemo Convex = {
110 "Convex.",
111 1.0/60.0,
112 init,
113 update,
114 ChipmunkDemoDefaultDrawImpl,
115 destroy,
116 };