annotate trunk/chipmunkd/cpSpace.d @ 29:80058cee1a77

updated to chipmunk 5.3.4
author Extrawurst
date Thu, 16 Dec 2010 00:33:06 +0100
parents 4541ca17975b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
1 // written in the D programming language
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
2
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
3 module chipmunkd.cpSpace;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
5 import chipmunkd.chipmunk;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
6
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
7 // Number of frames that contact information should persist.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
8 //extern cpTimestamp cp_contact_persistence;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
9
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
10 // User collision handler function types.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
11 alias cpBool function(cpArbiter *arb, cpSpace *space, void *data)cpCollisionBeginFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
12 alias cpBool function(cpArbiter *arb, cpSpace *space, void *data)cpCollisionPreSolveFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
13 alias void function(cpArbiter *arb, cpSpace *space, void *data)cpCollisionPostSolveFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
14 alias void function(cpArbiter *arb, cpSpace *space, void *data)cpCollisionSeparateFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
15
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
16 // Structure for holding collision pair function information.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
17 // Used internally.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
18 struct cpCollisionHandler {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
19 cpCollisionType a;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
20 cpCollisionType b;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
21 cpCollisionBeginFunc begin;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
22 cpCollisionPreSolveFunc preSolve;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
23 cpCollisionPostSolveFunc postSolve;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
24 cpCollisionSeparateFunc separate;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
25 void *data;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
26 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
27
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
28 struct cpContactBufferHeader {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
29 cpTimestamp stamp;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
30 cpContactBufferHeader *next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
31 uint numContacts;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
32 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
33
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
34 struct cpSpace{
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
35 // *** User definable fields
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
36
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
37 // Number of iterations to use in the impulse solver to solve contacts.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
38 int iterations;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
39
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
40 // Number of iterations to use in the impulse solver to solve elastic collisions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
41 int elasticIterations;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
42
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
43 // Default gravity to supply when integrating rigid body motions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
44 cpVect gravity;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
45
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
46 // Default damping to supply when integrating rigid body motions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
47 cpFloat damping;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
48
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
49 // Speed threshold for a body to be considered idle.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
50 // The default value of 0 means to let the space guess a good threshold based on gravity.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
51 cpFloat idleSpeedThreshold;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
52
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
53 // Time a group of bodies must remain idle in order to fall asleep
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
54 // The default value of INFINITY disables the sleeping algorithm.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
55 cpFloat sleepTimeThreshold;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
56
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
57 // *** Internally Used Fields
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
58
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
59 // When the space is locked, you should not add or remove objects;
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
60 int locked;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
61
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
62 // Time stamp. Is incremented on every call to cpSpaceStep().
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
63 cpTimestamp stamp;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
64
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
65 // The static and active shape spatial hashes.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
66 cpSpaceHash* staticShapes;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
67 cpSpaceHash* activeShapes;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
68
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
69 // List of bodies in the system.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
70 cpArray *bodies;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
71
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
72 // List of groups of sleeping bodies.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
73 cpArray *sleepingComponents;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
74
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
75 // List of bodies that have been flagged to be awoken.
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
76 cpArray *rousedBodies;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
77
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
78 // List of active arbiters for the impulse solver.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
79 cpArray* arbiters, pooledArbiters;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
80
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
81 // Linked list ring of contact buffers.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
82 // Head is the newest buffer, and each buffer points to a newer buffer.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
83 // Head wraps around and points to the oldest (tail) buffer.
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
84 cpContactBufferHeader* contactBuffersHead;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
85 deprecated cpContactBufferHeader* _contactBuffersTail_Deprecated;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
86
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
87 // List of buffers to be free()ed when destroying the space.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
88 cpArray *allocatedBuffers;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
89
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
90 // Persistant contact set.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
91 cpHashSet *contactSet;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
92
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
93 // List of constraints in the system.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
94 cpArray *constraints;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
95
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
96 // Set of collisionpair functions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
97 cpHashSet *collFuncSet;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
98 // Default collision handler.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
99 cpCollisionHandler defaultHandler;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
100
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
101 cpHashSet *postStepCallbacks;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
102
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
103 cpBody staticBody;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
104 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
105
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
106 //// Basic allocation/destruction functions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
107 //cpSpace* cpSpaceAlloc(void);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
108 //cpSpace* cpSpaceInit(cpSpace *space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
109 //cpSpace* cpSpaceNew(void);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
110 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
111 //void cpSpaceDestroy(cpSpace *space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
112 //void cpSpaceFree(cpSpace *space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
113 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
114 //// Convenience function. Frees all referenced entities. (bodies, shapes and constraints)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
115 //void cpSpaceFreeChildren(cpSpace *space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
116 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
117 //// Collision handler management functions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
118 //void cpSpaceSetDefaultCollisionHandler(
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
119 // cpSpace *space,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
120 // cpCollisionBeginFunc begin,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
121 // cpCollisionPreSolveFunc preSolve,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
122 // cpCollisionPostSolveFunc postSolve,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
123 // cpCollisionSeparateFunc separate,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
124 // void *data
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
125 //);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
126 //void cpSpaceAddCollisionHandler(
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
127 // cpSpace *space,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
128 // cpCollisionType a, cpCollisionType b,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
129 // cpCollisionBeginFunc begin,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
130 // cpCollisionPreSolveFunc preSolve,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
131 // cpCollisionPostSolveFunc postSolve,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
132 // cpCollisionSeparateFunc separate,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
133 // void *data
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
134 //);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
135 //void cpSpaceRemoveCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
136 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
137 //// Add and remove entities from the system.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
138 //cpShape *cpSpaceAddShape(cpSpace *space, cpShape *shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
139 //cpShape *cpSpaceAddStaticShape(cpSpace *space, cpShape *shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
140 //cpBody *cpSpaceAddBody(cpSpace *space, cpBody *body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
141 //cpConstraint *cpSpaceAddConstraint(cpSpace *space, cpConstraint *constraint);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
142 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
143 //void cpSpaceRemoveShape(cpSpace *space, cpShape *shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
144 //void cpSpaceRemoveStaticShape(cpSpace *space, cpShape *shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
145 //void cpSpaceRemoveBody(cpSpace *space, cpBody *body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
146 //void cpSpaceRemoveConstraint(cpSpace *space, cpConstraint *constraint);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
147 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
148 //// Post Step function definition
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
149 alias void function(cpSpace *space, void *obj, void *data) cpPostStepFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
150 //// Register a post step function to be called after cpSpaceStep() has finished.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
151 //// obj is used a key, you can only register one callback per unique value for obj
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
152 //void cpSpaceAddPostStepCallback(cpSpace *space, cpPostStepFunc func, void *obj, void *data);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
153 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
154 //// Point query callback function
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
155 alias void function(cpShape *shape, void *data) cpSpacePointQueryFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
156 //void cpSpacePointQuery(cpSpace *space, cpVect point, cpLayers layers, cpGroup group, cpSpacePointQueryFunc func, void *data);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
157 //cpShape *cpSpacePointQueryFirst(cpSpace *space, cpVect point, cpLayers layers, cpGroup group);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
158 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
159 //// Segment query callback function
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
160 alias void function(cpShape *shape, cpFloat t, cpVect n, void *data)cpSpaceSegmentQueryFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
161 //void cpSpaceSegmentQuery(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSpaceSegmentQueryFunc func, void *data);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
162 //cpShape *cpSpaceSegmentQueryFirst(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSegmentQueryInfo *out);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
163 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
164 //// BB query callback function
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
165 alias void function(cpShape *shape, void *data)cpSpaceBBQueryFunc;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
166 //void cpSpaceBBQuery(cpSpace *space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryFunc func, void *data);
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
167
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
168 // Shape query callback function
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
169 alias void function(cpShape *shape, cpContactPointSet *points, void *data)cpSpaceShapeQueryFunc;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
170 //cpBool cpSpaceShapeQuery(cpSpace *space, cpShape *shape, cpSpaceShapeQueryFunc func, void *data);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
171
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
172
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
173 //void cpSpaceActivateShapesTouchingShape(cpSpace *space, cpShape *shape);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
174
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
175 //// Iterator function for iterating the bodies in a space.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
176 alias void function(cpBody *_body, void *data)cpSpaceBodyIterator;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
177 //void cpSpaceEachBody(cpSpace *space, cpSpaceBodyIterator func, void *data);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
178 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
179 //// Spatial hash management functions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
180 //void cpSpaceResizeStaticHash(cpSpace *space, cpFloat dim, int count);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
181 //void cpSpaceResizeActiveHash(cpSpace *space, cpFloat dim, int count);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
182 //void cpSpaceRehashStatic(cpSpace *space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
183 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
184 //void cpSpaceRehashShape(cpSpace *space, cpShape *shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
185 //
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
186 //// Update the space.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
187 //void cpSpaceStep(cpSpace *space, cpFloat dt);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
188
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
189
28
4541ca17975b use __gshared as chipmunk heavily relies on globals and D would otherwise make them TLS
Extrawurst
parents: 23
diff changeset
190 __gshared cpTimestamp cp_contact_persistence = 3;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
191
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
192 // Equal function for contactSet.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
193 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
194 contactSetEql(cpShape **shapes, cpArbiter *arb)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
195 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
196 cpShape *a = shapes[0];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
197 cpShape *b = shapes[1];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
198
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
199 return ((a == arb.a && b == arb.b) || (b == arb.a && a == arb.b));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
200 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
201
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
202 // Transformation function for contactSet.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
203 static void *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
204 contactSetTrans(cpShape **shapes, cpSpace *space)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
205 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
206 if(space.pooledArbiters.num == 0){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
207 // arbiter pool is exhausted, make more
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
208 int count = CP_BUFFER_BYTES/cpArbiter.sizeof;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
209 assert(count, "Buffer size too small.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
210
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
211 cpArbiter *buffer = cast(cpArbiter *)cpmalloc(CP_BUFFER_BYTES);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
212 cpArrayPush(space.allocatedBuffers, buffer);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
213
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
214 for(int i=0; i<count; i++) cpArrayPush(space.pooledArbiters, buffer + i);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
215 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
216
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
217 return cpArbiterInit(cast(cpArbiter *) cpArrayPop(space.pooledArbiters), shapes[0], shapes[1]);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
218 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
219
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
220 // Equals function for collFuncSet.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
221 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
222 collFuncSetEql(cpCollisionHandler *check, cpCollisionHandler *pair)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
223 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
224 return ((check.a == pair.a && check.b == pair.b) || (check.b == pair.a && check.a == pair.b));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
225 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
226
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
227 // Transformation function for collFuncSet.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
228 static void *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
229 collFuncSetTrans(cpCollisionHandler *handler, void *unused)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
230 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
231 cpCollisionHandler *copy = cast(cpCollisionHandler *)cpmalloc(cpCollisionHandler.sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
232 (*copy) = (*handler);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
233
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
234 return copy;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
235 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
236
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
237 // Default collision functions.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
238 static cpBool alwaysCollide(cpArbiter *arb, cpSpace *space, void *data){return 1;}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
239 static void nothing(cpArbiter *arb, cpSpace *space, void *data){}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
240
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
241 // BBfunc callback for the spatial hash.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
242 static cpBB shapeBBFunc(cpShape *shape){return shape.bb;}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
243
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
244 // Iterator functions for destructors.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
245 static void freeWrap(void *ptr, void *unused){ cpfree(ptr);}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
246 static void shapeFreeWrap(cpShape *ptr, void *unused){ cpShapeFree(ptr);}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
247 static void bodyFreeWrap(cpBody *ptr, void *unused){ cpBodyFree(ptr);}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
248 static void constraintFreeWrap(cpConstraint *ptr, void *unused){cpConstraintFree(ptr);}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
249
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
250 cpSpace *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
251 cpSpaceAlloc()
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
252 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
253 return cast(cpSpace *)cpcalloc(1, cpSpace.sizeof);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
254 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
255
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
256 enum DEFAULT_DIM_SIZE = 100.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
257 enum DEFAULT_COUNT = 1000;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
258 enum DEFAULT_ITERATIONS = 10;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
259 enum DEFAULT_ELASTIC_ITERATIONS = 0;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
260
28
4541ca17975b use __gshared as chipmunk heavily relies on globals and D would otherwise make them TLS
Extrawurst
parents: 23
diff changeset
261 __gshared cpCollisionHandler defaultHandler = {0, 0, &alwaysCollide, &alwaysCollide, &nothing, &nothing, null};
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
262
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
263 cpSpace*
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
264 cpSpaceInit(cpSpace *space)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
265 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
266 space.iterations = DEFAULT_ITERATIONS;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
267 space.elasticIterations = DEFAULT_ELASTIC_ITERATIONS;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
268 // space.sleepTicks = 300;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
269
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
270 space.gravity = cpvzero;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
271 space.damping = 1.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
272
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
273 space.locked = 0;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
274 space.stamp = 0;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
275
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
276 space.staticShapes = cpSpaceHashNew(DEFAULT_DIM_SIZE, DEFAULT_COUNT, cast(cpSpaceHashBBFunc)&shapeBBFunc);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
277 space.activeShapes = cpSpaceHashNew(DEFAULT_DIM_SIZE, DEFAULT_COUNT, cast(cpSpaceHashBBFunc)&shapeBBFunc);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
278
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
279 space.allocatedBuffers = cpArrayNew(0);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
280
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
281 space.bodies = cpArrayNew(0);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
282 space.sleepingComponents = cpArrayNew(0);
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
283 space.rousedBodies = cpArrayNew(0);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
284
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
285 space.sleepTimeThreshold = INFINITY;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
286 space.idleSpeedThreshold = 0.0f;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
287
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
288 space.arbiters = cpArrayNew(0);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
289 space.pooledArbiters = cpArrayNew(0);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
290
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
291 space.contactBuffersHead = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
292 space.contactSet = cpHashSetNew(0, cast(cpHashSetEqlFunc)&contactSetEql, cast(cpHashSetTransFunc)&contactSetTrans);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
293
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
294 space.constraints = cpArrayNew(0);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
295
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
296 space.defaultHandler = defaultHandler;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
297 space.collFuncSet = cpHashSetNew(0, cast(cpHashSetEqlFunc)&collFuncSetEql, cast(cpHashSetTransFunc)&collFuncSetTrans);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
298 space.collFuncSet.default_value = &space.defaultHandler;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
299
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
300 space.postStepCallbacks = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
301
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
302 cpBodyInit(&space.staticBody, INFINITY, INFINITY);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
303 space.staticBody.space = space;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
304
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
305 return space;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
306 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
307
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
308 cpSpace*
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
309 cpSpaceNew()
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
310 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
311 return cpSpaceInit(cpSpaceAlloc());
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
312 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
313
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
314 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
315 cpSpaceDestroy(cpSpace *space)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
316 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
317 cpSpaceHashFree(space.staticShapes);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
318 cpSpaceHashFree(space.activeShapes);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
319
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
320 cpArrayFree(space.bodies);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
321 cpArrayFree(space.sleepingComponents);
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
322 cpArrayFree(space.rousedBodies);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
323
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
324 cpArrayFree(space.constraints);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
325
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
326 cpHashSetFree(space.contactSet);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
327
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
328 cpArrayFree(space.arbiters);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
329 cpArrayFree(space.pooledArbiters);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
330
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
331 if(space.allocatedBuffers){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
332 cpArrayEach(space.allocatedBuffers, &freeWrap, null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
333 cpArrayFree(space.allocatedBuffers);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
334 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
335
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
336 if(space.postStepCallbacks){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
337 cpHashSetEach(space.postStepCallbacks, &freeWrap, null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
338 cpHashSetFree(space.postStepCallbacks);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
339 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
340
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
341 if(space.collFuncSet){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
342 cpHashSetEach(space.collFuncSet, &freeWrap, null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
343 cpHashSetFree(space.collFuncSet);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
344 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
345 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
346
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
347 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
348 cpSpaceFree(cpSpace *space)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
349 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
350 if(space){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
351 cpSpaceDestroy(space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
352 cpfree(space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
353 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
354 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
355
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
356 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
357 cpSpaceFreeChildren(cpSpace *space)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
358 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
359 cpArray *components = space.sleepingComponents;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
360 while(components.num) cpBodyActivate(cast(cpBody *)components.arr[0]);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
361
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
362 cpSpaceHashEach(space.staticShapes, cast(cpSpaceHashIterator)&shapeFreeWrap, null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
363 cpSpaceHashEach(space.activeShapes, cast(cpSpaceHashIterator)&shapeFreeWrap, null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
364 cpArrayEach(space.bodies, cast(cpArrayIter)&bodyFreeWrap, null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
365 cpArrayEach(space.constraints, cast(cpArrayIter)&constraintFreeWrap, null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
366 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
367
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
368 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
369 cpSpaceAddCollisionHandler(
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
370 cpSpace *space,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
371 cpCollisionType a, cpCollisionType b,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
372 cpCollisionBeginFunc begin,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
373 cpCollisionPreSolveFunc preSolve,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
374 cpCollisionPostSolveFunc postSolve,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
375 cpCollisionSeparateFunc separate,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
376 void *data
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
377 ){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
378 // Remove any old function so the new one will get added.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
379 cpSpaceRemoveCollisionHandler(space, a, b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
380
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
381 cpCollisionHandler handler = {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
382 a, b,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
383 begin ? begin : &alwaysCollide,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
384 preSolve ? preSolve : &alwaysCollide,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
385 postSolve ? postSolve : &nothing,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
386 separate ? separate : &nothing,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
387 data
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
388 };
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
389
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
390 cpHashSetInsert(space.collFuncSet, CP_HASH_PAIR(a, b), &handler, null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
391 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
392
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
393 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
394 cpSpaceRemoveCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
395 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
396 struct tmp{cpCollisionType a, b;} tmp ids = {a, b};
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
397 cpCollisionHandler *old_handler = cast(cpCollisionHandler *) cpHashSetRemove(space.collFuncSet, CP_HASH_PAIR(a, b), &ids);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
398 cpfree(old_handler);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
399 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
400
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
401 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
402 cpSpaceSetDefaultCollisionHandler(
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
403 cpSpace *space,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
404 cpCollisionBeginFunc begin,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
405 cpCollisionPreSolveFunc preSolve,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
406 cpCollisionPostSolveFunc postSolve,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
407 cpCollisionSeparateFunc separate,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
408 void *data
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
409 ){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
410 cpCollisionHandler handler = {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
411 0, 0,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
412 begin ? begin : &alwaysCollide,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
413 preSolve ? preSolve : &alwaysCollide,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
414 postSolve ? postSolve : &nothing,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
415 separate ? separate : &nothing,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
416 data
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
417 };
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
418
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
419 space.defaultHandler = handler;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
420 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
421
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
422 //#pragma mark Body, Shape, and Joint Management
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
423
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
424 void cpAssertSpaceUnlocked(cpSpace* _space){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
425 assert(!_space.locked,
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
426 "This addition/removal cannot be done safely during a call to cpSpaceStep(). "
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
427 "Put these calls into a Post Step Callback."
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
428 );}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
429
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
430 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
431 cpBodyAddShape(cpBody *_body, cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
432 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
433 shape.next = shape._body.shapesList;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
434 shape._body.shapesList = shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
435 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
436
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
437 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
438 cpBodyRemoveShape(cpBody *_body, cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
439 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
440 cpShape **prev_ptr = &_body.shapesList;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
441 cpShape *node = _body.shapesList;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
442
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
443 while(node && node != shape){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
444 prev_ptr = &node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
445 node = node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
446 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
447
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
448 assert(node, "Attempted to remove a shape from a body it was never attached to.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
449 (*prev_ptr) = node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
450 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
451
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
452 cpShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
453 cpSpaceAddShape(cpSpace *space, cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
454 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
455 cpBody *_body = shape._body;
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
456 if(!_body || cpBodyIsStatic(_body)) return cpSpaceAddStaticShape(space, shape);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
457
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
458 assert(!cpHashSetFind(space.activeShapes.handleSet, shape.hashid, shape),
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
459 "Cannot add the same shape more than once.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
460 cpAssertSpaceUnlocked(space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
461
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
462 cpBodyActivate(_body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
463 cpBodyAddShape(_body, shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
464
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
465 cpShapeCacheBB(shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
466 cpSpaceHashInsert(space.activeShapes, shape, shape.hashid, shape.bb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
467
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
468 return shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
469 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
470
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
471 cpShape *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
472 cpSpaceAddStaticShape(cpSpace *space, cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
473 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
474 assert(!cpHashSetFind(space.staticShapes.handleSet, shape.hashid, shape),
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
475 "Cannot add the same static shape more than once.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
476 cpAssertSpaceUnlocked(space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
477
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
478 if(!shape._body) shape._body = &space.staticBody;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
479
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
480 cpShapeCacheBB(shape);
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
481 cpSpaceActivateShapesTouchingShape(space, shape);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
482 cpSpaceHashInsert(space.staticShapes, shape, shape.hashid, shape.bb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
483
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
484 return shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
485 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
486
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
487 cpBody *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
488 cpSpaceAddBody(cpSpace *space, cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
489 {
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
490 mixin(cpAssertWarn!("!cpBodyIsStatic(_body)", "Static bodies cannot be added to a space as they are not meant to be simulated.",__FILE__,__LINE__));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
491 assert(!_body.space, "Cannot add a body to a more than one space or to the same space twice.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
492 // cpAssertSpaceUnlocked(space); This should be safe as long as it's not from an integration callback
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
493
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
494 cpArrayPush(space.bodies, _body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
495 _body.space = space;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
496
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
497 return _body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
498 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
499
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
500 cpConstraint *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
501 cpSpaceAddConstraint(cpSpace *space, cpConstraint *constraint)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
502 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
503 assert(!cpArrayContains(space.constraints, constraint), "Cannot add the same constraint more than once.");
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
504 // cpAssertSpaceUnlocked(space); This should be safe as long as its not from a constraint callback.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
505
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
506 if(!constraint.a) constraint.a = &space.staticBody;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
507 if(!constraint.b) constraint.b = &space.staticBody;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
508
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
509 cpBodyActivate(constraint.a);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
510 cpBodyActivate(constraint.b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
511 cpArrayPush(space.constraints, constraint);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
512
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
513 return constraint;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
514 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
515
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
516 struct removalContext {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
517 cpSpace *space;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
518 cpShape *shape;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
519 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
520
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
521 // Hashset filter func to throw away old arbiters.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
522 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
523 contactSetFilterRemovedShape(cpArbiter *arb, removalContext *context)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
524 {
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
525 if(context.shape == arb.a || context.shape == arb.b){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
526 if(arb.state != cpArbiterState.cpArbiterStateCached){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
527 arb.handler.separate(arb, context.space, arb.handler.data);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
528 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
529
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
530 cpArrayPush(context.space.pooledArbiters, arb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
531 return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
532 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
533
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
534 return cpTrue;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
535 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
536
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
537 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
538 cpSpaceRemoveShape(cpSpace *space, cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
539 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
540 cpBody *_body = shape._body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
541 if(cpBodyIsStatic(_body)){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
542 cpSpaceRemoveStaticShape(space, shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
543 return;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
544 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
545
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
546 cpBodyActivate(_body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
547
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
548 cpAssertSpaceUnlocked(space);
13
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
549 mixin(cpAssertWarn!("cpHashSetFind(space.activeShapes.handleSet, shape.hashid, shape) !is null",
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
550 "Cannot remove a shape that was never added to the space. (Removed twice maybe?)",__FILE__,__LINE__));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
551
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
552 cpBodyRemoveShape(_body, shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
553
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
554 removalContext context = {space, shape};
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
555 cpHashSetFilter(space.contactSet, cast(cpHashSetFilterFunc)&contactSetFilterRemovedShape, &context);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
556 cpSpaceHashRemove(space.activeShapes, shape, shape.hashid);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
557 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
558
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
559 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
560 cpSpaceRemoveStaticShape(cpSpace *space, cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
561 {
13
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
562 mixin(cpAssertWarn!("cpHashSetFind(space.staticShapes.handleSet, shape.hashid, shape) !is null",
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
563 "Cannot remove a static or sleeping shape that was never added to the space. (Removed twice maybe?)",__FILE__,__LINE__));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
564 cpAssertSpaceUnlocked(space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
565
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
566 removalContext context = {space, shape};
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
567 cpHashSetFilter(space.contactSet, cast(cpHashSetFilterFunc)&contactSetFilterRemovedShape, &context);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
568 cpSpaceHashRemove(space.staticShapes, shape, shape.hashid);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
569
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
570 cpSpaceActivateShapesTouchingShape(space, shape);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
571 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
572
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
573 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
574 cpSpaceRemoveBody(cpSpace *space, cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
575 {
13
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
576 mixin(cpAssertWarn!("_body.space == space",
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
577 "Cannot remove a body that was never added to the space. (Removed twice maybe?)"));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
578 cpAssertSpaceUnlocked(space);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
579
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
580 cpBodyActivate(_body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
581 cpArrayDeleteObj(space.bodies, _body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
582 _body.space = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
583 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
584
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
585 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
586 cpSpaceRemoveConstraint(cpSpace *space, cpConstraint *constraint)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
587 {
13
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
588 mixin(cpAssertWarn!("cpArrayContains(space.constraints, constraint)",
c03a41d47b60 - finished all constraints properties
Extrawurst
parents: 4
diff changeset
589 "Cannot remove a constraint that was never added to the space. (Removed twice maybe?)",__FILE__,__LINE__));
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
590 // cpAssertSpaceUnlocked(space); Should be safe as long as its not from a constraint callback.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
591
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
592 cpBodyActivate(constraint.a);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
593 cpBodyActivate(constraint.b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
594 cpArrayDeleteObj(space.constraints, constraint);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
595 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
596
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
597 //#pragma mark Spatial Hash Management
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
598
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
599 static void updateBBCache(cpShape *shape, void *unused){cpShapeCacheBB(shape);}
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
600
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
601 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
602 cpSpaceResizeStaticHash(cpSpace *space, cpFloat dim, int count)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
603 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
604 cpSpaceHashResize(space.staticShapes, dim, count);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
605 cpSpaceHashRehash(space.staticShapes);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
606 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
607
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
608 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
609 cpSpaceResizeActiveHash(cpSpace *space, cpFloat dim, int count)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
610 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
611 cpSpaceHashResize(space.activeShapes, dim, count);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
612 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
613
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
614 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
615 cpSpaceRehashStatic(cpSpace *space)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
616 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
617 cpSpaceHashEach(space.staticShapes, cast(cpSpaceHashIterator)&updateBBCache, null);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
618 cpSpaceHashRehash(space.staticShapes);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
619 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
620
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
621 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
622 cpSpaceRehashShape(cpSpace *space, cpShape *shape)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
623 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
624 cpShapeCacheBB(shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
625
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
626 // attempt to rehash the shape in both hashes
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
627 cpSpaceHashRehashObject(space.activeShapes, shape, shape.hashid);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
628 cpSpaceHashRehashObject(space.staticShapes, shape, shape.hashid);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
629 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
630
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
631 void
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
632 cpSpaceEachBody(cpSpace *space, cpSpaceBodyIterator func, void *data)
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
633 {
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
634 cpArray *bodies = space.bodies;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
635
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
636 for(int i=0; i<bodies.num; i++){
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
637 func(cast(cpBody *)bodies.arr[i], data);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
638 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
639
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
640 cpArray *components = space.sleepingComponents;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
641 for(int i=0; i<components.num; i++){
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
642 cpBody *root = cast(cpBody *)components.arr[i];
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
643 cpBody *_body = root, next;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
644 do {
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
645 next = _body.node.next;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
646 func(_body , data);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
647 } while((_body = next) != root);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
648 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
649 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
650
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
651 // chipmunk_private.h
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
652
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
653 //void *cpSpaceGetPostStepData(cpSpace *space, void *obj);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
654
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
655 //void cpSpaceActivateBody(cpSpace *space, cpBody *body);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
656
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
657 static void
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
658 cpSpaceLock(cpSpace *space)
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
659 {
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
660 space.locked++;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
661 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
662
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
663 static void
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
664 cpSpaceUnlock(cpSpace *space)
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
665 {
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
666 space.locked--;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
667 assert(space.locked >= 0, "Internal error:Space lock underflow.");
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
668
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
669 if(!space.locked){
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
670 cpArray *waking = space.rousedBodies;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
671 for(int i=0, count=waking.num; i<count; i++){
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
672 cpSpaceActivateBody(space, cast(cpBody *)waking.arr[i]);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
673 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
674
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
675 waking.num = 0;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
676 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
677 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 28
diff changeset
678