Debian Codegraph @

demo/Slice.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
24 #include "ChipmunkDemo.h"
25
26 #define DENSITY (1.0/10000.0)
27
28 static void
29 ClipPoly(cpSpace *space, cpShape *shape, cpVect n, cpFloat dist)
30 {
31 cpBody *body = cpShapeGetBody(shape);
32
33 int count = cpPolyShapeGetCount(shape);
34 int clippedCount = 0;
35
36 cpVect *clipped = (cpVect *)alloca((count + 1)*sizeof(cpVect));
37
38 for(int i=0, j=count-1; i<count; j=i, i++){
39 cpVect a = cpBodyLocalToWorld(body, cpPolyShapeGetVert(shape, j));
40 cpFloat a_dist = cpvdot(a, n) - dist;
41
42 if(a_dist < 0.0){
43 clipped[clippedCount] = a;
44 clippedCount++;
45 }
46
47 cpVect b = cpBodyLocalToWorld(body, cpPolyShapeGetVert(shape, i));
48 cpFloat b_dist = cpvdot(b, n) - dist;
49
50 if(a_dist*b_dist < 0.0f){
51 cpFloat t = cpfabs(a_dist)/(cpfabs(a_dist) + cpfabs(b_dist));
52
53 clipped[clippedCount] = cpvlerp(a, b, t);
54 clippedCount++;
55 }
56 }
57
58 cpVect centroid = cpCentroidForPoly(clippedCount, clipped);
59 cpFloat mass = cpAreaForPoly(clippedCount, clipped, 0.0f)*DENSITY;
60 cpFloat moment = cpMomentForPoly(mass, clippedCount, clipped, cpvneg(centroid), 0.0f);
61
62 cpBody *new_body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
63 cpBodySetPosition(new_body, centroid);
64 cpBodySetVelocity(new_body, cpBodyGetVelocityAtWorldPoint(body, centroid));
65 cpBodySetAngularVelocity(new_body, cpBodyGetAngularVelocity(body));
66
67 cpTransform transform = cpTransformTranslate(cpvneg(centroid));
68 cpShape *new_shape = cpSpaceAddShape(space, cpPolyShapeNew(new_body, clippedCount, clipped, transform, 0.0));
69 // Copy whatever properties you have set on the original shape that are important
70 cpShapeSetFriction(new_shape, cpShapeGetFriction(shape));
71 }
72
73 // Context structs are annoying, use blocks or closures instead if your compiler supports them.
74 struct SliceContext {
75 cpVect a, b;
76 cpSpace *space;
77 };
78
79 static void
80 SliceShapePostStep(cpSpace *space, cpShape *shape, struct SliceContext *context)
81 {
82 cpVect a = context->a;
83 cpVect b = context->b;
84
85 // Clipping plane normal and distance.
86 cpVect n = cpvnormalize(cpvperp(cpvsub(b, a)));
87 cpFloat dist = cpvdot(a, n);
88
89 ClipPoly(space, shape, n, dist);
90 ClipPoly(space, shape, cpvneg(n), -dist);
91
92 cpBody *body = cpShapeGetBody(shape);
93 cpSpaceRemoveShape(space, shape);
94 cpSpaceRemoveBody(space, body);
95 cpShapeFree(shape);
96 cpBodyFree(body);
97 }
98
99 static void
100 SliceQuery(cpShape *shape, cpVect point, cpVect normal, cpFloat alpha, struct SliceContext *context)
101 {
102 cpVect a = context->a;
103 cpVect b = context->b;
104
105 // Check that the slice was complete by checking that the endpoints aren't in the sliced shape.
106 if(cpShapePointQuery(shape, a, NULL) > 0.0f && cpShapePointQuery(shape, b, NULL) > 0.0f){
107 // Can't modify the space during a query.
108 // Must make a post-step callback to do the actual slicing.
109 cpSpaceAddPostStepCallback(context->space, (cpPostStepFunc)SliceShapePostStep, shape, context);
110 }
111 }
112
113 static void
114 update(cpSpace *space, double dt)
115 {
116 cpSpaceStep(space, dt);
117
118 static cpBool lastClickState = cpFalse;
119 static cpVect sliceStart = {0.0, 0.0};
120
121 // Annoying state tracking code that you wouldn't need
122 // in a real event driven system.
123 if(ChipmunkDemoRightClick != lastClickState){
124 if(ChipmunkDemoRightClick){
125 // MouseDown
126 sliceStart = ChipmunkDemoMouse;
127 } else {
128 // MouseUp
129 struct SliceContext context = {sliceStart, ChipmunkDemoMouse, space};
130 cpSpaceSegmentQuery(space, sliceStart, ChipmunkDemoMouse, 0.0, GRAB_FILTER, (cpSpaceSegmentQueryFunc)SliceQuery, &context);
131 }
132
133 lastClickState = ChipmunkDemoRightClick;
134 }
135
136 if(ChipmunkDemoRightClick){
137 ChipmunkDebugDrawSegment(sliceStart, ChipmunkDemoMouse, RGBAColor(1, 0, 0, 1));
138 }
139 }
140
141 static cpSpace *
142 init(void)
143 {
144 ChipmunkDemoMessageString = "Right click and drag to slice up the block.";
145
146 cpSpace *space = cpSpaceNew();
147 cpSpaceSetIterations(space, 30);
148 cpSpaceSetGravity(space, cpv(0, -500));
149 cpSpaceSetSleepTimeThreshold(space, 0.5f);
150 cpSpaceSetCollisionSlop(space, 0.5f);
151
152 cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
153 cpShape *shape;
154
155 // Create segments around the edge of the screen.
156 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-1000,-240), cpv(1000,-240), 0.0f));
157 cpShapeSetElasticity(shape, 1.0f);
158 cpShapeSetFriction(shape, 1.0f);
159 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
160
161 cpFloat width = 200.0f;
162 cpFloat height = 300.0f;
163 cpFloat mass = width*height*DENSITY;
164 cpFloat moment = cpMomentForBox(mass, width, height);
165
166 body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
167
168 shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));
169 cpShapeSetFriction(shape, 0.6f);
170
171 return space;
172 }
173
174 static void
175 destroy(cpSpace *space)
176 {
177 ChipmunkDemoFreeSpaceChildren(space);
178 cpSpaceFree(space);
179 }
180
181 ChipmunkDemo Slice = {
182 "Slice.",
183 1.0/60.0,
184 init,
185 update,
186 ChipmunkDemoDefaultDrawImpl,
187 destroy,
188 };