Mercurial > projects > chipmunkd
comparison trunk/chipmunkd/cpArbiter.d @ 23:4ceef5833c8c
updated to chipmunk 5.3.3
author | Extrawurst |
---|---|
date | Fri, 10 Dec 2010 02:10:27 +0100 |
parents | df4ebc8add66 |
children | 4541ca17975b |
comparison
equal
deleted
inserted
replaced
22:ed2c81f3d1df | 23:4ceef5833c8c |
---|---|
35 // | 35 // |
36 //// Sum the contact impulses. (Can be used after cpSpaceStep() returns) | 36 //// Sum the contact impulses. (Can be used after cpSpaceStep() returns) |
37 //cpVect cpContactsSumImpulses(cpContact *contacts, int numContacts); | 37 //cpVect cpContactsSumImpulses(cpContact *contacts, int numContacts); |
38 //cpVect cpContactsSumImpulsesWithFriction(cpContact *contacts, int numContacts); | 38 //cpVect cpContactsSumImpulsesWithFriction(cpContact *contacts, int numContacts); |
39 | 39 |
40 enum CP_MAX_CONTACTS_PER_ARBITER = 6; | |
41 | |
40 enum cpArbiterState { | 42 enum cpArbiterState { |
41 cpArbiterStateNormal, | 43 cpArbiterStateNormal, |
42 cpArbiterStateFirstColl, | 44 cpArbiterStateFirstColl, |
43 cpArbiterStateIgnore, | 45 cpArbiterStateIgnore, |
44 cpArbiterStateSleep, | 46 cpArbiterStateSleep, |
53 | 55 |
54 // The two shapes and bodies involved in the collision. | 56 // The two shapes and bodies involved in the collision. |
55 // These variables are NOT in the order defined by the collision handler. | 57 // These variables are NOT in the order defined by the collision handler. |
56 // Using CP_ARBITER_GET_SHAPES and CP_ARBITER_GET_BODIES will save you from | 58 // Using CP_ARBITER_GET_SHAPES and CP_ARBITER_GET_BODIES will save you from |
57 // many headaches | 59 // many headaches |
58 cpShape* private_a; | 60 cpShape* a; |
59 cpShape* private_b; | 61 cpShape* b; |
60 | 62 |
61 // Calculated before calling the pre-solve collision handler | 63 // Calculated before calling the pre-solve collision handler |
62 // Override them with custom values if you want specialized behavior | 64 // Override them with custom values if you want specialized behavior |
63 cpFloat e; | 65 cpFloat e; |
64 cpFloat u; | 66 cpFloat u; |
95 | 97 |
96 static void | 98 static void |
97 cpArbiterGetShapes(/+const+/ cpArbiter *arb, cpShape **a, cpShape **b) | 99 cpArbiterGetShapes(/+const+/ cpArbiter *arb, cpShape **a, cpShape **b) |
98 { | 100 { |
99 if(arb.swappedColl){ | 101 if(arb.swappedColl){ |
100 (*a) = arb.private_b, (*b) = arb.private_a; | 102 (*a) = arb.b, (*b) = arb.a; |
101 } else { | 103 } else { |
102 (*a) = arb.private_a, (*b) = arb.private_b; | 104 (*a) = arb.a, (*b) = arb.b; |
103 } | 105 } |
104 } | 106 } |
105 | 107 |
106 template CP_ARBITER_GET_SHAPES(string arb,string a,string b) | 108 template CP_ARBITER_GET_SHAPES(string arb,string a,string b) |
107 { | 109 { |
123 cpArbiterIsFirstContact(const cpArbiter *arb) | 125 cpArbiterIsFirstContact(const cpArbiter *arb) |
124 { | 126 { |
125 return arb.state == cpArbiterState.cpArbiterStateFirstColl; | 127 return arb.state == cpArbiterState.cpArbiterStateFirstColl; |
126 } | 128 } |
127 | 129 |
130 static int | |
131 cpArbiterGetCount(const cpArbiter *arb) | |
132 { | |
133 return arb.numContacts; | |
134 } | |
135 | |
128 static cpVect | 136 static cpVect |
129 cpArbiterGetNormal(const cpArbiter *arb, int i) | 137 cpArbiterGetNormal(const cpArbiter *arb, int i) |
130 { | 138 { |
131 cpVect n = arb.contacts[i].n; | 139 cpVect n = arb.contacts[i].n; |
132 return arb.swappedColl ? cpvneg(n) : n; | 140 return arb.swappedColl ? cpvneg(n) : n; |
135 static cpVect | 143 static cpVect |
136 cpArbiterGetPoint(const cpArbiter *arb, int i) | 144 cpArbiterGetPoint(const cpArbiter *arb, int i) |
137 { | 145 { |
138 return arb.contacts[i].p; | 146 return arb.contacts[i].p; |
139 } | 147 } |
148 | |
149 | |
150 static cpFloat | |
151 cpArbiteGetDepth(const cpArbiter *arb, int i) | |
152 { | |
153 return arb.contacts[i].dist; | |
154 } | |
155 | |
156 struct cpContactPointSet { | |
157 int count; | |
158 | |
159 struct TPoint | |
160 { | |
161 cpVect point, normal; | |
162 cpFloat dist = 0; | |
163 } | |
164 | |
165 TPoint[CP_MAX_CONTACTS_PER_ARBITER] points; | |
166 } | |
167 | |
168 static cpContactPointSet | |
169 cpArbiterGetContactPointSet(const cpArbiter *arb) | |
170 { | |
171 cpContactPointSet set; | |
172 set.count = cpArbiterGetCount(arb); | |
173 | |
174 int i; | |
175 for(i=0; i<set.count; i++){ | |
176 set.points[i].point = arb.contacts[i].p; | |
177 set.points[i].normal = arb.contacts[i].p; | |
178 set.points[i].dist = arb.contacts[i].dist; | |
179 } | |
180 | |
181 return set; | |
182 } | |
183 | |
184 // cpArbiter.c -------------------------- | |
140 | 185 |
141 cpFloat cp_bias_coef = 0.1f; | 186 cpFloat cp_bias_coef = 0.1f; |
142 cpFloat cp_collision_slop = 0.1f; | 187 cpFloat cp_collision_slop = 0.1f; |
143 | 188 |
144 cpContact* | 189 cpContact* |
216 } | 261 } |
217 | 262 |
218 cpArbiter* | 263 cpArbiter* |
219 cpArbiterInit(cpArbiter *arb, cpShape *a, cpShape *b) | 264 cpArbiterInit(cpArbiter *arb, cpShape *a, cpShape *b) |
220 { | 265 { |
266 arb.handler = null; | |
267 arb.swappedColl = cpFalse; | |
268 | |
269 arb.e = 0.0f; | |
270 arb.u = 0.0f; | |
271 arb.surface_vr = cpvzero; | |
272 | |
221 arb.numContacts = 0; | 273 arb.numContacts = 0; |
222 arb.contacts = null; | 274 arb.contacts = null; |
223 | 275 |
224 arb.private_a = a; | 276 arb.a = a; |
225 arb.private_b = b; | 277 arb.b = b; |
226 | 278 |
227 arb.stamp = 0; | 279 arb.stamp = 0; |
228 arb.state = cpArbiterState.cpArbiterStateFirstColl; | 280 arb.state = cpArbiterState.cpArbiterStateFirstColl; |
229 | 281 |
230 return arb; | 282 return arb; |
269 new_contact.jnAcc = old.jnAcc; | 321 new_contact.jnAcc = old.jnAcc; |
270 new_contact.jtAcc = old.jtAcc; | 322 new_contact.jtAcc = old.jtAcc; |
271 } | 323 } |
272 } | 324 } |
273 } | 325 } |
274 | |
275 // cpfree(arb.contacts); | |
276 } | 326 } |
277 | 327 |
278 arb.contacts = contacts; | 328 arb.contacts = contacts; |
279 arb.numContacts = numContacts; | 329 arb.numContacts = numContacts; |
280 | 330 |
284 arb.e = a.e * b.e; | 334 arb.e = a.e * b.e; |
285 arb.u = a.u * b.u; | 335 arb.u = a.u * b.u; |
286 arb.surface_vr = cpvsub(a.surface_v, b.surface_v); | 336 arb.surface_vr = cpvsub(a.surface_v, b.surface_v); |
287 | 337 |
288 // For collisions between two similar primitive types, the order could have been swapped. | 338 // For collisions between two similar primitive types, the order could have been swapped. |
289 arb.private_a = a; | 339 arb.a = a; |
290 arb.private_b = b; | 340 arb.b = b; |
291 | 341 |
292 // mark it as new if it's been cached | 342 // mark it as new if it's been cached |
293 if(arb.state == cpArbiterState.cpArbiterStateCached) arb.state = cpArbiterState.cpArbiterStateFirstColl; | 343 if(arb.state == cpArbiterState.cpArbiterStateCached) arb.state = cpArbiterState.cpArbiterStateFirstColl; |
294 } | 344 } |
295 | 345 |
296 void | 346 void |
297 cpArbiterPreStep(cpArbiter *arb, cpFloat dt_inv) | 347 cpArbiterPreStep(cpArbiter *arb, cpFloat dt_inv) |
298 { | 348 { |
299 cpBody *a = arb.private_a._body; | 349 cpBody *a = arb.a._body; |
300 cpBody *b = arb.private_b._body; | 350 cpBody *b = arb.b._body; |
301 | 351 |
302 for(int i=0; i<arb.numContacts; i++){ | 352 for(int i=0; i<arb.numContacts; i++){ |
303 cpContact *con = &arb.contacts[i]; | 353 cpContact *con = &arb.contacts[i]; |
304 | 354 |
305 // Calculate the offsets. | 355 // Calculate the offsets. |
320 } | 370 } |
321 | 371 |
322 void | 372 void |
323 cpArbiterApplyCachedImpulse(cpArbiter *arb) | 373 cpArbiterApplyCachedImpulse(cpArbiter *arb) |
324 { | 374 { |
325 cpShape *shapea = arb.private_a; | 375 cpShape *shapea = arb.a; |
326 cpShape *shapeb = arb.private_b; | 376 cpShape *shapeb = arb.b; |
327 | 377 |
328 arb.u = shapea.u * shapeb.u; | 378 arb.u = shapea.u * shapeb.u; |
329 arb.surface_vr = cpvsub(shapeb.surface_v, shapea.surface_v); | 379 arb.surface_vr = cpvsub(shapeb.surface_v, shapea.surface_v); |
330 | 380 |
331 cpBody *a = shapea._body; | 381 cpBody *a = shapea._body; |
338 } | 388 } |
339 | 389 |
340 void | 390 void |
341 cpArbiterApplyImpulse(cpArbiter *arb, cpFloat eCoef) | 391 cpArbiterApplyImpulse(cpArbiter *arb, cpFloat eCoef) |
342 { | 392 { |
343 cpBody *a = arb.private_a._body; | 393 cpBody *a = arb.a._body; |
344 cpBody *b = arb.private_b._body; | 394 cpBody *b = arb.b._body; |
345 | 395 |
346 for(int i=0; i<arb.numContacts; i++){ | 396 for(int i=0; i<arb.numContacts; i++){ |
347 cpContact *con = &arb.contacts[i]; | 397 cpContact *con = &arb.contacts[i]; |
348 cpVect n = con.n; | 398 cpVect n = con.n; |
349 cpVect r1 = con.r1; | 399 cpVect r1 = con.r1; |