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 #ifndef CHIPMUNK_PRIVATE_H
22 #define CHIPMUNK_PRIVATE_H
23 #ifdef CHIPMUNK_H
24 #error Cannot include chipmunk_private.h after chipmunk.h.
25 #endif
26
27 #define CP_ALLOW_PRIVATE_ACCESS 1
28 #include "chipmunk/chipmunk.h"
29
30 #define CP_HASH_COEF (3344921057ul)
31 #define CP_HASH_PAIR(A, B) ((cpHashValue)(A)*CP_HASH_COEF ^ (cpHashValue)(B)*CP_HASH_COEF)
32
33 // TODO: Eww. Magic numbers.
34 #define MAGIC_EPSILON 1e-5
35
36
37 //MARK: cpArray
38
39 struct cpArray {
40 int num, max;
41 void **arr;
42 };
43
44 cpArray *cpArrayNew(int size);
45
46 void cpArrayFree(cpArray *arr);
47
48 void cpArrayPush(cpArray *arr, void *object);
49 void *cpArrayPop(cpArray *arr);
50 void cpArrayDeleteObj(cpArray *arr, void *obj);
51 cpBool cpArrayContains(cpArray *arr, void *ptr);
52
53 void cpArrayFreeEach(cpArray *arr, void (freeFunc)(void*));
54
55
56 //MARK: cpHashSet
57
58 typedef cpBool (*cpHashSetEqlFunc)(void *ptr, void *elt);
59 typedef void *(*cpHashSetTransFunc)(void *ptr, void *data);
60
61 cpHashSet *cpHashSetNew(int size, cpHashSetEqlFunc eqlFunc);
62 void cpHashSetSetDefaultValue(cpHashSet *set, void *default_value);
63
64 void cpHashSetFree(cpHashSet *set);
65
66 int cpHashSetCount(cpHashSet *set);
67 void *cpHashSetInsert(cpHashSet *set, cpHashValue hash, void *ptr, cpHashSetTransFunc trans, void *data);
68 void *cpHashSetRemove(cpHashSet *set, cpHashValue hash, void *ptr);
69 void *cpHashSetFind(cpHashSet *set, cpHashValue hash, void *ptr);
70
71 typedef void (*cpHashSetIteratorFunc)(void *elt, void *data);
72 void cpHashSetEach(cpHashSet *set, cpHashSetIteratorFunc func, void *data);
73
74 typedef cpBool (*cpHashSetFilterFunc)(void *elt, void *data);
75 void cpHashSetFilter(cpHashSet *set, cpHashSetFilterFunc func, void *data);
76
77
78 //MARK: Bodies
79
80 struct cpBody {
81 // Integration functions
82 cpBodyVelocityFunc velocity_func;
83 cpBodyPositionFunc position_func;
84
85 // mass and it's inverse
86 cpFloat m;
87 cpFloat m_inv;
88
89 // moment of inertia and it's inverse
90 cpFloat i;
91 cpFloat i_inv;
92
93 // center of gravity
94 cpVect cog;
95
96 // position, velocity, force
97 cpVect p;
98 cpVect v;
99 cpVect f;
100
101 // Angle, angular velocity, torque (radians)
102 cpFloat a;
103 cpFloat w;
104 cpFloat t;
105
106 cpTransform transform;
107
108 cpDataPointer userData;
109
110 // "pseudo-velocities" used for eliminating overlap.
111 // Erin Catto has some papers that talk about what these are.
112 cpVect v_bias;
113 cpFloat w_bias;
114
115 cpSpace *space;
116
117 cpShape *shapeList;
118 cpArbiter *arbiterList;
119 cpConstraint *constraintList;
120
121 struct {
122 cpBody *root;
123 cpBody *next;
124 cpFloat idleTime;
125 } sleeping;
126 };
127
128 void cpBodyAddShape(cpBody *body, cpShape *shape);
129 void cpBodyRemoveShape(cpBody *body, cpShape *shape);
130
131 //void cpBodyAccumulateMassForShape(cpBody *body, cpShape *shape);
132 void cpBodyAccumulateMassFromShapes(cpBody *body);
133
134 void cpBodyRemoveConstraint(cpBody *body, cpConstraint *constraint);
135
136
137 //MARK: Spatial Index Functions
138
139 cpSpatialIndex *cpSpatialIndexInit(cpSpatialIndex *index, cpSpatialIndexClass *klass, cpSpatialIndexBBFunc bbfunc, cpSpatialIndex *staticIndex);
140
141
142 //MARK: Arbiters
143
144 enum cpArbiterState {
145 // Arbiter is active and its the first collision.
146 CP_ARBITER_STATE_FIRST_COLLISION,
147 // Arbiter is active and its not the first collision.
148 CP_ARBITER_STATE_NORMAL,
149 // Collision has been explicitly ignored.
150 // Either by returning false from a begin collision handler or calling cpArbiterIgnore().
151 CP_ARBITER_STATE_IGNORE,
152 // Collison is no longer active. A space will cache an arbiter for up to cpSpace.collisionPersistence more steps.
153 CP_ARBITER_STATE_CACHED,
154 // Collison arbiter is invalid because one of the shapes was removed.
155 CP_ARBITER_STATE_INVALIDATED,
156 };
157
158 struct cpArbiterThread {
159 struct cpArbiter *next, *prev;
160 };
161
162 struct cpContact {
163 cpVect r1, r2;
164
165 cpFloat nMass, tMass;
166 cpFloat bounce; // TODO: look for an alternate bounce solution.
167
168 cpFloat jnAcc, jtAcc, jBias;
169 cpFloat bias;
170
171 cpHashValue hash;
172 };
173
174 struct cpCollisionInfo {
175 const cpShape *a, *b;
176 cpCollisionID id;
177
178 cpVect n;
179
180 int count;
181 // TODO Should this be a unique struct type?
182 struct cpContact *arr;
183 };
184
185 struct cpArbiter {
186 cpFloat e;
187 cpFloat u;
188 cpVect surface_vr;
189
190 cpDataPointer data;
191
192 const cpShape *a, *b;
193 cpBody *body_a, *body_b;
194 struct cpArbiterThread thread_a, thread_b;
195
196 int count;
197 struct cpContact *contacts;
198 cpVect n;
199
200 // Regular, wildcard A and wildcard B collision handlers.
201 cpCollisionHandler *handler, *handlerA, *handlerB;
202 cpBool swapped;
203
204 cpTimestamp stamp;
205 enum cpArbiterState state;
206 };
207
208 cpArbiter* cpArbiterInit(cpArbiter *arb, cpShape *a, cpShape *b);
209
210 static inline struct cpArbiterThread *
cpArbiterThreadForBody(cpArbiter * arb,cpBody * body)211 cpArbiterThreadForBody(cpArbiter *arb, cpBody *body)
212 {
213 return (arb->body_a == body ? &arb->thread_a : &arb->thread_b);
214 }
215
216 void cpArbiterUnthread(cpArbiter *arb);
217
218 void cpArbiterUpdate(cpArbiter *arb, struct cpCollisionInfo *info, cpSpace *space);
219 void cpArbiterPreStep(cpArbiter *arb, cpFloat dt, cpFloat bias, cpFloat slop);
220 void cpArbiterApplyCachedImpulse(cpArbiter *arb, cpFloat dt_coef);
221 void cpArbiterApplyImpulse(cpArbiter *arb);
222
223
224 //MARK: Shapes/Collisions
225
226 struct cpShapeMassInfo {
227 cpFloat m;
228 cpFloat i;
229 cpVect cog;
230 cpFloat area;
231 };
232
233 typedef enum cpShapeType{
234 CP_CIRCLE_SHAPE,
235 CP_SEGMENT_SHAPE,
236 CP_POLY_SHAPE,
237 CP_NUM_SHAPES
238 } cpShapeType;
239
240 typedef cpBB (*cpShapeCacheDataImpl)(cpShape *shape, cpTransform transform);
241 typedef void (*cpShapeDestroyImpl)(cpShape *shape);
242 typedef void (*cpShapePointQueryImpl)(const cpShape *shape, cpVect p, cpPointQueryInfo *info);
243 typedef void (*cpShapeSegmentQueryImpl)(const cpShape *shape, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info);
244
245 typedef struct cpShapeClass cpShapeClass;
246
247 struct cpShapeClass {
248 cpShapeType type;
249
250 cpShapeCacheDataImpl cacheData;
251 cpShapeDestroyImpl destroy;
252 cpShapePointQueryImpl pointQuery;
253 cpShapeSegmentQueryImpl segmentQuery;
254 };
255
256 struct cpShape {
257 const cpShapeClass *klass;
258
259 cpSpace *space;
260 cpBody *body;
261 struct cpShapeMassInfo massInfo;
262 cpBB bb;
263
264 cpBool sensor;
265
266 cpFloat e;
267 cpFloat u;
268 cpVect surfaceV;
269
270 cpDataPointer userData;
271
272 cpCollisionType type;
273 cpShapeFilter filter;
274
275 cpShape *next;
276 cpShape *prev;
277
278 cpHashValue hashid;
279 };
280
281 struct cpCircleShape {
282 cpShape shape;
283
284 cpVect c, tc;
285 cpFloat r;
286 };
287
288 struct cpSegmentShape {
289 cpShape shape;
290
291 cpVect a, b, n;
292 cpVect ta, tb, tn;
293 cpFloat r;
294
295 cpVect a_tangent, b_tangent;
296 };
297
298 struct cpSplittingPlane {
299 cpVect v0, n;
300 };
301
302 #define CP_POLY_SHAPE_INLINE_ALLOC 6
303
304 struct cpPolyShape {
305 cpShape shape;
306
307 cpFloat r;
308
309 int count;
310 // The untransformed planes are appended at the end of the transformed planes.
311 struct cpSplittingPlane *planes;
312
313 // Allocate a small number of splitting planes internally for simple poly.
314 struct cpSplittingPlane _planes[2*CP_POLY_SHAPE_INLINE_ALLOC];
315 };
316
317 cpShape *cpShapeInit(cpShape *shape, const cpShapeClass *klass, cpBody *body, struct cpShapeMassInfo massInfo);
318
319 static inline cpBool
cpShapeActive(cpShape * shape)320 cpShapeActive(cpShape *shape)
321 {
322 // checks if the shape is added to a shape list.
323 // TODO could this just check the space now?
324 return (shape->prev || (shape->body && shape->body->shapeList == shape));
325 }
326
327 // Note: This function returns contact points with r1/r2 in absolute coordinates, not body relative.
328 struct cpCollisionInfo cpCollide(const cpShape *a, const cpShape *b, cpCollisionID id, struct cpContact *contacts);
329
330 static inline void
CircleSegmentQuery(cpShape * shape,cpVect center,cpFloat r1,cpVect a,cpVect b,cpFloat r2,cpSegmentQueryInfo * info)331 CircleSegmentQuery(cpShape *shape, cpVect center, cpFloat r1, cpVect a, cpVect b, cpFloat r2, cpSegmentQueryInfo *info)
332 {
333 cpVect da = cpvsub(a, center);
334 cpVect db = cpvsub(b, center);
335 cpFloat rsum = r1 + r2;
336
337 cpFloat qa = cpvdot(da, da) - 2.0f*cpvdot(da, db) + cpvdot(db, db);
338 cpFloat qb = cpvdot(da, db) - cpvdot(da, da);
339 cpFloat det = qb*qb - qa*(cpvdot(da, da) - rsum*rsum);
340
341 if(det >= 0.0f){
342 cpFloat t = (-qb - cpfsqrt(det))/(qa);
343 if(0.0f<= t && t <= 1.0f){
344 cpVect n = cpvnormalize(cpvlerp(da, db, t));
345
346 info->shape = shape;
347 info->point = cpvsub(cpvlerp(a, b, t), cpvmult(n, r2));
348 info->normal = n;
349 info->alpha = t;
350 }
351 }
352 }
353
354 static inline cpBool
cpShapeFilterReject(cpShapeFilter a,cpShapeFilter b)355 cpShapeFilterReject(cpShapeFilter a, cpShapeFilter b)
356 {
357 // Reject the collision if:
358 return (
359 // They are in the same non-zero group.
360 (a.group != 0 && a.group == b.group) ||
361 // One of the category/mask combinations fails.
362 (a.categories & b.mask) == 0 ||
363 (b.categories & a.mask) == 0
364 );
365 }
366
367 void cpLoopIndexes(const cpVect *verts, int count, int *start, int *end);
368
369
370 //MARK: Constraints
371 // TODO naming conventions here
372
373 typedef void (*cpConstraintPreStepImpl)(cpConstraint *constraint, cpFloat dt);
374 typedef void (*cpConstraintApplyCachedImpulseImpl)(cpConstraint *constraint, cpFloat dt_coef);
375 typedef void (*cpConstraintApplyImpulseImpl)(cpConstraint *constraint, cpFloat dt);
376 typedef cpFloat (*cpConstraintGetImpulseImpl)(cpConstraint *constraint);
377
378 typedef struct cpConstraintClass {
379 cpConstraintPreStepImpl preStep;
380 cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
381 cpConstraintApplyImpulseImpl applyImpulse;
382 cpConstraintGetImpulseImpl getImpulse;
383 } cpConstraintClass;
384
385 struct cpConstraint {
386 const cpConstraintClass *klass;
387
388 cpSpace *space;
389
390 cpBody *a, *b;
391 cpConstraint *next_a, *next_b;
392
393 cpFloat maxForce;
394 cpFloat errorBias;
395 cpFloat maxBias;
396
397 cpBool collideBodies;
398
399 cpConstraintPreSolveFunc preSolve;
400 cpConstraintPostSolveFunc postSolve;
401
402 cpDataPointer userData;
403 };
404
405 struct cpPinJoint {
406 cpConstraint constraint;
407 cpVect anchorA, anchorB;
408 cpFloat dist;
409
410 cpVect r1, r2;
411 cpVect n;
412 cpFloat nMass;
413
414 cpFloat jnAcc;
415 cpFloat bias;
416 };
417
418 struct cpSlideJoint {
419 cpConstraint constraint;
420 cpVect anchorA, anchorB;
421 cpFloat min, max;
422
423 cpVect r1, r2;
424 cpVect n;
425 cpFloat nMass;
426
427 cpFloat jnAcc;
428 cpFloat bias;
429 };
430
431 struct cpPivotJoint {
432 cpConstraint constraint;
433 cpVect anchorA, anchorB;
434
435 cpVect r1, r2;
436 cpMat2x2 k;
437
438 cpVect jAcc;
439 cpVect bias;
440 };
441
442 struct cpGrooveJoint {
443 cpConstraint constraint;
444 cpVect grv_n, grv_a, grv_b;
445 cpVect anchorB;
446
447 cpVect grv_tn;
448 cpFloat clamp;
449 cpVect r1, r2;
450 cpMat2x2 k;
451
452 cpVect jAcc;
453 cpVect bias;
454 };
455
456 struct cpDampedSpring {
457 cpConstraint constraint;
458 cpVect anchorA, anchorB;
459 cpFloat restLength;
460 cpFloat stiffness;
461 cpFloat damping;
462 cpDampedSpringForceFunc springForceFunc;
463
464 cpFloat target_vrn;
465 cpFloat v_coef;
466
467 cpVect r1, r2;
468 cpFloat nMass;
469 cpVect n;
470
471 cpFloat jAcc;
472 };
473
474 struct cpDampedRotarySpring {
475 cpConstraint constraint;
476 cpFloat restAngle;
477 cpFloat stiffness;
478 cpFloat damping;
479 cpDampedRotarySpringTorqueFunc springTorqueFunc;
480
481 cpFloat target_wrn;
482 cpFloat w_coef;
483
484 cpFloat iSum;
485 cpFloat jAcc;
486 };
487
488 struct cpRotaryLimitJoint {
489 cpConstraint constraint;
490 cpFloat min, max;
491
492 cpFloat iSum;
493
494 cpFloat bias;
495 cpFloat jAcc;
496 };
497
498 struct cpRatchetJoint {
499 cpConstraint constraint;
500 cpFloat angle, phase, ratchet;
501
502 cpFloat iSum;
503
504 cpFloat bias;
505 cpFloat jAcc;
506 };
507
508 struct cpGearJoint {
509 cpConstraint constraint;
510 cpFloat phase, ratio;
511 cpFloat ratio_inv;
512
513 cpFloat iSum;
514
515 cpFloat bias;
516 cpFloat jAcc;
517 };
518
519 struct cpSimpleMotor {
520 cpConstraint constraint;
521 cpFloat rate;
522
523 cpFloat iSum;
524
525 cpFloat jAcc;
526 };
527
528 void cpConstraintInit(cpConstraint *constraint, const struct cpConstraintClass *klass, cpBody *a, cpBody *b);
529
530 static inline void
cpConstraintActivateBodies(cpConstraint * constraint)531 cpConstraintActivateBodies(cpConstraint *constraint)
532 {
533 cpBody *a = constraint->a; cpBodyActivate(a);
534 cpBody *b = constraint->b; cpBodyActivate(b);
535 }
536
537 static inline cpVect
relative_velocity(cpBody * a,cpBody * b,cpVect r1,cpVect r2)538 relative_velocity(cpBody *a, cpBody *b, cpVect r1, cpVect r2){
539 cpVect v1_sum = cpvadd(a->CP_PRIVATE(v), cpvmult(cpvperp(r1), a->CP_PRIVATE(w)));
540 cpVect v2_sum = cpvadd(b->CP_PRIVATE(v), cpvmult(cpvperp(r2), b->CP_PRIVATE(w)));
541
542 return cpvsub(v2_sum, v1_sum);
543 }
544
545 static inline cpFloat
normal_relative_velocity(cpBody * a,cpBody * b,cpVect r1,cpVect r2,cpVect n)546 normal_relative_velocity(cpBody *a, cpBody *b, cpVect r1, cpVect r2, cpVect n){
547 return cpvdot(relative_velocity(a, b, r1, r2), n);
548 }
549
550 static inline void
apply_impulse(cpBody * body,cpVect j,cpVect r)551 apply_impulse(cpBody *body, cpVect j, cpVect r){
552 body->CP_PRIVATE(v) = cpvadd(body->CP_PRIVATE(v), cpvmult(j, body->CP_PRIVATE(m_inv)));
553 body->CP_PRIVATE(w) += body->CP_PRIVATE(i_inv)*cpvcross(r, j);
554 }
555
556 static inline void
apply_impulses(cpBody * a,cpBody * b,cpVect r1,cpVect r2,cpVect j)557 apply_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j)
558 {
559 apply_impulse(a, cpvneg(j), r1);
560 apply_impulse(b, j, r2);
561 }
562
563 static inline void
apply_bias_impulse(cpBody * body,cpVect j,cpVect r)564 apply_bias_impulse(cpBody *body, cpVect j, cpVect r)
565 {
566 body->CP_PRIVATE(v_bias) = cpvadd(body->CP_PRIVATE(v_bias), cpvmult(j, body->CP_PRIVATE(m_inv)));
567 body->CP_PRIVATE(w_bias) += body->CP_PRIVATE(i_inv)*cpvcross(r, j);
568 }
569
570 static inline void
apply_bias_impulses(cpBody * a,cpBody * b,cpVect r1,cpVect r2,cpVect j)571 apply_bias_impulses(cpBody *a , cpBody *b, cpVect r1, cpVect r2, cpVect j)
572 {
573 apply_bias_impulse(a, cpvneg(j), r1);
574 apply_bias_impulse(b, j, r2);
575 }
576
577 static inline cpFloat
k_scalar_body(cpBody * body,cpVect r,cpVect n)578 k_scalar_body(cpBody *body, cpVect r, cpVect n)
579 {
580 cpFloat rcn = cpvcross(r, n);
581 return body->CP_PRIVATE(m_inv) + body->CP_PRIVATE(i_inv)*rcn*rcn;
582 }
583
584 static inline cpFloat
k_scalar(cpBody * a,cpBody * b,cpVect r1,cpVect r2,cpVect n)585 k_scalar(cpBody *a, cpBody *b, cpVect r1, cpVect r2, cpVect n)
586 {
587 cpFloat value = k_scalar_body(a, r1, n) + k_scalar_body(b, r2, n);
588 cpAssertSoft(value != 0.0, "Unsolvable collision or constraint.");
589
590 return value;
591 }
592
593 static inline cpMat2x2
k_tensor(cpBody * a,cpBody * b,cpVect r1,cpVect r2)594 k_tensor(cpBody *a, cpBody *b, cpVect r1, cpVect r2)
595 {
596 cpFloat m_sum = a->CP_PRIVATE(m_inv) + b->CP_PRIVATE(m_inv);
597
598 // start with Identity*m_sum
599 cpFloat k11 = m_sum, k12 = 0.0f;
600 cpFloat k21 = 0.0f, k22 = m_sum;
601
602 // add the influence from r1
603 cpFloat a_i_inv = a->CP_PRIVATE(i_inv);
604 cpFloat r1xsq = r1.x * r1.x * a_i_inv;
605 cpFloat r1ysq = r1.y * r1.y * a_i_inv;
606 cpFloat r1nxy = -r1.x * r1.y * a_i_inv;
607 k11 += r1ysq; k12 += r1nxy;
608 k21 += r1nxy; k22 += r1xsq;
609
610 // add the influnce from r2
611 cpFloat b_i_inv = b->CP_PRIVATE(i_inv);
612 cpFloat r2xsq = r2.x * r2.x * b_i_inv;
613 cpFloat r2ysq = r2.y * r2.y * b_i_inv;
614 cpFloat r2nxy = -r2.x * r2.y * b_i_inv;
615 k11 += r2ysq; k12 += r2nxy;
616 k21 += r2nxy; k22 += r2xsq;
617
618 // invert
619 cpFloat det = k11*k22 - k12*k21;
620 cpAssertSoft(det != 0.0, "Unsolvable constraint.");
621
622 cpFloat det_inv = 1.0f/det;
623 return cpMat2x2New(
624 k22*det_inv, -k12*det_inv,
625 -k21*det_inv, k11*det_inv
626 );
627 }
628
629 static inline cpFloat
bias_coef(cpFloat errorBias,cpFloat dt)630 bias_coef(cpFloat errorBias, cpFloat dt)
631 {
632 return 1.0f - cpfpow(errorBias, dt);
633 }
634
635
636 //MARK: Spaces
637
638 typedef struct cpContactBufferHeader cpContactBufferHeader;
639 typedef void (*cpSpaceArbiterApplyImpulseFunc)(cpArbiter *arb);
640
641 struct cpSpace {
642 int iterations;
643
644 cpVect gravity;
645 cpFloat damping;
646
647 cpFloat idleSpeedThreshold;
648 cpFloat sleepTimeThreshold;
649
650 cpFloat collisionSlop;
651 cpFloat collisionBias;
652 cpTimestamp collisionPersistence;
653
654 cpDataPointer userData;
655
656 cpTimestamp stamp;
657 cpFloat curr_dt;
658
659 cpArray *dynamicBodies;
660 cpArray *staticBodies;
661 cpArray *rousedBodies;
662 cpArray *sleepingComponents;
663
664 cpHashValue shapeIDCounter;
665 cpSpatialIndex *staticShapes;
666 cpSpatialIndex *dynamicShapes;
667
668 cpArray *constraints;
669
670 cpArray *arbiters;
671 cpContactBufferHeader *contactBuffersHead;
672 cpHashSet *cachedArbiters;
673 cpArray *pooledArbiters;
674
675 cpArray *allocatedBuffers;
676 unsigned int locked;
677
678 cpBool usesWildcards;
679 cpHashSet *collisionHandlers;
680 cpCollisionHandler defaultHandler;
681
682 cpBool skipPostStep;
683 cpArray *postStepCallbacks;
684
685 cpBody *staticBody;
686 cpBody _staticBody;
687 };
688
689 #define cpAssertSpaceUnlocked(space) \
690 cpAssertHard(!space->locked, \
691 "This operation cannot be done safely during a call to cpSpaceStep() or during a query. " \
692 "Put these calls into a post-step callback." \
693 );
694
695 void cpSpaceSetStaticBody(cpSpace *space, cpBody *body);
696
697 extern cpCollisionHandler cpCollisionHandlerDoNothing;
698
699 void cpSpaceProcessComponents(cpSpace *space, cpFloat dt);
700
701 void cpSpacePushFreshContactBuffer(cpSpace *space);
702 struct cpContact *cpContactBufferGetArray(cpSpace *space);
703 void cpSpacePushContacts(cpSpace *space, int count);
704
705 typedef struct cpPostStepCallback {
706 cpPostStepFunc func;
707 void *key;
708 void *data;
709 } cpPostStepCallback;
710
711 cpPostStepCallback *cpSpaceGetPostStepCallback(cpSpace *space, void *key);
712
713 cpBool cpSpaceArbiterSetFilter(cpArbiter *arb, cpSpace *space);
714 void cpSpaceFilterArbiters(cpSpace *space, cpBody *body, cpShape *filter);
715
716 void cpSpaceActivateBody(cpSpace *space, cpBody *body);
717 void cpSpaceLock(cpSpace *space);
718 void cpSpaceUnlock(cpSpace *space, cpBool runPostStep);
719
720 static inline void
cpSpaceUncacheArbiter(cpSpace * space,cpArbiter * arb)721 cpSpaceUncacheArbiter(cpSpace *space, cpArbiter *arb)
722 {
723 const cpShape *a = arb->a, *b = arb->b;
724 const cpShape *shape_pair[] = {a, b};
725 cpHashValue arbHashID = CP_HASH_PAIR((cpHashValue)a, (cpHashValue)b);
726 cpHashSetRemove(space->cachedArbiters, arbHashID, shape_pair);
727 cpArrayDeleteObj(space->arbiters, arb);
728 }
729
730 static inline cpArray *
cpSpaceArrayForBodyType(cpSpace * space,cpBodyType type)731 cpSpaceArrayForBodyType(cpSpace *space, cpBodyType type)
732 {
733 return (type == CP_BODY_TYPE_STATIC ? space->staticBodies : space->dynamicBodies);
734 }
735
736 void cpShapeUpdateFunc(cpShape *shape, void *unused);
737 cpCollisionID cpSpaceCollideShapes(cpShape *a, cpShape *b, cpCollisionID id, cpSpace *space);
738
739
740 //MARK: Foreach loops
741
742 static inline cpConstraint *
cpConstraintNext(cpConstraint * node,cpBody * body)743 cpConstraintNext(cpConstraint *node, cpBody *body)
744 {
745 return (node->a == body ? node->next_a : node->next_b);
746 }
747
748 #define CP_BODY_FOREACH_CONSTRAINT(bdy, var)\
749 for(cpConstraint *var = bdy->constraintList; var; var = cpConstraintNext(var, bdy))
750
751 static inline cpArbiter *
cpArbiterNext(cpArbiter * node,cpBody * body)752 cpArbiterNext(cpArbiter *node, cpBody *body)
753 {
754 return (node->body_a == body ? node->thread_a.next : node->thread_b.next);
755 }
756
757 #define CP_BODY_FOREACH_ARBITER(bdy, var)\
758 for(cpArbiter *var = bdy->arbiterList; var; var = cpArbiterNext(var, bdy))
759
760 #define CP_BODY_FOREACH_SHAPE(body, var)\
761 for(cpShape *var = body->shapeList; var; var = var->next)
762
763 #define CP_BODY_FOREACH_COMPONENT(root, var)\
764 for(cpBody *var = root; var; var = var->sleeping.next)
765
766 #endif
767