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"cxx . . $ `<file>/include/chipmunk/chipmunk.h`/
23
24 #include "ChipmunkDemo.h"cxx . . $ `<file>/demo/ChipmunkDemo.h`/
25
26 static void
27 updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt)
28 {
29 cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt);
30 }
31
32 #define FLUID_DENSITYcxx . . $ `demo/Buoyancy.c:32:9`! 0.00014
33 #define FLUID_DRAGcxx . . $ `demo/Buoyancy.c:33:9`! 2.0
34
35 char messageBuffercxx . . $ messageBuffer.[1024];
36
37 // Modified from chipmunk_private.h
38 static inline cpFloatcxx . . $ cpFloat#
39 k_scalar_bodycxx . . $ k_scalar_body(73c5c6160c3743a1).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# point, cpVectcxx . . $ cpVect# n)
40 {
41 cpFloatcxx . . $ cpFloat# rcn = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(point, cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(body)), n);
42 return 1.0f/cpBodyGetMasscxx . . $ cpBodyGetMass(2880d7999e0b34c3).(body) + rcn*rcn/cpBodyGetMomentcxx . . $ cpBodyGetMoment(2880d7999e0b34c3).(body);
43 }
44
45 static cpBoolcxx . . $ cpBool#
46 waterPreSolvecxx . . $ waterPreSolve(46926bbb19d93a13).(cpArbitercxx . . $ cpArbiter# *arb, cpSpacecxx . . $ cpSpace# *space, void *ptr)
47 {
48 CP_ARBITER_GET_SHAPEScxx . . $ `include/chipmunk/cpArbiter.h:73:9`!(arb, water, poly);
49 cpBodycxx . . $ cpBody# *body = cpShapeGetBodycxx . . $ cpShapeGetBody(abc772114277478f).(poly);
50
51 // Get the top of the water sensor bounding box to use as the water level.
52 cpFloatcxx . . $ cpFloat# level = cpShapeGetBBcxx . . $ cpShapeGetBB(44a9343cdf942fdb).(water).tcxx . . $ cpBB#t.;
53
54 // Clip the polygon against the water level
55 int count = cpPolyShapeGetCountcxx . . $ cpPolyShapeGetCount(46d926dc73b3f622).(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 cpVectcxx . . $ cpVect# clipped[count + 1];
62 #endif
63
64 for(int i=0, j=count-1; i<count; j=i, i++){
65 cpVectcxx . . $ cpVect# a = cpBodyLocalToWorldcxx . . $ cpBodyLocalToWorld(4ff4900d49a97e73).(body, cpPolyShapeGetVertcxx . . $ cpPolyShapeGetVert(9392d0fc785db450).(poly, j));
66 cpVectcxx . . $ cpVect# b = cpBodyLocalToWorldcxx . . $ cpBodyLocalToWorld(4ff4900d49a97e73).(body, cpPolyShapeGetVertcxx . . $ cpPolyShapeGetVert(9392d0fc785db450).(poly, i));
67
68 if(a.ycxx . . $ cpVect#y. < level){
69 clipped[clippedCount] = a;
70 clippedCount++;
71 }
72
73 cpFloatcxx . . $ cpFloat# a_level = a.ycxx . . $ cpVect#y. - level;
74 cpFloatcxx . . $ cpFloat# b_level = b.ycxx . . $ cpVect#y. - level;
75
76 if(a_level*b_level < 0.0f){
77 cpFloatcxx . . $ cpFloat# t = cpfabscxx . . $ cpfabs(447c67c6676f75c3).(a_level)/(cpfabscxx . . $ cpfabs(447c67c6676f75c3).(a_level) + cpfabscxx . . $ cpfabs(447c67c6676f75c3).(b_level));
78
79 clipped[clippedCount] = cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(a, b, t);
80 clippedCount++;
81 }
82 }
83
84 // Calculate buoyancy from the clipped polygon area
85 cpFloatcxx . . $ cpFloat# clippedArea = cpAreaForPolycxx . . $ cpAreaForPoly(c1e6bc8c2660eac5).(clippedCount, clipped, 0.0f);
86 cpFloatcxx . . $ cpFloat# displacedMass = clippedArea*FLUID_DENSITYcxx . . $ `demo/Buoyancy.c:32:9`!;
87 cpVectcxx . . $ cpVect# centroid = cpCentroidForPolycxx . . $ cpCentroidForPoly(740f059a8e4cff9d).(clippedCount, clipped);
88
89 ChipmunkDebugDrawPolygoncxx . . $ ChipmunkDebugDrawPolygon(6db671fc658f3d5c).(clippedCount, clipped, 5.0f, RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(0, 0, 1, 1), RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(0, 0, 1, 0.1f));
90 ChipmunkDebugDrawDotcxx . . $ ChipmunkDebugDrawDot(227e9b49eafe2913).(5, centroid, RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(0, 0, 1, 1));
91
92 cpFloatcxx . . $ cpFloat# dt = cpSpaceGetCurrentTimeStepcxx . . $ cpSpaceGetCurrentTimeStep(f2dd6279e77f9c6b).(space);
93 cpVectcxx . . $ cpVect# g = cpSpaceGetGravitycxx . . $ cpSpaceGetGravity(9a5a38e87209f086).(space);
94
95 // Apply the buoyancy force as an impulse.
96 cpBodyApplyImpulseAtWorldPointcxx . . $ cpBodyApplyImpulseAtWorldPoint(3a5c65d925931a3e).(body, cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(g, -displacedMass*dt), centroid);
97
98 // Apply linear damping for the fluid drag.
99 cpVectcxx . . $ cpVect# v_centroid = cpBodyGetVelocityAtWorldPointcxx . . $ cpBodyGetVelocityAtWorldPoint(4ff4900d49a97e73).(body, centroid);
100 cpFloatcxx . . $ cpFloat# k = k_scalar_bodycxx . . $ k_scalar_body(73c5c6160c3743a1).(body, centroid, cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(v_centroid));
101 cpFloatcxx . . $ cpFloat# damping = clippedArea*FLUID_DRAGcxx . . $ `demo/Buoyancy.c:33:9`!*FLUID_DENSITYcxx . . $ `demo/Buoyancy.c:32:9`!;
102 cpFloatcxx . . $ cpFloat# v_coef = cpfexpcxx . . $ `include/chipmunk/chipmunk_types.h:74:10`!(-damping*dt*k); // linear drag
103 // cpFloat v_coef = 1.0/(1.0 + damping*dt*cpvlength(v_centroid)*k); // quadratic drag
104 cpBodyApplyImpulseAtWorldPointcxx . . $ cpBodyApplyImpulseAtWorldPoint(3a5c65d925931a3e).(body, cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(v_centroid, v_coef), v_centroid), 1.0/k), centroid);
105
106 // Apply angular damping for the fluid drag.
107 cpVectcxx . . $ cpVect# cog = cpBodyLocalToWorldcxx . . $ cpBodyLocalToWorld(4ff4900d49a97e73).(body, cpBodyGetCenterOfGravitycxx . . $ cpBodyGetCenterOfGravity(2e8d436bcfa5d74b).(body));
108 cpFloatcxx . . $ cpFloat# w_damping = cpMomentForPolycxx . . $ cpMomentForPoly(89d843ff16ae6b28).(FLUID_DRAGcxx . . $ `demo/Buoyancy.c:33:9`!*FLUID_DENSITYcxx . . $ `demo/Buoyancy.c:32:9`!*clippedArea, clippedCount, clipped, cpvnegcxx . . $ cpvneg(e06142a62109ae08).(cog), 0.0f);
109 cpBodySetAngularVelocitycxx . . $ cpBodySetAngularVelocity(797bd3bb7a653b39).(body, cpBodyGetAngularVelocitycxx . . $ cpBodyGetAngularVelocity(2880d7999e0b34c3).(body)*cpfexpcxx . . $ `include/chipmunk/chipmunk_types.h:74:10`!(-w_damping*dt/cpBodyGetMomentcxx . . $ cpBodyGetMoment(2880d7999e0b34c3).(body)));
110
111 return cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!;
112 }
113
114 static cpSpacecxx . . $ cpSpace# *
115 initcxx . . $ init(1f0082e822a7ce76).(void)
116 {
117 ChipmunkDemoMessageStringcxx . . $ ChipmunkDemoMessageString. = messageBuffercxx . . $ messageBuffer.;
118
119 cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).();
120 cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 30);
121 cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -500));
122 // cpSpaceSetDamping(space, 0.5);
123 cpSpaceSetSleepTimeThresholdcxx . . $ cpSpaceSetSleepTimeThreshold(fa66a0a48611083e).(space, 0.5f);
124 cpSpaceSetCollisionSlopcxx . . $ cpSpaceSetCollisionSlop(fa66a0a48611083e).(space, 0.5f);
125
126 cpBodycxx . . $ cpBody# *body, *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space);
127 cpShapecxx . . $ cpShape# *shape;
128
129 // Create segments around the edge of the screen.
130 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), 0.0f));
131 cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f);
132 cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f);
133 cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.);
134
135 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f));
136 cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f);
137 cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f);
138 cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.);
139
140 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), 0.0f));
141 cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f);
142 cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f);
143 cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.);
144
145 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f));
146 cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f);
147 cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f);
148 cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.);
149
150 {
151 // Add the edges of the bucket
152 cpBBcxx . . $ cpBB# bb = cpBBNewcxx . . $ cpBBNew(f05aa3203068e6d3).(-300, -200, 100, 0);
153 cpFloatcxx . . $ cpFloat# radius = 5.0f;
154
155 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(bb.lcxx . . $ cpBB#l., bb.bcxx . . $ cpBB#b.), cpvcxx . . $ cpv(3142d57d978b2176).(bb.lcxx . . $ cpBB#l., bb.tcxx . . $ cpBB#t.), radius));
156 cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f);
157 cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f);
158 cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.);
159
160 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(bb.rcxx . . $ cpBB#r., bb.bcxx . . $ cpBB#b.), cpvcxx . . $ cpv(3142d57d978b2176).(bb.rcxx . . $ cpBB#r., bb.tcxx . . $ cpBB#t.), radius));
161 cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f);
162 cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f);
163 cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.);
164
165 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(bb.lcxx . . $ cpBB#l., bb.bcxx . . $ cpBB#b.), cpvcxx . . $ cpv(3142d57d978b2176).(bb.rcxx . . $ cpBB#r., bb.bcxx . . $ cpBB#b.), radius));
166 cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f);
167 cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f);
168 cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.);
169
170 // Add the sensor for the water.
171 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNew2cxx . . $ cpBoxShapeNew2(ac0258d00568be32).(staticBody, bb, 0.0));
172 cpShapeSetSensorcxx . . $ cpShapeSetSensor(eff4d15739771431).(shape, cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!);
173 cpShapeSetCollisionTypecxx . . $ cpShapeSetCollisionType(2dd430e3162ea2fe).(shape, 1);
174 }
175
176
177 {
178 cpFloatcxx . . $ cpFloat# width = 200.0f;
179 cpFloatcxx . . $ cpFloat# height = 50.0f;
180 cpFloatcxx . . $ cpFloat# mass = 0.3*FLUID_DENSITYcxx . . $ `demo/Buoyancy.c:32:9`!*width*height;
181 cpFloatcxx . . $ cpFloat# moment = cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(mass, width, height);
182
183 body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, moment));
184 cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(-50, -100));
185 cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(0, -100));
186 cpBodySetAngularVelocitycxx . . $ cpBodySetAngularVelocity(797bd3bb7a653b39).(body, 1);
187
188 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(body, width, height, 0.0));
189 cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.8f);
190 }
191
192 {
193 cpFloatcxx . . $ cpFloat# width = 40.0f;
194 cpFloatcxx . . $ cpFloat# height = width*2;
195 cpFloatcxx . . $ cpFloat# mass = 0.3*FLUID_DENSITYcxx . . $ `demo/Buoyancy.c:32:9`!*width*height;
196 cpFloatcxx . . $ cpFloat# moment = cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(mass, width, height);
197
198 body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, moment));
199 cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(-200, -50));
200 cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(0, -100));
201 cpBodySetAngularVelocitycxx . . $ cpBodySetAngularVelocity(797bd3bb7a653b39).(body, 1);
202
203 shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(body, width, height, 0.0));
204 cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.8f);
205 }
206
207 cpCollisionHandlercxx . . $ cpCollisionHandler# *handler = cpSpaceAddCollisionHandlercxx . . $ cpSpaceAddCollisionHandler(becf4587622cddc8).(space, 1, 0);
208 handler->preSolveFunccxx . . $ cpCollisionHandler#preSolveFunc. = (cpCollisionPreSolveFunccxx . . $ cpCollisionPreSolveFunc#)waterPreSolvecxx . . $ waterPreSolve(46926bbb19d93a13).;
209
210 return space;
211 }
212
213 static void
214 destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space)
215 {
216 ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space);
217 cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space);
218 }
219
220 ChipmunkDemocxx . . $ ChipmunkDemo# Buoyancycxx . . $ Buoyancy. = {
221 "Simple Sensor based fluids.",
222 1.0/180.0,
223 initcxx . . $ init(1f0082e822a7ce76).,
224 updatecxx . . $ update(fa66a0a48611083e).,
225 ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8).,
226 destroycxx . . $ destroy(728b9c9a69722ef8).,
227 };