annotate trunk/chipmunkd/cpSpaceComponent.d @ 30:34b36b5193af

commenting
author Extrawurst
date Thu, 16 Dec 2010 00:56:34 +0100
parents 80058cee1a77
children
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
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
38 void
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
39 cpSpaceActivateBody(cpSpace *space, cpBody *_body)
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
40 {
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
41 if(space.locked){
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
42 // cpSpaceActivateBody() is called again once the space is unlocked
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
43 cpArrayPush(space.rousedBodies, _body);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
44 } else {
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
45 cpArrayPush(space.bodies, _body);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
46 for(cpShape *shape=_body.shapesList; shape; shape=shape.next){
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
47 cpSpaceHashRemove(space.staticShapes, shape, shape.hashid);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
48 cpSpaceHashInsert(space.activeShapes, shape, shape.hashid, shape.bb);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
49 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
50 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
51 }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
52
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
53 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
54 componentActivate(cpBody *root)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
55 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
56 if(!cpBodyIsSleeping(root)) return;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
57
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
58 cpSpace *space = root.space;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
59 assert(space, "Trying to activate a body that was never added to a space.");
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
60
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
61 cpBody *_body = root, next;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
62 do {
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
63 next = _body.node.next;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
64
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
65 cpComponentNode node = {null, null, 0, 0.0f};
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
66 _body.node = node;
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
67
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
68 cpSpaceActivateBody(space, _body);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
69
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
70 } while((_body = next) != root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
71
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
72 cpArrayDeleteObj(space.sleepingComponents, root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
73 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
74
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
75 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
76 cpBodyActivate(cpBody *_body)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
77 {
30
34b36b5193af commenting
Extrawurst
parents: 29
diff changeset
78 //NOTE: dmd compiler bug when using -inline
34b36b5193af commenting
Extrawurst
parents: 29
diff changeset
79 //componentActivate(componentNodeRoot(_body));
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
80 auto foo = componentNodeRoot(_body);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
81 componentActivate(foo);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
82 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
83
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
84 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
85 mergeBodies(cpSpace *space, cpArray *components, cpArray *rogueBodies, cpBody *a, cpBody *b)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
86 {
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
87 // Ignore connections to static bodies
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
88 if(cpBodyIsStatic(a) || cpBodyIsStatic(b)) return;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
89
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
90 cpBody *a_root = componentNodeRoot(a);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
91 cpBody *b_root = componentNodeRoot(b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
92
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
93 cpBool a_sleep = cpBodyIsSleeping(a_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
94 cpBool b_sleep = cpBodyIsSleeping(b_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
95
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
96 if(a_sleep && b_sleep){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
97 return;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
98 } else if(a_sleep || b_sleep){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
99 componentActivate(a_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
100 componentActivate(b_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
101 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
102
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
103 // Add any rogue bodies found to the list and reset the idle time of anything they touch.
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
104 if(cpBodyIsRogue(a)){ cpArrayPush(rogueBodies, a); b.node.idleTime = 0.0f; }
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
105 if(cpBodyIsRogue(b)){ cpArrayPush(rogueBodies, b); a.node.idleTime = 0.0f; }
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
106
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
107 componentNodeMerge(a_root, b_root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
108 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
109
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
110 static cpBool
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
111 componentActive(cpBody *root, cpFloat threshold)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
112 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
113 cpBody *_body = root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
114 cpBody *next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
115 do {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
116 next = _body.node.next;
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
117 if(_body.node.idleTime < threshold) return cpTrue;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
118 } while((_body = next) != root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
119
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
120 return cpFalse;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
121 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
122
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
123 static void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
124 addToComponent(cpBody *_body, cpArray *components)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
125 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
126 // Check that the body is not already added to the component list
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
127 if(_body.node.next) return;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
128 cpBody *root = componentNodeRoot(_body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
129
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
130 cpBody *next = root.node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
131 if(!next){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
132 // If the root isn't part of a list yet, then it hasn't been
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
133 // added to the components list. Do that now.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
134 cpArrayPush(components, root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
135 // Start the list
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
136 _body.node.next = root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
137 root.node.next = _body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
138 } else if(root != _body) {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
139 // Splice in body after the root.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
140 _body.node.next = next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
141 root.node.next = _body;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
142 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
143 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
144
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
145 // TODO this function needs more commenting.
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
146 void
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
147 cpSpaceProcessComponents(cpSpace *space, cpFloat dt)
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
148 {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
149 cpArray *bodies = space.bodies;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
150 cpArray *newBodies = cpArrayNew(bodies.num);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
151 cpArray *rogueBodies = cpArrayNew(16);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
152 cpArray *arbiters = space.arbiters;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
153 cpArray *constraints = space.constraints;
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
154 cpArray *components = cpArrayNew(space.sleepingComponents.num);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
155
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
156 cpFloat dv = space.idleSpeedThreshold;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
157 cpFloat dvsq = (dv ? dv*dv : cpvdot(space.gravity, space.gravity)*dt*dt);
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
158
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
159 // update idling
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
160 for(int i=0; i<bodies.num; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
161 cpBody *_body = cast(cpBody*)bodies.arr[i];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
162
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
163 cpFloat thresh = (dvsq ? _body.m*dvsq : 0.0f);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
164 _body.node.idleTime = (cpBodyKineticEnergy(_body) > thresh ? 0.0f : _body.node.idleTime + dt);
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 graph edges and build forests
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
168 for(int i=0; i<arbiters.num; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
169 cpArbiter *arb = cast(cpArbiter*)arbiters.arr[i];
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
170 mergeBodies(space, components, rogueBodies, arb.a._body, arb.b._body);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
171 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
172 for(int j=0; j<constraints.num; j++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
173 cpConstraint *constraint = cast(cpConstraint *)constraints.arr[j];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
174 mergeBodies(space, components, rogueBodies, constraint.a, constraint.b);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
175 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
176
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
177 // iterate bodies and add them to their components
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
178 for(int i=0; i<bodies.num; i++) addToComponent(cast(cpBody*)bodies.arr[i], components);
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
179 for(int i=0; i<rogueBodies.num; i++) addToComponent(cast(cpBody*)rogueBodies.arr[i], components);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
180
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
181 // iterate components, copy or deactivate
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
182 for(int i=0; i<components.num; i++){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
183 cpBody *root = cast(cpBody*)components.arr[i];
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
184 if(componentActive(root, space.sleepTimeThreshold)){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
185 cpBody *_body = root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
186 cpBody *next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
187 do {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
188 next = _body.node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
189
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
190 if(!cpBodyIsRogue(_body)) cpArrayPush(newBodies, _body);
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
191 cpComponentNode node = {null, null, 0, _body.node.idleTime};
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
192 _body.node = node;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
193 } while((_body = next) != root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
194 } else {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
195 cpBody *_body = root;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
196 cpBody *next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
197 do {
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
198 next = _body.node.next;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
199
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
200 for(cpShape *shape = _body.shapesList; shape; shape = shape.next){
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
201 cpSpaceHashRemove(space.activeShapes, shape, shape.hashid);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
202 cpSpaceHashInsert(space.staticShapes, shape, shape.hashid, shape.bb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
203 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
204 } while((_body = next) != root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
205
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
206 cpArrayPush(space.sleepingComponents, root);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
207 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
208 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
209
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
210 space.bodies = newBodies;
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
211 cpArrayFree(bodies);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
212 cpArrayFree(rogueBodies);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
213 cpArrayFree(components);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
214 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
215
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
216 void
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
217 cpBodySleep(cpBody *_body)
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 cpBodySleepWithGroup(_body, null);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
220 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
221
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
222 void
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
223 cpBodySleepWithGroup(cpBody *_body, cpBody *group){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
224 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
225
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
226 cpSpace *space = _body.space;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
227 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
228 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
229 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
230
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
231 if(cpBodyIsSleeping(_body)) return;
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
232
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
233 for(cpShape *shape = _body.shapesList; shape; shape = shape.next){
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
234 cpShapeCacheBB(shape);
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
235 cpSpaceHashRemove(space.activeShapes, shape, shape.hashid);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
236 cpSpaceHashInsert(space.staticShapes, shape, shape.hashid, shape.bb);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
237 }
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
238
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
239 if(group){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
240 cpBody *root = componentNodeRoot(group);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
241
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
242 cpComponentNode node = {root, root.node.next, 0, 0.0f};
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
243 _body.node = node;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
244 root.node.next = _body;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
245 } else {
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
246 cpComponentNode node = {null, _body, 0, 0.0f};
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
247 _body.node = node;
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 cpArrayPush(space.sleepingComponents, _body);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
250 }
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
251
4
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
252 cpArrayDeleteObj(space.bodies, _body);
7ebbd4d05553 initial commit
Extrawurst
parents:
diff changeset
253 }
23
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 static void
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
256 activateTouchingHelper(cpShape *shape, cpContactPointSet *points, cpArray **bodies){
29
80058cee1a77 updated to chipmunk 5.3.4
Extrawurst
parents: 23
diff changeset
257 cpBodyActivate(shape._body);
23
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
258 }
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 void
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
261 cpSpaceActivateShapesTouchingShape(cpSpace *space, cpShape *shape){
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
262 cpArray *bodies = null;
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
263 cpSpaceShapeQuery(space, shape, cast(cpSpaceShapeQueryFunc)&activateTouchingHelper, &bodies);
4ceef5833c8c updated to chipmunk 5.3.3
Extrawurst
parents: 15
diff changeset
264 }