comparison trunk/chipmunkd/cpSpaceComponent.d @ 23:4ceef5833c8c

updated to chipmunk 5.3.3
author Extrawurst
date Fri, 10 Dec 2010 02:10:27 +0100
parents df4ebc8add66
children 80058cee1a77
comparison
equal deleted inserted replaced
22:ed2c81f3d1df 23:4ceef5833c8c
40 { 40 {
41 if(!cpBodyIsSleeping(root)) return; 41 if(!cpBodyIsSleeping(root)) return;
42 42
43 cpSpace *space = root.space; 43 cpSpace *space = root.space;
44 assert(space, "Trying to activate a body that was never added to a space."); 44 assert(space, "Trying to activate a body that was never added to a space.");
45 45 assert(!space.locked, "Bodies can not be awakened during a query or a call to cpSpaceSte(). Put these calls into a post-step callback.");
46 cpBody* _body = root; 46
47 cpBody* next; 47 cpBody *_body = root;
48 cpBody *next;
48 do { 49 do {
49 next = _body.node.next; 50 next = _body.node.next;
50 51
51 cpComponentNode node = {null, null, 0, 0.0f}; 52 cpFloat idleTime = (cpBodyIsStatic(_body) ? cast(cpFloat)INFINITY : 0.0f);
53 cpComponentNode node = {null, null, 0, idleTime};
52 _body.node = node; 54 _body.node = node;
53 cpArrayPush(space.bodies, _body); 55 if(!cpBodyIsRogue(_body)) cpArrayPush(space.bodies, _body);
54 56
55 for(cpShape *shape=_body.shapesList; shape; shape=shape.next){ 57 for(cpShape *shape=_body.shapesList; shape; shape=shape.next){
56 cpSpaceHashRemove(space.staticShapes, shape, shape.hashid); 58 cpSpaceHashRemove(space.staticShapes, shape, shape.hashid);
57 cpSpaceHashInsert(space.activeShapes, shape, shape.hashid, shape.bb); 59 cpSpaceHashInsert(space.activeShapes, shape, shape.hashid, shape.bb);
58 } 60 }
64 void 66 void
65 cpBodyActivate(cpBody *_body) 67 cpBodyActivate(cpBody *_body)
66 { 68 {
67 // Reset the idle time even if it's not in a currently sleeping component 69 // Reset the idle time even if it's not in a currently sleeping component
68 // Like a body resting on or jointed to a rogue body. 70 // Like a body resting on or jointed to a rogue body.
69 _body.node.idleTime = 0.0f; 71 if(!cpBodyIsStatic(_body)) _body.node.idleTime = 0.0f;
70 componentActivate(componentNodeRoot(_body)); 72 componentActivate(componentNodeRoot(_body));
71 } 73 }
72 74
73 static void 75 static void
74 mergeBodies(cpSpace *space, cpArray *components, cpArray *rogueBodies, cpBody *a, cpBody *b) 76 mergeBodies(cpSpace *space, cpArray *components, cpArray *rogueBodies, cpBody *a, cpBody *b)
88 componentActivate(a_root); 90 componentActivate(a_root);
89 componentActivate(b_root); 91 componentActivate(b_root);
90 } 92 }
91 93
92 // Add any rogue bodies (bodies not added to the space) 94 // Add any rogue bodies (bodies not added to the space)
93 if(!a.space) cpArrayPush(rogueBodies, a); 95 if(cpBodyIsRogue(a)) cpArrayPush(rogueBodies, a);
94 if(!b.space) cpArrayPush(rogueBodies, b); 96 if(cpBodyIsRogue(b)) cpArrayPush(rogueBodies, b);
95 97
96 componentNodeMerge(a_root, b_root); 98 componentNodeMerge(a_root, b_root);
97 } 99 }
98 100
99 static cpBool 101 static cpBool
101 { 103 {
102 cpBody *_body = root; 104 cpBody *_body = root;
103 cpBody *next; 105 cpBody *next;
104 do { 106 do {
105 next = _body.node.next; 107 next = _body.node.next;
106 if(cpBodyIsRogue(_body) || _body.node.idleTime < threshold) return cpTrue; 108 if(_body.node.idleTime < threshold) return cpTrue;
107 } while((_body = next) != root); 109 } while((_body = next) != root);
108 110
109 return cpFalse; 111 return cpFalse;
110 } 112 }
111 113
153 } 155 }
154 156
155 // iterate graph edges and build forests 157 // iterate graph edges and build forests
156 for(int i=0; i<arbiters.num; i++){ 158 for(int i=0; i<arbiters.num; i++){
157 cpArbiter *arb = cast(cpArbiter*)arbiters.arr[i]; 159 cpArbiter *arb = cast(cpArbiter*)arbiters.arr[i];
158 mergeBodies(space, components, rogueBodies, arb.private_a._body, arb.private_b._body); 160 mergeBodies(space, components, rogueBodies, arb.a._body, arb.b._body);
159 } 161 }
160 for(int j=0; j<constraints.num; j++){ 162 for(int j=0; j<constraints.num; j++){
161 cpConstraint *constraint = cast(cpConstraint *)constraints.arr[j]; 163 cpConstraint *constraint = cast(cpConstraint *)constraints.arr[j];
162 mergeBodies(space, components, rogueBodies, constraint.a, constraint.b); 164 mergeBodies(space, components, rogueBodies, constraint.a, constraint.b);
163 } 165 }
203 cpArrayFree(rogueBodies); 205 cpArrayFree(rogueBodies);
204 cpArrayFree(components); 206 cpArrayFree(components);
205 } 207 }
206 208
207 void 209 void
208 cpSpaceSleepBody(cpSpace *space, cpBody *_body){ 210 cpBodySleep(cpBody *_body)
209 cpComponentNode node = {null, _body, 0, 0.0f}; 211 {
210 _body.node = node; 212 cpBodySleepWithGroup(_body, null);
213 }
214
215 void
216 cpBodySleepWithGroup(cpBody *_body, cpBody *group){
217 assert(!cpBodyIsStatic(_body) && !cpBodyIsRogue(_body), "Rogue and static bodies cannot be put to sleep.");
218
219 cpSpace *space = _body.space;
220 assert(space, "Cannot put a body to sleep that has not been added to a space.");
221 assert(!space.locked, "Bodies can not be put to sleep during a query or a call to cpSpaceSte(). Put these calls into a post-step callback.");
222 assert(!group || cpBodyIsSleeping(group), "Cannot use a non-sleeping body as a group identifier.");
223
224 if(cpBodyIsSleeping(_body)) return;
211 225
212 for(cpShape *shape = _body.shapesList; shape; shape = shape.next){ 226 for(cpShape *shape = _body.shapesList; shape; shape = shape.next){
213 cpSpaceHashRemove(space.activeShapes, shape, shape.hashid); 227 cpSpaceHashRemove(space.activeShapes, shape, shape.hashid);
214 228
215 cpShapeCacheBB(shape); 229 cpShapeCacheBB(shape);
216 cpSpaceHashInsert(space.staticShapes, shape, shape.hashid, shape.bb); 230 cpSpaceHashInsert(space.staticShapes, shape, shape.hashid, shape.bb);
217 } 231 }
218 232
219 cpArrayPush(space.sleepingComponents, _body); 233 if(group){
234 cpBody *root = componentNodeRoot(group);
235
236 cpComponentNode node = {root, root.node.next, 0, 0.0f};
237 _body.node = node;
238 root.node.next = _body;
239 } else {
240 cpComponentNode node = {null, _body, 0, 0.0f};
241 _body.node = node;
242
243 cpArrayPush(space.sleepingComponents, _body);
244 }
245
220 cpArrayDeleteObj(space.bodies, _body); 246 cpArrayDeleteObj(space.bodies, _body);
221 } 247 }
248
249 static void
250 activateTouchingHelper(cpShape *shape, cpContactPointSet *points, cpArray **bodies){
251 if(*bodies == null) (*bodies) = cpArrayNew(0);
252 cpArrayPush(*bodies, shape._body);
253 }
254
255 void
256 cpSpaceActivateShapesTouchingShape(cpSpace *space, cpShape *shape){
257 cpArray *bodies = null;
258 cpSpaceShapeQuery(space, shape, cast(cpSpaceShapeQueryFunc)&activateTouchingHelper, &bodies);
259
260 if(bodies){
261 for(int i=0; i<bodies.num; i++){
262 cpBody *_body = cast(cpBody *)bodies.arr[i];
263 cpBodyActivate(_body);
264 }
265 }
266 }