Debian Codegraph @

demo/Buoyancy.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 static void
27 update(cpSpace *space, double dt)
28 {
29 cpSpaceStep(space, dt);
30 }
31
32 #define FLUID_DENSITY 0.00014
33 #define FLUID_DRAG 2.0
34
35 char messageBuffer[1024];
36
37 // Modified from chipmunk_private.h
38 static inline cpFloat
39 k_scalar_body(cpBody *body, cpVect point, cpVect n)
40 {
41 cpFloat rcn = cpvcross(cpvsub(point, cpBodyGetPosition(body)), n);
42 return 1.0f/cpBodyGetMass(body) + rcn*rcn/cpBodyGetMoment(body);
43 }
44
45 static cpBool
46 waterPreSolve(cpArbiter *arb, cpSpace *space, void *ptr)
47 {
48 CP_ARBITER_GET_SHAPES(arb, water, poly);
49 cpBody *body = cpShapeGetBody(poly);
50
51 // Get the top of the water sensor bounding box to use as the water level.
52 cpFloat level = cpShapeGetBB(water).t;
53
54 // Clip the polygon against the water level
55 int count = cpPolyShapeGetCount(poly);
56 int clippedCount = 0;
57 #ifdef _MSC_VER
58 // MSVC is pretty much the only compiler in existence that doesn't support variable sized arrays.
59 cpVect clipped[10];
60 #else
61 cpVect clipped[count + 1];
62 #endif
63
64 for(int i=0, j=count-1; i<count; j=i, i++){
65 cpVect a = cpBodyLocalToWorld(body, cpPolyShapeGetVert(poly, j));
66 cpVect b = cpBodyLocalToWorld(body, cpPolyShapeGetVert(poly, i));
67
68 if(a.y < level){
69 clipped[clippedCount] = a;
70 clippedCount++;
71 }
72
73 cpFloat a_level = a.y - level;
74 cpFloat b_level = b.y - level;
75
76 if(a_level*b_level < 0.0f){
77 cpFloat t = cpfabs(a_level)/(cpfabs(a_level) + cpfabs(b_level));
78
79 clipped[clippedCount] = cpvlerp(a, b, t);
80 clippedCount++;
81 }
82 }
83
84 // Calculate buoyancy from the clipped polygon area
85 cpFloat clippedArea = cpAreaForPoly(clippedCount, clipped, 0.0f);
86 cpFloat displacedMass = clippedArea*FLUID_DENSITY;
87 cpVect centroid = cpCentroidForPoly(clippedCount, clipped);
88
89 ChipmunkDebugDrawPolygon(clippedCount, clipped, 5.0f, RGBAColor(0, 0, 1, 1), RGBAColor(0, 0, 1, 0.1f));
90 ChipmunkDebugDrawDot(5, centroid, RGBAColor(0, 0, 1, 1));
91
92 cpFloat dt = cpSpaceGetCurrentTimeStep(space);
93 cpVect g = cpSpaceGetGravity(space);
94
95 // Apply the buoyancy force as an impulse.
96 cpBodyApplyImpulseAtWorldPoint(body, cpvmult(g, -displacedMass*dt), centroid);
97
98 // Apply linear damping for the fluid drag.
99 cpVect v_centroid = cpBodyGetVelocityAtWorldPoint(body, centroid);
100 cpFloat k = k_scalar_body(body, centroid, cpvnormalize(v_centroid));
101 cpFloat damping = clippedArea*FLUID_DRAG*FLUID_DENSITY;
102 cpFloat v_coef = cpfexp(-damping*dt*k); // linear drag
103 // cpFloat v_coef = 1.0/(1.0 + damping*dt*cpvlength(v_centroid)*k); // quadratic drag
104 cpBodyApplyImpulseAtWorldPoint(body, cpvmult(cpvsub(cpvmult(v_centroid, v_coef), v_centroid), 1.0/k), centroid);
105
106 // Apply angular damping for the fluid drag.
107 cpVect cog = cpBodyLocalToWorld(body, cpBodyGetCenterOfGravity(body));
108 cpFloat w_damping = cpMomentForPoly(FLUID_DRAG*FLUID_DENSITY*clippedArea, clippedCount, clipped, cpvneg(cog), 0.0f);
109 cpBodySetAngularVelocity(body, cpBodyGetAngularVelocity(body)*cpfexp(-w_damping*dt/cpBodyGetMoment(body)));
110
111 return cpTrue;
112 }
113
114 static cpSpace *
115 init(void)
116 {
117 ChipmunkDemoMessageString = messageBuffer;
118
119 cpSpace *space = cpSpaceNew();
120 cpSpaceSetIterations(space, 30);
121 cpSpaceSetGravity(space, cpv(0, -500));
122 // cpSpaceSetDamping(space, 0.5);
123 cpSpaceSetSleepTimeThreshold(space, 0.5f);
124 cpSpaceSetCollisionSlop(space, 0.5f);
125
126 cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
127 cpShape *shape;
128
129 // Create segments around the edge of the screen.
130 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
131 cpShapeSetElasticity(shape, 1.0f);
132 cpShapeSetFriction(shape, 1.0f);
133 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
134
135 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
136 cpShapeSetElasticity(shape, 1.0f);
137 cpShapeSetFriction(shape, 1.0f);
138 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
139
140 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
141 cpShapeSetElasticity(shape, 1.0f);
142 cpShapeSetFriction(shape, 1.0f);
143 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
144
145 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f));
146 cpShapeSetElasticity(shape, 1.0f);
147 cpShapeSetFriction(shape, 1.0f);
148 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
149
150 {
151 // Add the edges of the bucket
152 cpBB bb = cpBBNew(-300, -200, 100, 0);
153 cpFloat radius = 5.0f;
154
155 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(bb.l, bb.b), cpv(bb.l, bb.t), radius));
156 cpShapeSetElasticity(shape, 1.0f);
157 cpShapeSetFriction(shape, 1.0f);
158 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
159
160 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(bb.r, bb.b), cpv(bb.r, bb.t), radius));
161 cpShapeSetElasticity(shape, 1.0f);
162 cpShapeSetFriction(shape, 1.0f);
163 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
164
165 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(bb.l, bb.b), cpv(bb.r, bb.b), radius));
166 cpShapeSetElasticity(shape, 1.0f);
167 cpShapeSetFriction(shape, 1.0f);
168 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
169
170 // Add the sensor for the water.
171 shape = cpSpaceAddShape(space, cpBoxShapeNew2(staticBody, bb, 0.0));
172 cpShapeSetSensor(shape, cpTrue);
173 cpShapeSetCollisionType(shape, 1);
174 }
175
176
177 {
178 cpFloat width = 200.0f;
179 cpFloat height = 50.0f;
180 cpFloat mass = 0.3*FLUID_DENSITY*width*height;
181 cpFloat moment = cpMomentForBox(mass, width, height);
182
183 body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
184 cpBodySetPosition(body, cpv(-50, -100));
185 cpBodySetVelocity(body, cpv(0, -100));
186 cpBodySetAngularVelocity(body, 1);
187
188 shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));
189 cpShapeSetFriction(shape, 0.8f);
190 }
191
192 {
193 cpFloat width = 40.0f;
194 cpFloat height = width*2;
195 cpFloat mass = 0.3*FLUID_DENSITY*width*height;
196 cpFloat moment = cpMomentForBox(mass, width, height);
197
198 body = cpSpaceAddBody(space, cpBodyNew(mass, moment));
199 cpBodySetPosition(body, cpv(-200, -50));
200 cpBodySetVelocity(body, cpv(0, -100));
201 cpBodySetAngularVelocity(body, 1);
202
203 shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0));
204 cpShapeSetFriction(shape, 0.8f);
205 }
206
207 cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space, 1, 0);
208 handler->preSolveFunc = (cpCollisionPreSolveFunc)waterPreSolve;
209
210 return space;
211 }
212
213 static void
214 destroy(cpSpace *space)
215 {
216 ChipmunkDemoFreeSpaceChildren(space);
217 cpSpaceFree(space);
218 }
219
220 ChipmunkDemo Buoyancy = {
221 "Simple Sensor based fluids.",
222 1.0/180.0,
223 init,
224 update,
225 ChipmunkDemoDefaultDrawImpl,
226 destroy,
227 };