chipmunk 5.3.4.5 → 6.1.3.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/ext/chipmunk/chipmunk.c +199 -28
  2. data/ext/chipmunk/chipmunk.h +123 -68
  3. data/ext/chipmunk/chipmunk_ffi.h +129 -11
  4. data/ext/chipmunk/chipmunk_private.h +232 -16
  5. data/ext/chipmunk/chipmunk_types.h +94 -30
  6. data/ext/chipmunk/chipmunk_unsafe.h +12 -3
  7. data/ext/chipmunk/constraints/cpConstraint.h +90 -34
  8. data/ext/chipmunk/{cpDampedRotarySpring.h → constraints/cpDampedRotarySpring.h} +18 -8
  9. data/ext/chipmunk/{cpDampedSpring.h → constraints/cpDampedSpring.h} +27 -16
  10. data/ext/chipmunk/constraints/cpGearJoint.h +17 -7
  11. data/ext/chipmunk/constraints/cpGrooveJoint.h +19 -10
  12. data/ext/chipmunk/constraints/cpPinJoint.h +17 -8
  13. data/ext/chipmunk/constraints/cpPivotJoint.h +18 -9
  14. data/ext/chipmunk/constraints/cpRatchetJoint.h +17 -8
  15. data/ext/chipmunk/constraints/cpRotaryLimitJoint.h +16 -7
  16. data/ext/chipmunk/{cpSimpleMotor.h → constraints/cpSimpleMotor.h} +15 -6
  17. data/ext/chipmunk/constraints/cpSlideJoint.h +18 -9
  18. data/ext/chipmunk/constraints/util.h +36 -44
  19. data/ext/chipmunk/cpArbiter.c +159 -94
  20. data/ext/chipmunk/cpArbiter.h +135 -129
  21. data/ext/chipmunk/cpArray.c +37 -56
  22. data/ext/chipmunk/cpBB.c +1 -12
  23. data/ext/chipmunk/cpBB.h +80 -18
  24. data/ext/chipmunk/cpBBTree.c +891 -0
  25. data/ext/chipmunk/cpBody.c +185 -47
  26. data/ext/chipmunk/cpBody.h +156 -124
  27. data/ext/chipmunk/cpCollision.c +126 -115
  28. data/ext/chipmunk/cpConstraint.c +10 -6
  29. data/ext/chipmunk/cpDampedRotarySpring.c +26 -17
  30. data/ext/chipmunk/cpDampedSpring.c +25 -18
  31. data/ext/chipmunk/cpGearJoint.c +23 -17
  32. data/ext/chipmunk/cpGrooveJoint.c +26 -22
  33. data/ext/chipmunk/cpHashSet.c +51 -51
  34. data/ext/chipmunk/cpPinJoint.c +26 -19
  35. data/ext/chipmunk/cpPivotJoint.c +23 -19
  36. data/ext/chipmunk/cpPolyShape.c +93 -69
  37. data/ext/chipmunk/cpPolyShape.h +33 -69
  38. data/ext/chipmunk/cpRatchetJoint.c +26 -21
  39. data/ext/chipmunk/cpRotaryLimitJoint.c +28 -22
  40. data/ext/chipmunk/cpShape.c +122 -133
  41. data/ext/chipmunk/cpShape.h +146 -95
  42. data/ext/chipmunk/cpSimpleMotor.c +24 -17
  43. data/ext/chipmunk/cpSlideJoint.c +28 -26
  44. data/ext/chipmunk/cpSpace.c +251 -196
  45. data/ext/chipmunk/cpSpace.h +173 -103
  46. data/ext/chipmunk/cpSpaceComponent.c +236 -159
  47. data/ext/chipmunk/cpSpaceHash.c +259 -159
  48. data/ext/chipmunk/cpSpaceQuery.c +127 -59
  49. data/ext/chipmunk/cpSpaceStep.c +235 -197
  50. data/ext/chipmunk/cpSpatialIndex.c +69 -0
  51. data/ext/chipmunk/cpSpatialIndex.h +227 -0
  52. data/ext/chipmunk/cpSweep1D.c +254 -0
  53. data/ext/chipmunk/cpVect.c +11 -26
  54. data/ext/chipmunk/cpVect.h +76 -71
  55. data/ext/chipmunk/extconf.rb +4 -31
  56. data/ext/chipmunk/prime.h +1 -1
  57. data/ext/chipmunk/rb_chipmunk.c +36 -45
  58. data/ext/chipmunk/rb_chipmunk.h +6 -3
  59. data/ext/chipmunk/rb_cpArbiter.c +2 -2
  60. data/ext/chipmunk/rb_cpBB.c +116 -35
  61. data/ext/chipmunk/rb_cpBody.c +5 -12
  62. data/ext/chipmunk/rb_cpConstraint.c +144 -9
  63. data/ext/chipmunk/rb_cpShape.c +69 -78
  64. data/ext/chipmunk/rb_cpSpace.c +81 -76
  65. metadata +61 -61
  66. data/LICENSE +0 -22
  67. data/README +0 -110
  68. data/Rakefile +0 -102
  69. data/ext/chipmunk/cpArray.h +0 -49
  70. data/ext/chipmunk/cpCollision.h +0 -28
  71. data/ext/chipmunk/cpHashSet.h +0 -82
  72. data/ext/chipmunk/cpSpaceHash.h +0 -110
  73. data/lib/chipmunk.rb +0 -194
@@ -1,3 +1,5 @@
1
+ #ifdef CHIPMUNK_FFI
2
+
1
3
  // Create non static inlined copies of Chipmunk functions, useful for working with dynamic FFIs
2
4
  // This file should only be included in chipmunk.c
3
5
 
@@ -11,6 +13,9 @@
11
13
  #define MAKE_REF(name) __typeof__(name) *_##name = name
12
14
  #endif
13
15
 
16
+ #define MAKE_PROPERTIES_REF(struct, property) \
17
+ MAKE_REF(struct##Get##property); MAKE_REF(struct##Set##property)
18
+
14
19
  MAKE_REF(cpv); // makes a variable named _cpv that contains the function pointer for cpv()
15
20
  MAKE_REF(cpveql);
16
21
  MAKE_REF(cpvadd);
@@ -22,9 +27,12 @@ MAKE_REF(cpvcross);
22
27
  MAKE_REF(cpvperp);
23
28
  MAKE_REF(cpvrperp);
24
29
  MAKE_REF(cpvproject);
30
+ MAKE_REF(cpvforangle);
31
+ MAKE_REF(cpvtoangle);
25
32
  MAKE_REF(cpvrotate);
26
33
  MAKE_REF(cpvunrotate);
27
34
  MAKE_REF(cpvlengthsq);
35
+ MAKE_REF(cpvlength);
28
36
  MAKE_REF(cpvlerp);
29
37
  MAKE_REF(cpvnormalize);
30
38
  MAKE_REF(cpvnormalize_safe);
@@ -34,26 +42,136 @@ MAKE_REF(cpvdist);
34
42
  MAKE_REF(cpvdistsq);
35
43
  MAKE_REF(cpvnear);
36
44
 
45
+ MAKE_REF(cpfmax);
46
+ MAKE_REF(cpfmin);
47
+ MAKE_REF(cpfabs);
48
+ MAKE_REF(cpfclamp);
49
+ MAKE_REF(cpflerp);
50
+ MAKE_REF(cpflerpconst);
51
+
37
52
  MAKE_REF(cpBBNew);
38
- MAKE_REF(cpBBintersects);
39
- MAKE_REF(cpBBcontainsBB);
40
- MAKE_REF(cpBBcontainsVect);
41
- MAKE_REF(cpBBmerge);
42
- MAKE_REF(cpBBexpand);
53
+ MAKE_REF(cpBBNewForCircle);
54
+ MAKE_REF(cpBBIntersects);
55
+ MAKE_REF(cpBBContainsBB);
56
+ MAKE_REF(cpBBContainsVect);
57
+ MAKE_REF(cpBBMerge);
58
+ MAKE_REF(cpBBExpand);
59
+ MAKE_REF(cpBBArea);
60
+ MAKE_REF(cpBBMergedArea);
61
+ MAKE_REF(cpBBSegmentQuery);
62
+ MAKE_REF(cpBBIntersectsSegment);
63
+ MAKE_REF(cpBBClampVect);
43
64
 
44
- MAKE_REF(cpBodyWorld2Local);
45
- MAKE_REF(cpBodyLocal2World);
46
- MAKE_REF(cpBodyApplyImpulse);
65
+ MAKE_REF(cpBodyGetMass);
66
+ MAKE_REF(cpBodyGetMoment);
67
+ MAKE_REF(cpBodyGetPos);
68
+ MAKE_REF(cpBodyGetAngle);
69
+ MAKE_REF(cpBodyGetRot);
70
+ MAKE_PROPERTIES_REF(cpBody, Vel);
71
+ MAKE_PROPERTIES_REF(cpBody, Force);
72
+ MAKE_PROPERTIES_REF(cpBody, AngVel);
73
+ MAKE_PROPERTIES_REF(cpBody, Torque);
74
+ MAKE_PROPERTIES_REF(cpBody, VelLimit);
75
+ MAKE_PROPERTIES_REF(cpBody, AngVelLimit);
76
+ MAKE_PROPERTIES_REF(cpBody, UserData);
47
77
  MAKE_REF(cpBodyIsSleeping);
78
+ MAKE_REF(cpBodyIsStatic);
48
79
  MAKE_REF(cpBodyIsRogue);
80
+ MAKE_REF(cpBodyLocal2World);
81
+ MAKE_REF(cpBodyWorld2Local);
49
82
  MAKE_REF(cpBodyKineticEnergy);
50
83
 
51
- MAKE_REF(cpArbiterIsFirstContact);
84
+ MAKE_REF(cpShapeGetBB);
85
+ MAKE_PROPERTIES_REF(cpShape, Body);
86
+ MAKE_PROPERTIES_REF(cpShape, Sensor);
87
+ MAKE_PROPERTIES_REF(cpShape, Elasticity);
88
+ MAKE_PROPERTIES_REF(cpShape, Friction);
89
+ MAKE_PROPERTIES_REF(cpShape, SurfaceVelocity);
90
+ MAKE_PROPERTIES_REF(cpShape, UserData);
91
+ MAKE_PROPERTIES_REF(cpShape, CollisionType);
92
+ MAKE_PROPERTIES_REF(cpShape, Group);
93
+ MAKE_PROPERTIES_REF(cpShape, Layers);
94
+
52
95
  MAKE_REF(cpArbiterGetShapes);
53
- MAKE_REF(cpArbiterGetNormal);
54
- MAKE_REF(cpArbiterGetPoint);
96
+ MAKE_REF(cpArbiterGetBodies);
97
+ MAKE_REF(cpArbiterIsFirstContact);
98
+ MAKE_REF(cpArbiterGetCount);
55
99
 
100
+ MAKE_REF(cpConstraintGetA);
101
+ MAKE_REF(cpConstraintGetB);
102
+ MAKE_PROPERTIES_REF(cpConstraint, MaxForce);
103
+ MAKE_PROPERTIES_REF(cpConstraint, ErrorBias);
104
+ MAKE_PROPERTIES_REF(cpConstraint, MaxBias);
105
+ MAKE_PROPERTIES_REF(cpConstraint, UserData);
56
106
  MAKE_REF(cpConstraintGetImpulse);
57
107
 
108
+ MAKE_PROPERTIES_REF(cpDampedRotarySpring, RestAngle);
109
+ MAKE_PROPERTIES_REF(cpDampedRotarySpring, Stiffness);
110
+ MAKE_PROPERTIES_REF(cpDampedRotarySpring, Damping);
111
+ //MAKE_PROPERTIES_REF(cpDampedRotarySpring, SpringTorqueFunc);
112
+
113
+ MAKE_PROPERTIES_REF(cpDampedSpring, Anchr1);
114
+ MAKE_PROPERTIES_REF(cpDampedSpring, Anchr2);
115
+ MAKE_PROPERTIES_REF(cpDampedSpring, RestLength);
116
+ MAKE_PROPERTIES_REF(cpDampedSpring, Stiffness);
117
+ MAKE_PROPERTIES_REF(cpDampedSpring, Damping);
118
+ //MAKE_PROPERTIES_REF(cpDampedSpring, SpringForceFunc);
119
+
120
+ MAKE_PROPERTIES_REF(cpGearJoint, Phase);
121
+ MAKE_REF(cpGearJointGetRatio);
122
+
123
+ MAKE_PROPERTIES_REF(cpGrooveJoint, Anchr2);
124
+ MAKE_REF(cpGrooveJointGetGrooveA);
125
+ MAKE_REF(cpGrooveJointGetGrooveB);
126
+
127
+ MAKE_PROPERTIES_REF(cpPinJoint, Anchr1);
128
+ MAKE_PROPERTIES_REF(cpPinJoint, Anchr2);
129
+ MAKE_PROPERTIES_REF(cpPinJoint, Dist);
130
+
131
+ MAKE_PROPERTIES_REF(cpPivotJoint, Anchr1);
132
+ MAKE_PROPERTIES_REF(cpPivotJoint, Anchr2);
133
+
134
+ MAKE_PROPERTIES_REF(cpRatchetJoint, Angle);
135
+ MAKE_PROPERTIES_REF(cpRatchetJoint, Phase);
136
+ MAKE_PROPERTIES_REF(cpRatchetJoint, Ratchet);
137
+
138
+ MAKE_PROPERTIES_REF(cpRotaryLimitJoint, Min);
139
+ MAKE_PROPERTIES_REF(cpRotaryLimitJoint, Max);
140
+
141
+ MAKE_PROPERTIES_REF(cpSimpleMotor, Rate);
142
+
143
+ MAKE_PROPERTIES_REF(cpSlideJoint, Anchr1);
144
+ MAKE_PROPERTIES_REF(cpSlideJoint, Anchr2);
145
+ MAKE_PROPERTIES_REF(cpSlideJoint, Min);
146
+ MAKE_PROPERTIES_REF(cpSlideJoint, Max);
147
+
58
148
  MAKE_REF(cpSegmentQueryHitPoint);
59
149
  MAKE_REF(cpSegmentQueryHitDist);
150
+
151
+ MAKE_REF(cpSpatialIndexDestroy);
152
+ MAKE_REF(cpSpatialIndexCount);
153
+ MAKE_REF(cpSpatialIndexEach);
154
+ MAKE_REF(cpSpatialIndexContains);
155
+ MAKE_REF(cpSpatialIndexInsert);
156
+ MAKE_REF(cpSpatialIndexRemove);
157
+ MAKE_REF(cpSpatialIndexReindex);
158
+ MAKE_REF(cpSpatialIndexReindexObject);
159
+ MAKE_REF(cpSpatialIndexSegmentQuery);
160
+ MAKE_REF(cpSpatialIndexQuery);
161
+ MAKE_REF(cpSpatialIndexReindexQuery);
162
+
163
+ MAKE_PROPERTIES_REF(cpSpace, Iterations);
164
+ MAKE_PROPERTIES_REF(cpSpace, Gravity);
165
+ MAKE_PROPERTIES_REF(cpSpace, Damping);
166
+ MAKE_PROPERTIES_REF(cpSpace, IdleSpeedThreshold);
167
+ MAKE_PROPERTIES_REF(cpSpace, SleepTimeThreshold);
168
+ MAKE_PROPERTIES_REF(cpSpace, CollisionSlop);
169
+ MAKE_PROPERTIES_REF(cpSpace, CollisionBias);
170
+ MAKE_PROPERTIES_REF(cpSpace, CollisionPersistence);
171
+ MAKE_PROPERTIES_REF(cpSpace, EnableContactGraph);
172
+ MAKE_PROPERTIES_REF(cpSpace, UserData);
173
+ MAKE_REF(cpSpaceGetStaticBody);
174
+ MAKE_REF(cpSpaceGetCurrentTimeStep);
175
+ MAKE_REF(cpSpaceIsLocked);
176
+
177
+ #endif
@@ -22,28 +22,244 @@
22
22
  #define CP_ALLOW_PRIVATE_ACCESS 1
23
23
  #include "chipmunk.h"
24
24
 
25
- void *cpSpaceGetPostStepData(cpSpace *space, void *obj);
25
+ #define CP_HASH_COEF (3344921057ul)
26
+ #define CP_HASH_PAIR(A, B) ((cpHashValue)(A)*CP_HASH_COEF ^ (cpHashValue)(B)*CP_HASH_COEF)
27
+
28
+ //MARK: cpArray
29
+
30
+ struct cpArray {
31
+ int num, max;
32
+ void **arr;
33
+ };
34
+
35
+ cpArray *cpArrayNew(int size);
36
+
37
+ void cpArrayFree(cpArray *arr);
38
+
39
+ void cpArrayPush(cpArray *arr, void *object);
40
+ void *cpArrayPop(cpArray *arr);
41
+ void cpArrayDeleteObj(cpArray *arr, void *obj);
42
+ cpBool cpArrayContains(cpArray *arr, void *ptr);
43
+
44
+ void cpArrayFreeEach(cpArray *arr, void (freeFunc)(void*));
45
+
46
+ //MARK: Foreach loops
47
+
48
+ static inline cpConstraint *
49
+ cpConstraintNext(cpConstraint *node, cpBody *body)
50
+ {
51
+ return (node->a == body ? node->next_a : node->next_b);
52
+ }
53
+
54
+ #define CP_BODY_FOREACH_CONSTRAINT(bdy, var)\
55
+ for(cpConstraint *var = bdy->constraintList; var; var = cpConstraintNext(var, bdy))
56
+
57
+ static inline cpArbiter *
58
+ cpArbiterNext(cpArbiter *node, cpBody *body)
59
+ {
60
+ return (node->body_a == body ? node->thread_a.next : node->thread_b.next);
61
+ }
62
+
63
+ #define CP_BODY_FOREACH_ARBITER(bdy, var)\
64
+ for(cpArbiter *var = bdy->arbiterList; var; var = cpArbiterNext(var, bdy))
65
+
66
+ #define CP_BODY_FOREACH_SHAPE(body, var)\
67
+ for(cpShape *var = body->shapeList; var; var = var->next)
68
+
69
+ #define CP_BODY_FOREACH_COMPONENT(root, var)\
70
+ for(cpBody *var = root; var; var = var->node.next)
71
+
72
+ //MARK: cpHashSet
73
+
74
+ typedef cpBool (*cpHashSetEqlFunc)(void *ptr, void *elt);
75
+ typedef void *(*cpHashSetTransFunc)(void *ptr, void *data);
76
+
77
+ cpHashSet *cpHashSetNew(int size, cpHashSetEqlFunc eqlFunc);
78
+ void cpHashSetSetDefaultValue(cpHashSet *set, void *default_value);
79
+
80
+ void cpHashSetFree(cpHashSet *set);
81
+
82
+ int cpHashSetCount(cpHashSet *set);
83
+ void *cpHashSetInsert(cpHashSet *set, cpHashValue hash, void *ptr, void *data, cpHashSetTransFunc trans);
84
+ void *cpHashSetRemove(cpHashSet *set, cpHashValue hash, void *ptr);
85
+ void *cpHashSetFind(cpHashSet *set, cpHashValue hash, void *ptr);
86
+
87
+ typedef void (*cpHashSetIteratorFunc)(void *elt, void *data);
88
+ void cpHashSetEach(cpHashSet *set, cpHashSetIteratorFunc func, void *data);
89
+
90
+ typedef cpBool (*cpHashSetFilterFunc)(void *elt, void *data);
91
+ void cpHashSetFilter(cpHashSet *set, cpHashSetFilterFunc func, void *data);
92
+
93
+ //MARK: Body Functions
94
+
95
+ void cpBodyAddShape(cpBody *body, cpShape *shape);
96
+ void cpBodyRemoveShape(cpBody *body, cpShape *shape);
97
+ void cpBodyRemoveConstraint(cpBody *body, cpConstraint *constraint);
98
+
99
+
100
+ //MARK: Shape/Collision Functions
101
+
102
+ // TODO should move this to the cpVect API. It's pretty useful.
103
+ static inline cpVect
104
+ cpClosetPointOnSegment(const cpVect p, const cpVect a, const cpVect b)
105
+ {
106
+ cpVect delta = cpvsub(a, b);
107
+ cpFloat t = cpfclamp01(cpvdot(delta, cpvsub(p, b))/cpvlengthsq(delta));
108
+ return cpvadd(b, cpvmult(delta, t));
109
+ }
110
+
111
+ cpShape* cpShapeInit(cpShape *shape, const cpShapeClass *klass, cpBody *body);
112
+
113
+ static inline cpBool
114
+ cpShapeActive(cpShape *shape)
115
+ {
116
+ return shape->prev || (shape->body && shape->body->shapeList == shape);
117
+ }
118
+
119
+ int cpCollideShapes(const cpShape *a, const cpShape *b, cpContact *arr);
120
+
121
+ // TODO doesn't really need to be inline, but need a better place to put this function
122
+ static inline cpSplittingPlane
123
+ cpSplittingPlaneNew(cpVect a, cpVect b)
124
+ {
125
+ cpVect n = cpvnormalize(cpvperp(cpvsub(b, a)));
126
+ cpSplittingPlane plane = {n, cpvdot(n, a)};
127
+ return plane;
128
+ }
129
+
130
+ static inline cpFloat
131
+ cpSplittingPlaneCompare(cpSplittingPlane plane, cpVect v)
132
+ {
133
+ return cpvdot(plane.n, v) - plane.d;
134
+ }
135
+
136
+ void cpLoopIndexes(cpVect *verts, int count, int *start, int *end);
137
+
138
+ static inline cpFloat
139
+ cpPolyShapeValueOnAxis(const cpPolyShape *poly, const cpVect n, const cpFloat d)
140
+ {
141
+ cpVect *verts = poly->tVerts;
142
+ cpFloat min = cpvdot(n, verts[0]);
143
+
144
+ for(int i=1; i<poly->numVerts; i++){
145
+ min = cpfmin(min, cpvdot(n, verts[i]));
146
+ }
147
+
148
+ return min - d;
149
+ }
150
+
151
+ static inline cpBool
152
+ cpPolyShapeContainsVert(const cpPolyShape *poly, const cpVect v)
153
+ {
154
+ cpSplittingPlane *planes = poly->tPlanes;
155
+
156
+ for(int i=0; i<poly->numVerts; i++){
157
+ cpFloat dist = cpSplittingPlaneCompare(planes[i], v);
158
+ if(dist > 0.0f) return cpFalse;
159
+ }
160
+
161
+ return cpTrue;
162
+ }
163
+
164
+ static inline cpBool
165
+ cpPolyShapeContainsVertPartial(const cpPolyShape *poly, const cpVect v, const cpVect n)
166
+ {
167
+ cpSplittingPlane *planes = poly->tPlanes;
168
+
169
+ for(int i=0; i<poly->numVerts; i++){
170
+ if(cpvdot(planes[i].n, n) < 0.0f) continue;
171
+ cpFloat dist = cpSplittingPlaneCompare(planes[i], v);
172
+ if(dist > 0.0f) return cpFalse;
173
+ }
174
+
175
+ return cpTrue;
176
+ }
177
+
178
+ //MARK: Spatial Index Functions
179
+
180
+ cpSpatialIndex *cpSpatialIndexInit(cpSpatialIndex *index, cpSpatialIndexClass *klass, cpSpatialIndexBBFunc bbfunc, cpSpatialIndex *staticIndex);
181
+
182
+ //MARK: Space Functions
183
+
184
+ extern cpCollisionHandler cpDefaultCollisionHandler;
185
+ void cpSpaceProcessComponents(cpSpace *space, cpFloat dt);
186
+
187
+ void cpSpacePushFreshContactBuffer(cpSpace *space);
188
+ cpContact *cpContactBufferGetArray(cpSpace *space);
189
+ void cpSpacePushContacts(cpSpace *space, int count);
190
+
191
+ typedef struct cpPostStepCallback {
192
+ cpPostStepFunc func;
193
+ void *key;
194
+ void *data;
195
+ } cpPostStepCallback;
196
+
197
+ cpPostStepCallback *cpSpaceGetPostStepCallback(cpSpace *space, void *key);
198
+
199
+ cpBool cpSpaceArbiterSetFilter(cpArbiter *arb, cpSpace *space);
200
+ void cpSpaceFilterArbiters(cpSpace *space, cpBody *body, cpShape *filter);
26
201
 
27
202
  void cpSpaceActivateBody(cpSpace *space, cpBody *body);
203
+ void cpSpaceLock(cpSpace *space);
204
+ void cpSpaceUnlock(cpSpace *space, cpBool runPostStep);
205
+
206
+ static inline cpCollisionHandler *
207
+ cpSpaceLookupHandler(cpSpace *space, cpCollisionType a, cpCollisionType b)
208
+ {
209
+ cpCollisionType types[] = {a, b};
210
+ return (cpCollisionHandler *)cpHashSetFind(space->collisionHandlers, CP_HASH_PAIR(a, b), types);
211
+ }
28
212
 
29
213
  static inline void
30
- cpSpaceLock(cpSpace *space)
214
+ cpSpaceUncacheArbiter(cpSpace *space, cpArbiter *arb)
31
215
  {
32
- space->locked++;
216
+ cpShape *a = arb->a, *b = arb->b;
217
+ cpShape *shape_pair[] = {a, b};
218
+ cpHashValue arbHashID = CP_HASH_PAIR((cpHashValue)a, (cpHashValue)b);
219
+ cpHashSetRemove(space->cachedArbiters, arbHashID, shape_pair);
220
+ cpArrayDeleteObj(space->arbiters, arb);
33
221
  }
34
222
 
223
+ void cpShapeUpdateFunc(cpShape *shape, void *unused);
224
+ void cpSpaceCollideShapes(cpShape *a, cpShape *b, cpSpace *space);
225
+
226
+
227
+
228
+ //MARK: Arbiters
229
+
230
+ struct cpContact {
231
+ cpVect p, n;
232
+ cpFloat dist;
233
+
234
+ cpVect r1, r2;
235
+ cpFloat nMass, tMass, bounce;
236
+
237
+ cpFloat jnAcc, jtAcc, jBias;
238
+ cpFloat bias;
239
+
240
+ cpHashValue hash;
241
+ };
242
+
243
+ cpContact* cpContactInit(cpContact *con, cpVect p, cpVect n, cpFloat dist, cpHashValue hash);
244
+ cpArbiter* cpArbiterInit(cpArbiter *arb, cpShape *a, cpShape *b);
245
+
35
246
  static inline void
36
- cpSpaceUnlock(cpSpace *space)
37
- {
38
- space->locked--;
39
- cpAssert(space->locked >= 0, "Internal error:Space lock underflow.");
40
-
41
- if(!space->locked){
42
- cpArray *waking = space->rousedBodies;
43
- for(int i=0, count=waking->num; i<count; i++){
44
- cpSpaceActivateBody(space, (cpBody *)waking->arr[i]);
45
- }
46
-
47
- waking->num = 0;
48
- }
247
+ cpArbiterCallSeparate(cpArbiter *arb, cpSpace *space)
248
+ {
249
+ // The handler needs to be looked up again as the handler cached on the arbiter may have been deleted since the last step.
250
+ cpCollisionHandler *handler = cpSpaceLookupHandler(space, arb->a->collision_type, arb->b->collision_type);
251
+ handler->separate(arb, space, handler->data);
49
252
  }
253
+
254
+ static inline struct cpArbiterThread *
255
+ cpArbiterThreadForBody(cpArbiter *arb, cpBody *body)
256
+ {
257
+ return (arb->body_a == body ? &arb->thread_a : &arb->thread_b);
258
+ }
259
+
260
+ void cpArbiterUnthread(cpArbiter *arb);
261
+
262
+ void cpArbiterUpdate(cpArbiter *arb, cpContact *contacts, int numContacts, struct cpCollisionHandler *handler, cpShape *a, cpShape *b);
263
+ void cpArbiterPreStep(cpArbiter *arb, cpFloat dt, cpFloat bias, cpFloat slop);
264
+ void cpArbiterApplyCachedImpulse(cpArbiter *arb, cpFloat dt_coef);
265
+ void cpArbiterApplyImpulse(cpArbiter *arb);
@@ -1,16 +1,18 @@
1
+ #include <stdint.h>
2
+
1
3
  #ifdef __APPLE__
2
- #import "TargetConditionals.h"
4
+ #include "TargetConditionals.h"
3
5
  #endif
4
6
 
5
- #if (defined TARGET_OS_IPHONE) && (!defined CP_USE_CGPOINTS)
6
- #define CP_USE_CGPOINTS
7
+ #if (TARGET_OS_IPHONE == 1) || (TARGET_OS_MAC == 1) && (!defined CP_USE_CGPOINTS)
8
+ #define CP_USE_CGPOINTS 1
7
9
  #endif
8
10
 
9
- #ifdef CP_USE_CGPOINTS
11
+ #if CP_USE_CGPOINTS == 1
10
12
  #if TARGET_OS_IPHONE
11
13
  #import <CoreGraphics/CGGeometry.h>
12
14
  #elif TARGET_OS_MAC
13
- #import <ApplicationServices/ApplicationServices.h>
15
+ #include <ApplicationServices/ApplicationServices.h>
14
16
  #endif
15
17
 
16
18
  #if defined(__LP64__) && __LP64__
@@ -25,7 +27,13 @@
25
27
  #define CP_USE_DOUBLES 1
26
28
  #endif
27
29
 
30
+ /// @defgroup basicTypes Basic Types
31
+ /// Most of these types can be configured at compile time.
32
+ /// @{
33
+
28
34
  #if CP_USE_DOUBLES
35
+ /// Chipmunk's floating point type.
36
+ /// Can be reconfigured at compile time.
29
37
  typedef double cpFloat;
30
38
  #define cpfsqrt sqrt
31
39
  #define cpfsin sin
@@ -51,53 +59,84 @@
51
59
  #define cpfceil ceilf
52
60
  #endif
53
61
 
54
- static inline cpFloat
55
- cpfmax(cpFloat a, cpFloat b)
62
+ #ifndef INFINITY
63
+ #ifdef _MSC_VER
64
+ union MSVC_EVIL_FLOAT_HACK
65
+ {
66
+ unsigned __int8 Bytes[4];
67
+ float Value;
68
+ };
69
+ static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
70
+ #define INFINITY (INFINITY_HACK.Value)
71
+ #endif
72
+
73
+ #ifdef __GNUC__
74
+ #define INFINITY (__builtin_inf())
75
+ #endif
76
+
77
+ #ifndef INFINITY
78
+ #define INFINITY (1e1000)
79
+ #endif
80
+ #endif
81
+
82
+ #ifndef M_PI
83
+ #define M_PI 3.14159265358979323846264338327950288
84
+ #endif
85
+
86
+ #ifndef M_E
87
+ #define M_E 2.71828182845904523536028747135266250
88
+ #endif
89
+
90
+
91
+ /// Return the max of two cpFloats.
92
+ static inline cpFloat cpfmax(cpFloat a, cpFloat b)
56
93
  {
57
94
  return (a > b) ? a : b;
58
95
  }
59
96
 
60
- static inline cpFloat
61
- cpfmin(cpFloat a, cpFloat b)
97
+ /// Return the min of two cpFloats.
98
+ static inline cpFloat cpfmin(cpFloat a, cpFloat b)
62
99
  {
63
100
  return (a < b) ? a : b;
64
101
  }
65
102
 
66
- static inline cpFloat
67
- cpfabs(cpFloat n)
103
+ /// Return the absolute value of a cpFloat.
104
+ static inline cpFloat cpfabs(cpFloat f)
68
105
  {
69
- return (n < 0) ? -n : n;
106
+ return (f < 0) ? -f : f;
70
107
  }
71
108
 
72
- static inline cpFloat
73
- cpfclamp(cpFloat f, cpFloat min, cpFloat max)
109
+ /// Clamp @c f to be between @c min and @c max.
110
+ static inline cpFloat cpfclamp(cpFloat f, cpFloat min, cpFloat max)
74
111
  {
75
112
  return cpfmin(cpfmax(f, min), max);
76
113
  }
77
114
 
78
- static inline cpFloat
79
- cpflerp(cpFloat f1, cpFloat f2, cpFloat t)
115
+ /// Clamp @c f to be between 0 and 1.
116
+ static inline cpFloat cpfclamp01(cpFloat f)
117
+ {
118
+ return cpfmax(0.0f, cpfmin(f, 1.0f));
119
+ }
120
+
121
+
122
+
123
+ /// Linearly interpolate (or extrapolate) between @c f1 and @c f2 by @c t percent.
124
+ static inline cpFloat cpflerp(cpFloat f1, cpFloat f2, cpFloat t)
80
125
  {
81
126
  return f1*(1.0f - t) + f2*t;
82
127
  }
83
128
 
84
- static inline cpFloat
85
- cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
129
+ /// Linearly interpolate from @c f1 to @c f2 by no more than @c d.
130
+ static inline cpFloat cpflerpconst(cpFloat f1, cpFloat f2, cpFloat d)
86
131
  {
87
132
  return f1 + cpfclamp(f2 - f1, -d, d);
88
133
  }
89
134
 
90
- // CGPoints are structurally the same, and allow
91
- // easy interoperability with other Cocoa libraries
92
- #ifdef CP_USE_CGPOINTS
93
- typedef CGPoint cpVect;
94
- #else
95
- typedef struct cpVect{cpFloat x,y;} cpVect;
96
- #endif
97
-
98
- typedef unsigned int cpHashValue;
135
+ /// Hash value type.
136
+ typedef uintptr_t cpHashValue;
99
137
 
100
138
  // Oh C, how we love to define our own boolean types to get compiler compatibility
139
+ /// Chipmunk's boolean type.
101
140
  #ifdef CP_BOOL_TYPE
102
141
  typedef CP_BOOL_TYPE cpBool;
103
142
  #else
@@ -105,47 +144,72 @@ typedef unsigned int cpHashValue;
105
144
  #endif
106
145
 
107
146
  #ifndef cpTrue
147
+ /// true value.
108
148
  #define cpTrue 1
109
149
  #endif
110
150
 
111
151
  #ifndef cpFalse
152
+ /// false value.
112
153
  #define cpFalse 0
113
154
  #endif
114
155
 
115
156
  #ifdef CP_DATA_POINTER_TYPE
116
157
  typedef CP_DATA_POINTER_TYPE cpDataPointer;
117
158
  #else
159
+ /// Type used for user data pointers.
118
160
  typedef void * cpDataPointer;
119
161
  #endif
120
162
 
121
163
  #ifdef CP_COLLISION_TYPE_TYPE
122
164
  typedef CP_COLLISION_TYPE_TYPE cpCollisionType;
123
165
  #else
124
- typedef unsigned int cpCollisionType;
166
+ /// Type used for cpSpace.collision_type.
167
+ typedef uintptr_t cpCollisionType;
125
168
  #endif
126
169
 
127
170
  #ifdef CP_GROUP_TYPE
128
171
  typedef CP_GROUP_TYPE cpGroup;
129
172
  #else
130
- typedef unsigned int cpGroup;
173
+ /// Type used for cpShape.group.
174
+ typedef uintptr_t cpGroup;
131
175
  #endif
132
176
 
133
177
  #ifdef CP_LAYERS_TYPE
134
- typedef CP_GROUP_TYPE cpLayers;
178
+ typedef CP_LAYERS_TYPE cpLayers;
135
179
  #else
180
+ /// Type used for cpShape.layers.
136
181
  typedef unsigned int cpLayers;
137
182
  #endif
138
183
 
139
184
  #ifdef CP_TIMESTAMP_TYPE
140
185
  typedef CP_TIMESTAMP_TYPE cpTimestamp;
141
186
  #else
187
+ /// Type used for various timestamps in Chipmunk.
142
188
  typedef unsigned int cpTimestamp;
143
189
  #endif
144
190
 
145
191
  #ifndef CP_NO_GROUP
192
+ /// Value for cpShape.group signifying that a shape is in no group.
146
193
  #define CP_NO_GROUP ((cpGroup)0)
147
194
  #endif
148
195
 
149
196
  #ifndef CP_ALL_LAYERS
197
+ /// Value for cpShape.layers signifying that a shape is in every layer.
150
198
  #define CP_ALL_LAYERS (~(cpLayers)0)
151
199
  #endif
200
+ /// @}
201
+
202
+ // CGPoints are structurally the same, and allow
203
+ // easy interoperability with other Cocoa libraries
204
+ #if CP_USE_CGPOINTS
205
+ typedef CGPoint cpVect;
206
+ #else
207
+ /// Chipmunk's 2D vector type.
208
+ /// @addtogroup cpVect
209
+ typedef struct cpVect{cpFloat x,y;} cpVect;
210
+ #endif
211
+
212
+ typedef struct cpMat2x2 {
213
+ // Row major [[a, b][c d]]
214
+ cpFloat a, b, c, d;
215
+ } cpMat2x2;