annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
1
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
2 // written in the D programming language
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
3
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
4 module chipmunkd.cpSpaceComponent;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
5
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
6 import chipmunkd.chipmunk;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
7
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
8 // Chipmunk uses a data structure called a disjoint set forest.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
9 // My attempts to find a way to splice circularly linked lists in
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
10 // constant time failed, and so I found this neat data structure instead.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
11
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
12 static cpBody *
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
13 componentNodeRoot(cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
14 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
15 cpBody *parent = _body.node.parent;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
16
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
17 if(parent){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
18 // path compression, attaches this node directly to the root
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
19 return (_body.node.parent = componentNodeRoot(parent));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
20 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
21 return _body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
22 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
23 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
24
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
25 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
26 componentNodeMerge(cpBody *a_root, cpBody *b_root)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
27 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
28 if(a_root.node.rank < b_root.node.rank){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
29 a_root.node.parent = b_root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
30 } else if(a_root.node.rank > b_root.node.rank){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
31 b_root.node.parent = a_root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
32 } else if(a_root != b_root){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
33 b_root.node.parent = a_root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
34 a_root.node.rank++;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
35 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
36 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
37
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
38 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
39 componentActivate(cpBody *root)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
40 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
41 if(!cpBodyIsSleeping(root)) return;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
42
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
43 cpSpace *space = root.space;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
44 assert(space, "Trying to activate a body that was never added to a space.");
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
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.");
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
46
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
47 cpBody *_body = root;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
48 cpBody *next;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
49 do {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
50 next = _body.node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
51
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
52 cpFloat idleTime = (cpBodyIsStatic(_body) ? cast(cpFloat)INFINITY : 0.0f);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
53 cpComponentNode node = {null, null, 0, idleTime};
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
54 _body.node = node;
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
55 if(!cpBodyIsRogue(_body)) cpArrayPush(space.bodies, _body);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
56
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
57 for(cpShape *shape=_body.shapesList; shape; shape=shape.next){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
58 cpSpaceHashRemove(space.staticShapes, shape, shape.hashid);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
59 cpSpaceHashInsert(space.activeShapes, shape, shape.hashid, shape.bb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
60 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
61 } while((_body = next) != root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
62
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
63 cpArrayDeleteObj(space.sleepingComponents, root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
64 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
65
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
66 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
67 cpBodyActivate(cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
68 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
69 // Reset the idle time even if it's not in a currently sleeping component
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
70 // Like a body resting on or jointed to a rogue body.
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
71 if(!cpBodyIsStatic(_body)) _body.node.idleTime = 0.0f;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
72 componentActivate(componentNodeRoot(_body));
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
73 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
74
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
75 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
76 mergeBodies(cpSpace *space, cpArray *components, cpArray *rogueBodies, cpBody *a, cpBody *b)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
77 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
78 // Don't merge with the static body
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
79 if(cpBodyIsStatic(a) || cpBodyIsStatic(b)) return;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
80
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
81 cpBody *a_root = componentNodeRoot(a);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
82 cpBody *b_root = componentNodeRoot(b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
83
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
84 cpBool a_sleep = cpBodyIsSleeping(a_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
85 cpBool b_sleep = cpBodyIsSleeping(b_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
86
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
87 if(a_sleep && b_sleep){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
88 return;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
89 } else if(a_sleep || b_sleep){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
90 componentActivate(a_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
91 componentActivate(b_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
92 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
93
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
94 // Add any rogue bodies (bodies not added to the space)
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
95 if(cpBodyIsRogue(a)) cpArrayPush(rogueBodies, a);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
96 if(cpBodyIsRogue(b)) cpArrayPush(rogueBodies, b);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
97
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
98 componentNodeMerge(a_root, b_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
99 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
100
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
101 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
102 componentActive(cpBody *root, cpFloat threshold)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
103 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
104 cpBody *_body = root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
105 cpBody *next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
106 do {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
107 next = _body.node.next;
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
108 if(_body.node.idleTime < threshold) return cpTrue;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
109 } while((_body = next) != root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
110
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
111 return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
112 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
113
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
114 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
115 addToComponent(cpBody *_body, cpArray *components)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
116 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
117 // Check that the body is not already added to the component list
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
118 if(_body.node.next) return;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
119 cpBody *root = componentNodeRoot(_body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
120
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
121 cpBody *next = root.node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
122 if(!next){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
123 // If the root isn't part of a list yet, then it hasn't been
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
124 // added to the components list. Do that now.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
125 cpArrayPush(components, root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
126 // Start the list
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
127 _body.node.next = root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
128 root.node.next = _body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
129 } else if(root != _body) {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
130 // Splice in body after the root.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
131 _body.node.next = next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
132 root.node.next = _body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
133 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
134 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
135
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
136 // TODO this function needs more commenting.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
137 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
138 cpSpaceProcessComponents(cpSpace *space, cpFloat dt)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
139 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
140 cpArray *bodies = space.bodies;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
141 cpArray *newBodies = cpArrayNew(bodies.num);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
142 cpArray *rogueBodies = cpArrayNew(16);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
143 cpArray *arbiters = space.arbiters;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
144 cpArray *constraints = space.constraints;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
145 cpArray *components = cpArrayNew(bodies.num/8);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
146
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
147 cpFloat dv = space.idleSpeedThreshold;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
148 cpFloat dvsq = (dv ? dv*dv : cpvdot(space.gravity, space.gravity)*dt*dt);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
149 // update idling
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
150 for(int i=0; i<bodies.num; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
151 cpBody *_body = cast(cpBody*)bodies.arr[i];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
152
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
153 cpFloat thresh = (dvsq ? _body.m*dvsq : 0.0f);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
154 _body.node.idleTime = (cpBodyKineticEnergy(_body) > thresh ? 0.0f : _body.node.idleTime + dt);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
155 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
156
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
157 // iterate graph edges and build forests
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
158 for(int i=0; i<arbiters.num; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
159 cpArbiter *arb = cast(cpArbiter*)arbiters.arr[i];
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
160 mergeBodies(space, components, rogueBodies, arb.a._body, arb.b._body);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
161 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
162 for(int j=0; j<constraints.num; j++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
163 cpConstraint *constraint = cast(cpConstraint *)constraints.arr[j];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
164 mergeBodies(space, components, rogueBodies, constraint.a, constraint.b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
165 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
166
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
167 // iterate bodies and add them to their components
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
168 for(int i=0; i<bodies.num; i++)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
169 addToComponent(cast(cpBody*)bodies.arr[i], components);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
170 for(int i=0; i<rogueBodies.num; i++)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
171 addToComponent(cast(cpBody*)rogueBodies.arr[i], components);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
172
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
173 // iterate components, copy or deactivate
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
174 for(int i=0; i<components.num; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
175 cpBody *root = cast(cpBody*)components.arr[i];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
176 if(componentActive(root, space.sleepTimeThreshold)){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
177 cpBody *_body = root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
178 cpBody *next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
179 do {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
180 next = _body.node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
181
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
182 if(!cpBodyIsRogue(_body)) cpArrayPush(newBodies, _body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
183 _body.node.next = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
184 _body.node.parent = null;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
185 _body.node.rank = 0;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
186 } while((_body = next) != root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
187 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
188 cpBody *_body = root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
189 cpBody *next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
190 do {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
191 next = _body.node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
192
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
193 for(cpShape *shape = _body.shapesList; shape; shape = shape.next){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
194 cpSpaceHashRemove(space.activeShapes, shape, shape.hashid);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
195 cpSpaceHashInsert(space.staticShapes, shape, shape.hashid, shape.bb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
196 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
197 } while((_body = next) != root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
198
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
199 cpArrayPush(space.sleepingComponents, root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
200 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
201 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
202
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
203 space.bodies = newBodies;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
204 cpArrayFree(bodies);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
205 cpArrayFree(rogueBodies);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
206 cpArrayFree(components);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
207 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
208
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
209 void
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
210 cpBodySleep(cpBody *_body)
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
211 {
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
212 cpBodySleepWithGroup(_body, null);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
213 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
214
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
215 void
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
216 cpBodySleepWithGroup(cpBody *_body, cpBody *group){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
217 assert(!cpBodyIsStatic(_body) && !cpBodyIsRogue(_body), "Rogue and static bodies cannot be put to sleep.");
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
218
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
219 cpSpace *space = _body.space;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
220 assert(space, "Cannot put a body to sleep that has not been added to a space.");
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
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.");
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
222 assert(!group || cpBodyIsSleeping(group), "Cannot use a non-sleeping body as a group identifier.");
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
223
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
224 if(cpBodyIsSleeping(_body)) return;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
225
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
226 for(cpShape *shape = _body.shapesList; shape; shape = shape.next){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
227 cpSpaceHashRemove(space.activeShapes, shape, shape.hashid);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
228
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
229 cpShapeCacheBB(shape);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
230 cpSpaceHashInsert(space.staticShapes, shape, shape.hashid, shape.bb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
231 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
232
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
233 if(group){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
234 cpBody *root = componentNodeRoot(group);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
235
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
236 cpComponentNode node = {root, root.node.next, 0, 0.0f};
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
237 _body.node = node;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
238 root.node.next = _body;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
239 } else {
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
240 cpComponentNode node = {null, _body, 0, 0.0f};
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
241 _body.node = node;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
242
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
243 cpArrayPush(space.sleepingComponents, _body);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
244 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
245
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
246 cpArrayDeleteObj(space.bodies, _body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
247 }
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
248
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
249 static void
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
250 activateTouchingHelper(cpShape *shape, cpContactPointSet *points, cpArray **bodies){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
251 if(*bodies == null) (*bodies) = cpArrayNew(0);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
252 cpArrayPush(*bodies, shape._body);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
253 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
254
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
255 void
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
256 cpSpaceActivateShapesTouchingShape(cpSpace *space, cpShape *shape){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
257 cpArray *bodies = null;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
258 cpSpaceShapeQuery(space, shape, cast(cpSpaceShapeQueryFunc)&activateTouchingHelper, &bodies);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
259
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
260 if(bodies){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
261 for(int i=0; i<bodies.num; i++){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
262 cpBody *_body = cast(cpBody *)bodies.arr[i];
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
263 cpBodyActivate(_body);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
264 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
265 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
266 }