Debian Codegraph @

src/cpBody.c

raw symbols SCIP
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
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 <float.h>
23 #include <stdarg.h>
24
25 #include "chipmunk/chipmunk_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/
26
27 cpBodycxx . . $ cpBody#*
28 cpBodyAlloccxx . . $ cpBodyAlloc(af6db6e743345a49).(void)
29 {
30 return (cpBodycxx . . $ cpBody# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpBodycxx . . $ cpBody#));
31 }
32
33 cpBodycxx . . $ cpBody# *
34 cpBodyInitcxx . . $ cpBodyInit(5f20355d40defd9e).(cpBodycxx . . $ cpBody# *body, cpFloatcxx . . $ cpFloat# mass, cpFloatcxx . . $ cpFloat# moment)
35 {
36 body->spacecxx . . $ cpBody#space. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
37 body->shapeListcxx . . $ cpBody#shapeList. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
38 body->arbiterListcxx . . $ cpBody#arbiterList. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
39 body->constraintListcxx . . $ cpBody#constraintList. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
40
41 body->velocity_funccxx . . $ cpBody#velocity_func. = cpBodyUpdateVelocitycxx . . $ cpBodyUpdateVelocity(1f531eb4b4f6d01b).;
42 body->position_funccxx . . $ cpBody#position_func. = cpBodyUpdatePositioncxx . . $ cpBodyUpdatePosition(797bd3bb7a653b39).;
43
44 body->sleepingcxx . . $ cpBody#sleeping..rootcxx . . $ cpBody#$anonymous_type_0#root. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
45 body->sleepingcxx . . $ cpBody#sleeping..nextcxx . . $ cpBody#$anonymous_type_0#next. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
46 body->sleepingcxx . . $ cpBody#sleeping..idleTimecxx . . $ cpBody#$anonymous_type_0#idleTime. = 0.0f;
47
48 body->pcxx . . $ cpBody#p. = cpvzerocxx . . $ cpvzero.;
49 body->vcxx . . $ cpBody#v. = cpvzerocxx . . $ cpvzero.;
50 body->fcxx . . $ cpBody#f. = cpvzerocxx . . $ cpvzero.;
51
52 body->wcxx . . $ cpBody#w. = 0.0f;
53 body->tcxx . . $ cpBody#t. = 0.0f;
54
55 body->v_biascxx . . $ cpBody#v_bias. = cpvzerocxx . . $ cpvzero.;
56 body->w_biascxx . . $ cpBody#w_bias. = 0.0f;
57
58 body->userDatacxx . . $ cpBody#userData. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
59
60 // Setters must be called after full initialization so the sanity checks don't assert on garbage data.
61 cpBodySetMasscxx . . $ cpBodySetMass(797bd3bb7a653b39).(body, mass);
62 cpBodySetMomentcxx . . $ cpBodySetMoment(797bd3bb7a653b39).(body, moment);
63 cpBodySetAnglecxx . . $ cpBodySetAngle(797bd3bb7a653b39).(body, 0.0f);
64
65 return body;
66 }
67
68 cpBodycxx . . $ cpBody#*
69 cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(cpFloatcxx . . $ cpFloat# mass, cpFloatcxx . . $ cpFloat# moment)
70 {
71 return cpBodyInitcxx . . $ cpBodyInit(5f20355d40defd9e).(cpBodyAlloccxx . . $ cpBodyAlloc(af6db6e743345a49).(), mass, moment);
72 }
73
74 cpBodycxx . . $ cpBody#*
75 cpBodyNewKinematiccxx . . $ cpBodyNewKinematic(af6db6e743345a49).()
76 {
77 cpBodycxx . . $ cpBody# *body = cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(0.0f, 0.0f);
78 cpBodySetTypecxx . . $ cpBodySetType(f2bd8afc8ccfe52).(body, CP_BODY_TYPE_KINEMATICcxx . . $ CP_BODY_TYPE_KINEMATIC.);
79
80 return body;
81 }
82
83 cpBodycxx . . $ cpBody#*
84 cpBodyNewStaticcxx . . $ cpBodyNewStatic(af6db6e743345a49).()
85 {
86 cpBodycxx . . $ cpBody# *body = cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(0.0f, 0.0f);
87 cpBodySetTypecxx . . $ cpBodySetType(f2bd8afc8ccfe52).(body, CP_BODY_TYPE_STATICcxx . . $ CP_BODY_TYPE_STATIC.);
88
89 return body;
90 }
91
92 void cpBodyDestroycxx . . $ cpBodyDestroy(92943d3541b63e72).(cpBodycxx . . $ cpBody# *body){}
93
94 void
95 cpBodyFreecxx . . $ cpBodyFree(92943d3541b63e72).(cpBodycxx . . $ cpBody# *body)
96 {
97 if(body){
98 cpBodyDestroycxx . . $ cpBodyDestroy(92943d3541b63e72).(body);
99 cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(body);
100 }
101 }
102
103 #ifdef NDEBUGcxx . . $ `<built-in>:413:9`!
104 #define cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body)
105 #else
106 static void cpv_assert_nan(cpVect v, char *message){cpAssertHard(v.x == v.x && v.y == v.y, message);}
107 static void cpv_assert_infinite(cpVect v, char *message){cpAssertHard(cpfabs(v.x) != INFINITY && cpfabs(v.y) != INFINITY, message);}
108 static void cpv_assert_sane(cpVect v, char *message){cpv_assert_nan(v, message); cpv_assert_infinite(v, message);}
109
110 static void
111 cpBodySanityCheck(const cpBody *body)
112 {
113 cpAssertHard(body->m == body->m && body->m_inv == body->m_inv, "Body's mass is NaN.");
114 cpAssertHard(body->i == body->i && body->i_inv == body->i_inv, "Body's moment is NaN.");
115 cpAssertHard(body->m >= 0.0f, "Body's mass is negative.");
116 cpAssertHard(body->i >= 0.0f, "Body's moment is negative.");
117
118 cpv_assert_sane(body->p, "Body's position is invalid.");
119 cpv_assert_sane(body->v, "Body's velocity is invalid.");
120 cpv_assert_sane(body->f, "Body's force is invalid.");
121
122 cpAssertHard(body->a == body->a && cpfabs(body->a) != INFINITY, "Body's angle is invalid.");
123 cpAssertHard(body->w == body->w && cpfabs(body->w) != INFINITY, "Body's angular velocity is invalid.");
124 cpAssertHard(body->t == body->t && cpfabs(body->t) != INFINITY, "Body's torque is invalid.");
125 }
126
127 #define cpAssertSaneBody(body) cpBodySanityCheck(body)
128 #endif
129
130 cpBoolcxx . . $ cpBool#
131 cpBodyIsSleepingcxx . . $ cpBodyIsSleeping(25bfad07631ec420).(const cpBodycxx . . $ cpBody# *body)
132 {
133 return (body->sleepingcxx . . $ cpBody#sleeping..rootcxx . . $ cpBody#$anonymous_type_0#root. != ((cpBodycxx . . $ cpBody#*)0));
134 }
135
136 cpBodyTypecxx . . $ cpBodyType#
137 cpBodyGetTypecxx . . $ cpBodyGetType(ea52de4c196d619c).(cpBodycxx . . $ cpBody# *body)
138 {
139 if(body->sleepingcxx . . $ cpBody#sleeping..idleTimecxx . . $ cpBody#$anonymous_type_0#idleTime. == INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!){
140 return CP_BODY_TYPE_STATICcxx . . $ CP_BODY_TYPE_STATIC.;
141 } else if(body->mcxx . . $ cpBody#m. == INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!){
142 return CP_BODY_TYPE_KINEMATICcxx . . $ CP_BODY_TYPE_KINEMATIC.;
143 } else {
144 return CP_BODY_TYPE_DYNAMICcxx . . $ CP_BODY_TYPE_DYNAMIC.;
145 }
146 }
147
148 void
149 cpBodySetTypecxx . . $ cpBodySetType(f2bd8afc8ccfe52).(cpBodycxx . . $ cpBody# *body, cpBodyTypecxx . . $ cpBodyType# type)
150 {
151 cpBodyTypecxx . . $ cpBodyType# oldType = cpBodyGetTypecxx . . $ cpBodyGetType(ea52de4c196d619c).(body);
152 if(oldType == type) return;
153
154 // Static bodies have their idle timers set to infinity.
155 // Non-static bodies should have their idle timer reset.
156 body->sleepingcxx . . $ cpBody#sleeping..idleTimecxx . . $ cpBody#$anonymous_type_0#idleTime. = (type == CP_BODY_TYPE_STATICcxx . . $ CP_BODY_TYPE_STATIC. ? INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`! : 0.0f);
157
158 if(type == CP_BODY_TYPE_DYNAMICcxx . . $ CP_BODY_TYPE_DYNAMIC.){
159 body->mcxx . . $ cpBody#m. = body->icxx . . $ cpBody#i. = 0.0f;
160 body->m_invcxx . . $ cpBody#m_inv. = body->i_invcxx . . $ cpBody#i_inv. = INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!;
161
162 cpBodyAccumulateMassFromShapescxx . . $ cpBodyAccumulateMassFromShapes(92943d3541b63e72).(body);
163 } else {
164 body->mcxx . . $ cpBody#m. = body->icxx . . $ cpBody#i. = INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!;
165 body->m_invcxx . . $ cpBody#m_inv. = body->i_invcxx . . $ cpBody#i_inv. = 0.0f;
166
167 body->vcxx . . $ cpBody#v. = cpvzerocxx . . $ cpvzero.;
168 body->wcxx . . $ cpBody#w. = 0.0f;
169 }
170
171 // If the body is added to a space already, we'll need to update some space data structures.
172 cpSpacecxx . . $ cpSpace# *space = cpBodyGetSpacecxx . . $ cpBodyGetSpace(9afa6925f7def55d).(body);
173 if(space != NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!){
174 cpAssertSpaceUnlockedcxx . . $ `include/chipmunk/chipmunk_private.h:273:9`!(space);
175
176 if(oldType == CP_BODY_TYPE_STATICcxx . . $ CP_BODY_TYPE_STATIC.){
177 // TODO This is probably not necessary
178 // cpBodyActivateStatic(body, NULL);
179 } else {
180 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
181 }
182
183 // Move the bodies to the correct array.
184 cpArraycxx . . $ cpArray# *fromArray = cpSpaceArrayForBodyTypecxx . . $ cpSpaceArrayForBodyType(32c9f2d2827df95e).(space, oldType);
185 cpArraycxx . . $ cpArray# *toArray = cpSpaceArrayForBodyTypecxx . . $ cpSpaceArrayForBodyType(32c9f2d2827df95e).(space, type);
186 if(fromArray != toArray){
187 cpArrayDeleteObjcxx . . $ cpArrayDeleteObj(5027a0abedaf1ddc).(fromArray, body);
188 cpArrayPushcxx . . $ cpArrayPush(5027a0abedaf1ddc).(toArray, body);
189 }
190
191 // Move the body's shapes to the correct spatial index.
192 cpSpatialIndexcxx . . $ cpSpatialIndex# *fromIndex = (oldType == CP_BODY_TYPE_STATICcxx . . $ CP_BODY_TYPE_STATIC. ? space->staticShapescxx . . $ cpSpace#staticShapes. : space->dynamicShapescxx . . $ cpSpace#dynamicShapes.);
193 cpSpatialIndexcxx . . $ cpSpatialIndex# *toIndex = (type == CP_BODY_TYPE_STATICcxx . . $ CP_BODY_TYPE_STATIC. ? space->staticShapescxx . . $ cpSpace#staticShapes. : space->dynamicShapescxx . . $ cpSpace#dynamicShapes.);
194 if(fromIndex != toIndex){
195 CP_BODY_FOREACH_SHAPEcxx . . $ `include/chipmunk/chipmunk_private.h:338:9`!(body, shape){
196 cpSpatialIndexRemovecxx . . $ cpSpatialIndexRemove(58272ff302488c63).(fromIndex, shape, shape->hashidcxx . . $ cpShape#hashid.);
197 cpSpatialIndexInsertcxx . . $ cpSpatialIndexInsert(58272ff302488c63).(toIndex, shape, shape->hashidcxx . . $ cpShape#hashid.);
198 }
199 }
200 }
201 }
202
203
204
205 // Should *only* be called when shapes with mass info are modified, added or removed.
206 void
207 cpBodyAccumulateMassFromShapescxx . . $ cpBodyAccumulateMassFromShapes(92943d3541b63e72).(cpBodycxx . . $ cpBody# *body)
208 {
209 if(body == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`! || cpBodyGetTypecxx . . $ cpBodyGetType(ea52de4c196d619c).(body) != CP_BODY_TYPE_DYNAMICcxx . . $ CP_BODY_TYPE_DYNAMIC.) return;
210
211 // Reset the body's mass data.
212 body->mcxx . . $ cpBody#m. = body->icxx . . $ cpBody#i. = 0.0f;
213 body->cogcxx . . $ cpBody#cog. = cpvzerocxx . . $ cpvzero.;
214
215 // Cache the position to realign it at the end.
216 cpVectcxx . . $ cpVect# pos = cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(body);
217
218 // Accumulate mass from shapes.
219 CP_BODY_FOREACH_SHAPEcxx . . $ `include/chipmunk/chipmunk_private.h:338:9`!(body, shape){
220 struct cpShapeMassInfocxx . . $ cpShapeMassInfo# *info = &shape->massInfocxx . . $ cpShape#massInfo.;
221 cpFloatcxx . . $ cpFloat# m = info->mcxx . . $ cpShapeMassInfo#m.;
222
223 if(m > 0.0f){
224 cpFloatcxx . . $ cpFloat# msum = body->mcxx . . $ cpBody#m. + m;
225
226 body->icxx . . $ cpBody#i. += m*info->icxx . . $ cpShapeMassInfo#i. + cpvdistsqcxx . . $ cpvdistsq(7d57c1c4dd559d53).(body->cogcxx . . $ cpBody#cog., info->cogcxx . . $ cpShapeMassInfo#cog.)*(m*body->mcxx . . $ cpBody#m.)/msum;
227 body->cogcxx . . $ cpBody#cog. = cpvlerpcxx . . $ cpvlerp(12b05c0b9acd6d4b).(body->cogcxx . . $ cpBody#cog., info->cogcxx . . $ cpShapeMassInfo#cog., m/msum);
228 body->mcxx . . $ cpBody#m. = msum;
229 }
230 }
231
232 // Recalculate the inverses.
233 body->m_invcxx . . $ cpBody#m_inv. = 1.0f/body->mcxx . . $ cpBody#m.;
234 body->i_invcxx . . $ cpBody#i_inv. = 1.0f/body->icxx . . $ cpBody#i.;
235
236 // Realign the body since the CoG has probably moved.
237 cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, pos);
238 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
239 }
240
241 cpSpacecxx . . $ cpSpace# *
242 cpBodyGetSpacecxx . . $ cpBodyGetSpace(9afa6925f7def55d).(const cpBodycxx . . $ cpBody# *body)
243 {
244 return body->spacecxx . . $ cpBody#space.;
245 }
246
247 cpFloatcxx . . $ cpFloat#
248 cpBodyGetMasscxx . . $ cpBodyGetMass(2880d7999e0b34c3).(const cpBodycxx . . $ cpBody# *body)
249 {
250 return body->mcxx . . $ cpBody#m.;
251 }
252
253 void
254 cpBodySetMasscxx . . $ cpBodySetMass(797bd3bb7a653b39).(cpBodycxx . . $ cpBody# *body, cpFloatcxx . . $ cpFloat# mass)
255 {
256 cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpBodyGetType(body) == CP_BODY_TYPE_DYNAMIC, "You cannot set the mass of kinematic or static bodies.");
257 cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(0.0f <= mass && mass < INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!, "Mass must be positive and finite.");
258
259 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
260 body->mcxx . . $ cpBody#m. = mass;
261 body->m_invcxx . . $ cpBody#m_inv. = mass == 0.0f ? INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`! : 1.0f/mass;
262 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
263 }
264
265 cpFloatcxx . . $ cpFloat#
266 cpBodyGetMomentcxx . . $ cpBodyGetMoment(2880d7999e0b34c3).(const cpBodycxx . . $ cpBody# *body)
267 {
268 return body->icxx . . $ cpBody#i.;
269 }
270
271 void
272 cpBodySetMomentcxx . . $ cpBodySetMoment(797bd3bb7a653b39).(cpBodycxx . . $ cpBody# *body, cpFloatcxx . . $ cpFloat# moment)
273 {
274 cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(moment >= 0.0f, "Moment of Inertia must be positive.");
275
276 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
277 body->icxx . . $ cpBody#i. = moment;
278 body->i_invcxx . . $ cpBody#i_inv. = moment == 0.0f ? INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`! : 1.0f/moment;
279 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
280 }
281
282 cpVectcxx . . $ cpVect#
283 cpBodyGetRotationcxx . . $ cpBodyGetRotation(2e8d436bcfa5d74b).(const cpBodycxx . . $ cpBody# *body)
284 {
285 return cpvcxx . . $ cpv(3142d57d978b2176).(body->transformcxx . . $ cpBody#transform..acxx . . $ cpTransform#a., body->transformcxx . . $ cpBody#transform..bcxx . . $ cpTransform#b.);
286 }
287
288 void
289 cpBodyAddShapecxx . . $ cpBodyAddShape(84a6c2742ea0ff64).(cpBodycxx . . $ cpBody# *body, cpShapecxx . . $ cpShape# *shape)
290 {
291 cpShapecxx . . $ cpShape# *next = body->shapeListcxx . . $ cpBody#shapeList.;
292 if(next) next->prevcxx . . $ cpShape#prev. = shape;
293
294 shape->nextcxx . . $ cpShape#next. = next;
295 body->shapeListcxx . . $ cpBody#shapeList. = shape;
296
297 if(shape->massInfocxx . . $ cpShape#massInfo..mcxx . . $ cpShapeMassInfo#m. > 0.0f){
298 cpBodyAccumulateMassFromShapescxx . . $ cpBodyAccumulateMassFromShapes(92943d3541b63e72).(body);
299 }
300 }
301
302 void
303 cpBodyRemoveShapecxx . . $ cpBodyRemoveShape(84a6c2742ea0ff64).(cpBodycxx . . $ cpBody# *body, cpShapecxx . . $ cpShape# *shape)
304 {
305 cpShapecxx . . $ cpShape# *prev = shape->prevcxx . . $ cpShape#prev.;
306 cpShapecxx . . $ cpShape# *next = shape->nextcxx . . $ cpShape#next.;
307
308 if(prev){
309 prev->nextcxx . . $ cpShape#next. = next;
310 } else {
311 body->shapeListcxx . . $ cpBody#shapeList. = next;
312 }
313
314 if(next){
315 next->prevcxx . . $ cpShape#prev. = prev;
316 }
317
318 shape->prevcxx . . $ cpShape#prev. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
319 shape->nextcxx . . $ cpShape#next. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
320
321 if(cpBodyGetTypecxx . . $ cpBodyGetType(ea52de4c196d619c).(body) == CP_BODY_TYPE_DYNAMICcxx . . $ CP_BODY_TYPE_DYNAMIC. && shape->massInfocxx . . $ cpShape#massInfo..mcxx . . $ cpShapeMassInfo#m. > 0.0f){
322 cpBodyAccumulateMassFromShapescxx . . $ cpBodyAccumulateMassFromShapes(92943d3541b63e72).(body);
323 }
324 }
325
326 static cpConstraintcxx . . $ cpConstraint# *
327 filterConstraintscxx . . $ filterConstraints(e28c68ddc36557ed).(cpConstraintcxx . . $ cpConstraint# *node, cpBodycxx . . $ cpBody# *body, cpConstraintcxx . . $ cpConstraint# *filter)
328 {
329 if(node == filter){
330 return cpConstraintNextcxx . . $ cpConstraintNext(5c2e539fc6d7ad37).(node, body);
331 } else if(node->acxx . . $ cpConstraint#a. == body){
332 node->next_acxx . . $ cpConstraint#next_a. = filterConstraintscxx . . $ filterConstraints(e28c68ddc36557ed).(node->next_acxx . . $ cpConstraint#next_a., body, filter);
333 } else {
334 node->next_bcxx . . $ cpConstraint#next_b. = filterConstraintscxx . . $ filterConstraints(e28c68ddc36557ed).(node->next_bcxx . . $ cpConstraint#next_b., body, filter);
335 }
336
337 return node;
338 }
339
340 void
341 cpBodyRemoveConstraintcxx . . $ cpBodyRemoveConstraint(f4dd701ccc0b05d0).(cpBodycxx . . $ cpBody# *body, cpConstraintcxx . . $ cpConstraint# *constraint)
342 {
343 body->constraintListcxx . . $ cpBody#constraintList. = filterConstraintscxx . . $ filterConstraints(e28c68ddc36557ed).(body->constraintListcxx . . $ cpBody#constraintList., body, constraint);
344 }
345
346 // 'p' is the position of the CoG
347 static void
348 SetTransformcxx . . $ SetTransform(c1433ec04480551).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# p, cpFloatcxx . . $ cpFloat# a)
349 {
350 cpVectcxx . . $ cpVect# rot = cpvforanglecxx . . $ cpvforangle(8d5769cc69967cd4).(a);
351 cpVectcxx . . $ cpVect# c = body->cogcxx . . $ cpBody#cog.;
352
353 body->transformcxx . . $ cpBody#transform. = cpTransformNewTransposecxx . . $ cpTransformNewTranspose(1e44c8647e61dd7e).(
354 rot.xcxx . . $ cpVect#x., -rot.ycxx . . $ cpVect#y., p.xcxx . . $ cpVect#x. - (c.xcxx . . $ cpVect#x.*rot.xcxx . . $ cpVect#x. - c.ycxx . . $ cpVect#y.*rot.ycxx . . $ cpVect#y.),
355 rot.ycxx . . $ cpVect#y., rot.xcxx . . $ cpVect#x., p.ycxx . . $ cpVect#y. - (c.xcxx . . $ cpVect#x.*rot.ycxx . . $ cpVect#y. + c.ycxx . . $ cpVect#y.*rot.xcxx . . $ cpVect#x.)
356 );
357 }
358
359 static inline cpFloatcxx . . $ cpFloat#
360 SetAnglecxx . . $ SetAngle(931ce5e1a301777d).(cpBodycxx . . $ cpBody# *body, cpFloatcxx . . $ cpFloat# a)
361 {
362 body->acxx . . $ cpBody#a. = a;
363 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
364
365 return a;
366 }
367
368 cpVectcxx . . $ cpVect#
369 cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(const cpBodycxx . . $ cpBody# *body)
370 {
371 return cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., cpvzerocxx . . $ cpvzero.);
372 }
373
374 void
375 cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# position)
376 {
377 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
378 cpVectcxx . . $ cpVect# p = body->pcxx . . $ cpBody#p. = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., body->cogcxx . . $ cpBody#cog.), position);
379 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
380
381 SetTransformcxx . . $ SetTransform(c1433ec04480551).(body, p, body->acxx . . $ cpBody#a.);
382 }
383
384 cpVectcxx . . $ cpVect#
385 cpBodyGetCenterOfGravitycxx . . $ cpBodyGetCenterOfGravity(2e8d436bcfa5d74b).(const cpBodycxx . . $ cpBody# *body)
386 {
387 return body->cogcxx . . $ cpBody#cog.;
388 }
389
390 void
391 cpBodySetCenterOfGravitycxx . . $ cpBodySetCenterOfGravity(864e17bcbe059b08).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# cog)
392 {
393 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
394 body->cogcxx . . $ cpBody#cog. = cog;
395 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
396 }
397
398 cpVectcxx . . $ cpVect#
399 cpBodyGetVelocitycxx . . $ cpBodyGetVelocity(2e8d436bcfa5d74b).(const cpBodycxx . . $ cpBody# *body)
400 {
401 return body->vcxx . . $ cpBody#v.;
402 }
403
404 void
405 cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# velocity)
406 {
407 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
408 body->vcxx . . $ cpBody#v. = velocity;
409 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
410 }
411
412 cpVectcxx . . $ cpVect#
413 cpBodyGetForcecxx . . $ cpBodyGetForce(2e8d436bcfa5d74b).(const cpBodycxx . . $ cpBody# *body)
414 {
415 return body->fcxx . . $ cpBody#f.;
416 }
417
418 void
419 cpBodySetForcecxx . . $ cpBodySetForce(864e17bcbe059b08).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# force)
420 {
421 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
422 body->fcxx . . $ cpBody#f. = force;
423 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
424 }
425
426 cpFloatcxx . . $ cpFloat#
427 cpBodyGetAnglecxx . . $ cpBodyGetAngle(2880d7999e0b34c3).(const cpBodycxx . . $ cpBody# *body)
428 {
429 return body->acxx . . $ cpBody#a.;
430 }
431
432 void
433 cpBodySetAnglecxx . . $ cpBodySetAngle(797bd3bb7a653b39).(cpBodycxx . . $ cpBody# *body, cpFloatcxx . . $ cpFloat# angle)
434 {
435 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
436 SetAnglecxx . . $ SetAngle(931ce5e1a301777d).(body, angle);
437
438 SetTransformcxx . . $ SetTransform(c1433ec04480551).(body, body->pcxx . . $ cpBody#p., angle);
439 }
440
441 cpFloatcxx . . $ cpFloat#
442 cpBodyGetAngularVelocitycxx . . $ cpBodyGetAngularVelocity(2880d7999e0b34c3).(const cpBodycxx . . $ cpBody# *body)
443 {
444 return body->wcxx . . $ cpBody#w.;
445 }
446
447 void
448 cpBodySetAngularVelocitycxx . . $ cpBodySetAngularVelocity(797bd3bb7a653b39).(cpBodycxx . . $ cpBody# *body, cpFloatcxx . . $ cpFloat# angularVelocity)
449 {
450 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
451 body->wcxx . . $ cpBody#w. = angularVelocity;
452 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
453 }
454
455 cpFloatcxx . . $ cpFloat#
456 cpBodyGetTorquecxx . . $ cpBodyGetTorque(2880d7999e0b34c3).(const cpBodycxx . . $ cpBody# *body)
457 {
458 return body->tcxx . . $ cpBody#t.;
459 }
460
461 void
462 cpBodySetTorquecxx . . $ cpBodySetTorque(797bd3bb7a653b39).(cpBodycxx . . $ cpBody# *body, cpFloatcxx . . $ cpFloat# torque)
463 {
464 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
465 body->tcxx . . $ cpBody#t. = torque;
466 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
467 }
468
469 cpDataPointercxx . . $ cpDataPointer#
470 cpBodyGetUserDatacxx . . $ cpBodyGetUserData(30790fc606a40a79).(const cpBodycxx . . $ cpBody# *body)
471 {
472 return body->userDatacxx . . $ cpBody#userData.;
473 }
474
475 void
476 cpBodySetUserDatacxx . . $ cpBodySetUserData(3eac74103e9ce6d).(cpBodycxx . . $ cpBody# *body, cpDataPointercxx . . $ cpDataPointer# userData)
477 {
478 body->userDatacxx . . $ cpBody#userData. = userData;
479 }
480
481 void
482 cpBodySetVelocityUpdateFunccxx . . $ cpBodySetVelocityUpdateFunc(54ad947a9354141a).(cpBodycxx . . $ cpBody# *body, cpBodyVelocityFunccxx . . $ cpBodyVelocityFunc# velocityFunc)
483 {
484 body->velocity_funccxx . . $ cpBody#velocity_func. = velocityFunc;
485 }
486
487 void
488 cpBodySetPositionUpdateFunccxx . . $ cpBodySetPositionUpdateFunc(22e00ee7bfa83717).(cpBodycxx . . $ cpBody# *body, cpBodyPositionFunccxx . . $ cpBodyPositionFunc# positionFunc)
489 {
490 body->position_funccxx . . $ cpBody#position_func. = positionFunc;
491 }
492
493 void
494 cpBodyUpdateVelocitycxx . . $ cpBodyUpdateVelocity(1f531eb4b4f6d01b).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# gravity, cpFloatcxx . . $ cpFloat# damping, cpFloatcxx . . $ cpFloat# dt)
495 {
496 // Skip kinematic bodies.
497 if(cpBodyGetTypecxx . . $ cpBodyGetType(ea52de4c196d619c).(body) == CP_BODY_TYPE_KINEMATICcxx . . $ CP_BODY_TYPE_KINEMATIC.) return;
498
499 cpAssertSoftcxx . . $ `include/chipmunk/chipmunk.h:51:10`!(body->m > 0.0f && body->i > 0.0f, "Body's mass and moment must be positive to simulate. (Mass: %f Moment: %f)", body->m, body->i);
500
501 body->vcxx . . $ cpBody#v. = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(body->vcxx . . $ cpBody#v., damping), cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(gravity, cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(body->fcxx . . $ cpBody#f., body->m_invcxx . . $ cpBody#m_inv.)), dt));
502 body->wcxx . . $ cpBody#w. = body->wcxx . . $ cpBody#w.*damping + body->tcxx . . $ cpBody#t.*body->i_invcxx . . $ cpBody#i_inv.*dt;
503
504 // Reset forces.
505 body->fcxx . . $ cpBody#f. = cpvzerocxx . . $ cpvzero.;
506 body->tcxx . . $ cpBody#t. = 0.0f;
507
508 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
509 }
510
511 void
512 cpBodyUpdatePositioncxx . . $ cpBodyUpdatePosition(797bd3bb7a653b39).(cpBodycxx . . $ cpBody# *body, cpFloatcxx . . $ cpFloat# dt)
513 {
514 cpVectcxx . . $ cpVect# p = body->pcxx . . $ cpBody#p. = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(body->pcxx . . $ cpBody#p., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(body->vcxx . . $ cpBody#v., body->v_biascxx . . $ cpBody#v_bias.), dt));
515 cpFloatcxx . . $ cpFloat# a = SetAnglecxx . . $ SetAngle(931ce5e1a301777d).(body, body->acxx . . $ cpBody#a. + (body->wcxx . . $ cpBody#w. + body->w_biascxx . . $ cpBody#w_bias.)*dt);
516 SetTransformcxx . . $ SetTransform(c1433ec04480551).(body, p, a);
517
518 body->v_biascxx . . $ cpBody#v_bias. = cpvzerocxx . . $ cpvzero.;
519 body->w_biascxx . . $ cpBody#w_bias. = 0.0f;
520
521 cpAssertSaneBodycxx . . $ `src/cpBody.c:104:10`!(body);
522 }
523
524 cpVectcxx . . $ cpVect#
525 cpBodyLocalToWorldcxx . . $ cpBodyLocalToWorld(4ff4900d49a97e73).(const cpBodycxx . . $ cpBody# *body, const cpVectcxx . . $ cpVect# point)
526 {
527 return cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., point);
528 }
529
530 cpVectcxx . . $ cpVect#
531 cpBodyWorldToLocalcxx . . $ cpBodyWorldToLocal(4ff4900d49a97e73).(const cpBodycxx . . $ cpBody# *body, const cpVectcxx . . $ cpVect# point)
532 {
533 return cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(cpTransformRigidInversecxx . . $ cpTransformRigidInverse(3d60147e912b055a).(body->transformcxx . . $ cpBody#transform.), point);
534 }
535
536 void
537 cpBodyApplyForceAtWorldPointcxx . . $ cpBodyApplyForceAtWorldPoint(3a5c65d925931a3e).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# force, cpVectcxx . . $ cpVect# point)
538 {
539 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
540 body->fcxx . . $ cpBody#f. = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(body->fcxx . . $ cpBody#f., force);
541
542 cpVectcxx . . $ cpVect# r = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(point, cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., body->cogcxx . . $ cpBody#cog.));
543 body->tcxx . . $ cpBody#t. += cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(r, force);
544 }
545
546 void
547 cpBodyApplyForceAtLocalPointcxx . . $ cpBodyApplyForceAtLocalPoint(3a5c65d925931a3e).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# force, cpVectcxx . . $ cpVect# point)
548 {
549 cpBodyApplyForceAtWorldPointcxx . . $ cpBodyApplyForceAtWorldPoint(3a5c65d925931a3e).(body, cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., force), cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., point));
550 }
551
552 void
553 cpBodyApplyImpulseAtWorldPointcxx . . $ cpBodyApplyImpulseAtWorldPoint(3a5c65d925931a3e).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# impulse, cpVectcxx . . $ cpVect# point)
554 {
555 cpBodyActivatecxx . . $ cpBodyActivate(92943d3541b63e72).(body);
556
557 cpVectcxx . . $ cpVect# r = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(point, cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., body->cogcxx . . $ cpBody#cog.));
558 apply_impulsecxx . . $ apply_impulse(3a5c65d925931a3e).(body, impulse, r);
559 }
560
561 void
562 cpBodyApplyImpulseAtLocalPointcxx . . $ cpBodyApplyImpulseAtLocalPoint(3a5c65d925931a3e).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# impulse, cpVectcxx . . $ cpVect# point)
563 {
564 cpBodyApplyImpulseAtWorldPointcxx . . $ cpBodyApplyImpulseAtWorldPoint(3a5c65d925931a3e).(body, cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., impulse), cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., point));
565 }
566
567 cpVectcxx . . $ cpVect#
568 cpBodyGetVelocityAtLocalPointcxx . . $ cpBodyGetVelocityAtLocalPoint(4ff4900d49a97e73).(const cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# point)
569 {
570 cpVectcxx . . $ cpVect# r = cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(point, body->cogcxx . . $ cpBody#cog.));
571 return cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(body->vcxx . . $ cpBody#v., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(cpvperpcxx . . $ cpvperp(e06142a62109ae08).(r), body->wcxx . . $ cpBody#w.));
572 }
573
574 cpVectcxx . . $ cpVect#
575 cpBodyGetVelocityAtWorldPointcxx . . $ cpBodyGetVelocityAtWorldPoint(4ff4900d49a97e73).(const cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# point)
576 {
577 cpVectcxx . . $ cpVect# r = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(point, cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(body->transformcxx . . $ cpBody#transform., body->cogcxx . . $ cpBody#cog.));
578 return cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(body->vcxx . . $ cpBody#v., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(cpvperpcxx . . $ cpvperp(e06142a62109ae08).(r), body->wcxx . . $ cpBody#w.));
579 }
580
581 cpFloatcxx . . $ cpFloat#
582 cpBodyKineticEnergycxx . . $ cpBodyKineticEnergy(2880d7999e0b34c3).(const cpBodycxx . . $ cpBody# *body)
583 {
584 // Need to do some fudging to avoid NaNs
585 cpFloatcxx . . $ cpFloat# vsq = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(body->vcxx . . $ cpBody#v., body->vcxx . . $ cpBody#v.);
586 cpFloatcxx . . $ cpFloat# wsq = body->wcxx . . $ cpBody#w.*body->wcxx . . $ cpBody#w.;
587 return (vsq ? vsq*body->mcxx . . $ cpBody#m. : 0.0f) + (wsq ? wsq*body->icxx . . $ cpBody#i. : 0.0f);
588 }
589
590 void
591 cpBodyEachShapecxx . . $ cpBodyEachShape(222302faca60539f).(cpBodycxx . . $ cpBody# *body, cpBodyShapeIteratorFunccxx . . $ cpBodyShapeIteratorFunc# func, void *data)
592 {
593 cpShapecxx . . $ cpShape# *shape = body->shapeListcxx . . $ cpBody#shapeList.;
594 while(shape){
595 cpShapecxx . . $ cpShape# *next = shape->nextcxx . . $ cpShape#next.;
596 func(body, shape, data);
597 shape = next;
598 }
599 }
600
601 void
602 cpBodyEachConstraintcxx . . $ cpBodyEachConstraint(18e65bf19666ca5e).(cpBodycxx . . $ cpBody# *body, cpBodyConstraintIteratorFunccxx . . $ cpBodyConstraintIteratorFunc# func, void *data)
603 {
604 cpConstraintcxx . . $ cpConstraint# *constraint = body->constraintListcxx . . $ cpBody#constraintList.;
605 while(constraint){
606 cpConstraintcxx . . $ cpConstraint# *next = cpConstraintNextcxx . . $ cpConstraintNext(5c2e539fc6d7ad37).(constraint, body);
607 func(body, constraint, data);
608 constraint = next;
609 }
610 }
611
612 void
613 cpBodyEachArbitercxx . . $ cpBodyEachArbiter(d9db403274f2bd29).(cpBodycxx . . $ cpBody# *body, cpBodyArbiterIteratorFunccxx . . $ cpBodyArbiterIteratorFunc# func, void *data)
614 {
615 cpArbitercxx . . $ cpArbiter# *arb = body->arbiterListcxx . . $ cpBody#arbiterList.;
616 while(arb){
617 cpArbitercxx . . $ cpArbiter# *next = cpArbiterNextcxx . . $ cpArbiterNext(2411cd886f3cd97b).(arb, body);
618
619 cpBoolcxx . . $ cpBool# swapped = arb->swappedcxx . . $ cpArbiter#swapped.; {
620 arb->swappedcxx . . $ cpArbiter#swapped. = (body == arb->body_bcxx . . $ cpArbiter#body_b.);
621 func(body, arb, data);
622 } arb->swappedcxx . . $ cpArbiter#swapped. = swapped;
623
624 arb = next;
625 }
626 }