Debian Codegraph @

src/cpHastySpace.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2013 Howling Moon Software. All rights reserved.
2 // See http://chipmunk2d.net/legal.php for more information.
3
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 //TODO: Move all the thread stuff to another file
8
9 //#include <sys/param.h >
10 #ifndef _WIN32
11 #include <sys/sysctl.h>
12 #include <pthread.h>
13 #else
14 #ifndef WIN32_LEAN_AND_MEAN
15 #define WIN32_LEAN_AND_MEAN
16 #endif
17
18 #ifndef NOMINMAX
19 #define NOMINMAX
20 #endif
21
22 #include <process.h> // _beginthreadex
23 #include <windows.h>
24
25 #ifndef ETIMEDOUT
26 #define ETIMEDOUT 1
27 #endif
28
29 // Simple pthread implementation for Windows
30 // Made from scratch to avoid the LGPL licence from pthread-win32
31 enum {
32 SIGNAL = 0,
33 BROADCAST = 1,
34 MAX_EVENTS = 2
35 };
36
37 typedef HANDLE pthread_t;
38 typedef struct
39 {
40 // Based on http://www.cs.wustl.edu/~schmidt/win32-cv-1.html since Windows has no condition variable until NT6
41 UINT waiters_count;
42 // Count of the number of waiters.
43
44 CRITICAL_SECTION waiters_count_lock;
45 // Serialize access to <waiters_count_>.
46
47 HANDLE events[MAX_EVENTS];
48 } pthread_cond_t;
49 typedef CRITICAL_SECTION pthread_mutex_t;
50
51 typedef struct {} pthread_condattr_t; // Dummy;
52
53 int pthread_cond_destroy(pthread_cond_t* cv)
54 {
55 CloseHandle(cv->events[BROADCAST]);
56 CloseHandle(cv->events[SIGNAL]);
57
58 DeleteCriticalSection(&cv->waiters_count_lock);
59
60 return 0;
61 }
62
63 int pthread_cond_init(pthread_cond_t* cv, const pthread_condattr_t* attr)
64 {
65 // Initialize the count to 0.
66 cv->waiters_count = 0;
67
68 // Create an auto-reset event.
69 cv->events[SIGNAL] = CreateEvent(NULL, // no security
70 FALSE, // auto-reset event
71 FALSE, // non-signaled initially
72 NULL); // unnamed
73
74 // Create a manual-reset event.
75 cv->events[BROADCAST] = CreateEvent(NULL, // no security
76 TRUE, // manual-reset
77 FALSE, // non-signaled initially
78 NULL); // unnamed
79
80 InitializeCriticalSection(&cv->waiters_count_lock);
81
82 return 0;
83 }
84
85 int pthread_cond_broadcast(pthread_cond_t *cv)
86 {
87 // Avoid race conditions.
88 EnterCriticalSection(&cv->waiters_count_lock);
89 int have_waiters = cv->waiters_count > 0;
90 LeaveCriticalSection(&cv->waiters_count_lock);
91
92 if (have_waiters)
93 SetEvent(cv->events[BROADCAST]);
94
95 return 0;
96 }
97
98 int pthread_cond_signal(pthread_cond_t* cv)
99 {
100 // Avoid race conditions.
101 EnterCriticalSection(&cv->waiters_count_lock);
102 int have_waiters = cv->waiters_count > 0;
103 LeaveCriticalSection(&cv->waiters_count_lock);
104
105 if (have_waiters)
106 SetEvent(cv->events[SIGNAL]);
107
108 return 0;
109 }
110
111 int pthread_cond_wait(pthread_cond_t* cv, pthread_mutex_t* external_mutex)
112 {
113 // Avoid race conditions.
114 EnterCriticalSection(&cv->waiters_count_lock);
115 cv->waiters_count++;
116 LeaveCriticalSection(&cv->waiters_count_lock);
117
118 // It's ok to release the <external_mutex> here since Win32
119 // manual-reset events maintain state when used with
120 // <SetEvent>. This avoids the "lost wakeup" bug...
121 LeaveCriticalSection(external_mutex);
122
123 // Wait for either event to become signaled due to <pthread_cond_signal>
124 // being called or <pthread_cond_broadcast> being called.
125 int result = WaitForMultipleObjects(2, cv->events, FALSE, INFINITE);
126
127 EnterCriticalSection(&cv->waiters_count_lock);
128 cv->waiters_count--;
129 int last_waiter =
130 result == WAIT_OBJECT_0 + BROADCAST
131 && cv->waiters_count == 0;
132 LeaveCriticalSection(&cv->waiters_count_lock);
133
134 // Some thread called <pthread_cond_broadcast>.
135 if (last_waiter)
136 // We're the last waiter to be notified or to stop waiting, so
137 // reset the manual event.
138 ResetEvent(cv->events[BROADCAST]);
139
140 // Reacquire the <external_mutex>.
141 EnterCriticalSection(external_mutex);
142
143 return result == WAIT_TIMEOUT ? ETIMEDOUT : 0;
144 }
145
146 typedef struct {} pthread_mutexattr_t; //< Dummy
147
148 int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr)
149 {
150 InitializeCriticalSection(mutex);
151 return 0;
152 }
153
154 int pthread_mutex_destroy(pthread_mutex_t* mutex)
155 {
156 DeleteCriticalSection(mutex);
157 return 0;
158 }
159
160 int pthread_mutex_lock(pthread_mutex_t* mutex)
161 {
162 EnterCriticalSection(mutex);
163 return 0;
164 }
165
166 int pthread_mutex_unlock(pthread_mutex_t* mutex)
167 {
168 LeaveCriticalSection(mutex);
169 return 0;
170 }
171
172 typedef struct {} pthread_attr_t;
173
174 typedef struct
175 {
176 void *(*start_routine) (void *);
177 void* arg;
178 } pthread_internal_thread;
179
180 unsigned int __stdcall ThreadProc(void* userdata)
181 {
182 pthread_internal_thread* ud = (pthread_internal_thread*) userdata;
183 ud->start_routine(ud->arg);
184
185 free(ud);
186
187 return 0;
188 }
189
190 int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void *(*start_routine) (void *), void *arg)
191 {
192 pthread_internal_thread* ud = (pthread_internal_thread*) malloc(sizeof(pthread_internal_thread));
193 ud->start_routine = start_routine;
194 ud->arg = arg;
195
196 *thread = (HANDLE) (_beginthreadex(NULL, 0, &ThreadProc, ud, 0, NULL));
197 if (!*thread)
198 return 1;
199
200 return 0;
201 }
202
203 int pthread_join(pthread_t thread, void **value_ptr)
204 {
205 WaitForSingleObject(thread, INFINITE);
206 CloseHandle(thread);
207
208 return 0;
209 }
210
211 #endif
212
213 #include "chipmunk/chipmunk_private.h"
214 #include "chipmunk/cpHastySpace.h"
215
216
217 //MARK: ARM NEON Solver
218
219 #if __ARM_NEON__
220 #include <arm_neon.h>
221
222 // Tested and known to work fine with Clang 3.0 and GCC 4.2
223 // Doesn't work with Clang 1.6, and I have no idea why.
224 #if defined(__clang_major__) && __clang_major__ < 3
225 #error Compiler not supported.
226 #endif
227
228 #if CP_USE_DOUBLES
229 #if !__arm64
230 #error Cannot use CP_USE_DOUBLES on 32 bit ARM.
231 #endif
232
233 typedef float64_t cpFloat_t;
234 typedef float64x2_t cpFloatx2_t;
235 #define vld vld1q_f64
236 #define vdup_n vdupq_n_f64
237 #define vst vst1q_f64
238 #define vst_lane vst1q_lane_f64
239 #define vadd vaddq_f64
240 #define vsub vsubq_f64
241 #define vpadd vpaddq_f64
242 #define vmul vmulq_f64
243 #define vmul_n vmulq_n_f64
244 #define vneg vnegq_f64
245 #define vget_lane vgetq_lane_f64
246 #define vset_lane vsetq_lane_f64
247 #define vmin vminq_f64
248 #define vmax vmaxq_f64
249 #define vrev(__a) __builtin_shufflevector(__a, __a, 1, 0)
250 #else
251 typedef float32_t cpFloat_t;
252 typedef float32x2_t cpFloatx2_t;
253 #define vld vld1_f32
254 #define vdup_n vdup_n_f32
255 #define vst vst1_f32
256 #define vst_lane vst1_lane_f32
257 #define vadd vadd_f32
258 #define vsub vsub_f32
259 #define vpadd vpadd_f32
260 #define vmul vmul_f32
261 #define vmul_n vmul_n_f32
262 #define vneg vneg_f32
263 #define vget_lane vget_lane_f32
264 #define vset_lane vset_lane_f32
265 #define vmin vmin_f32
266 #define vmax vmax_f32
267 #define vrev vrev64_f32
268 #endif
269
270 // TODO could probably do better here, maybe using vcreate?
271 // especially for the constants
272 // Maybe use the {} notation for GCC/Clang?
273 static inline cpFloatx2_t
274 vmake(cpFloat_t x, cpFloat_t y)
275 {
276 // cpFloatx2_t v = {};
277 // v = vset_lane(x, v, 0);
278 // v = vset_lane(y, v, 1);
279 //
280 // return v;
281
282 // This might not be super compatible, but all the NEON headers use it...
283 return (cpFloatx2_t){x, y};
284 }
285
286 static void
287 cpArbiterApplyImpulse_NEON(cpArbiter *arb)
288 {
289 cpBody *a = arb->body_a;
290 cpBody *b = arb->body_b;
291 cpFloatx2_t surface_vr = vld((cpFloat_t *)&arb->surface_vr);
292 cpFloatx2_t n = vld((cpFloat_t *)&arb->n);
293 cpFloat_t friction = arb->u;
294
295 int numContacts = arb->count;
296 struct cpContact *contacts = arb->contacts;
297 for(int i=0; i<numContacts; i++){
298 struct cpContact *con = contacts + i;
299 cpFloatx2_t r1 = vld((cpFloat_t *)&con->r1);
300 cpFloatx2_t r2 = vld((cpFloat_t *)&con->r2);
301
302 cpFloatx2_t perp = vmake(-1.0, 1.0);
303 cpFloatx2_t r1p = vmul(vrev(r1), perp);
304 cpFloatx2_t r2p = vmul(vrev(r2), perp);
305
306 cpFloatx2_t vBias_a = vld((cpFloat_t *)&a->v_bias);
307 cpFloatx2_t vBias_b = vld((cpFloat_t *)&b->v_bias);
308 cpFloatx2_t wBias = vmake(a->w_bias, b->w_bias);
309
310 cpFloatx2_t vb1 = vadd(vBias_a, vmul_n(r1p, vget_lane(wBias, 0)));
311 cpFloatx2_t vb2 = vadd(vBias_b, vmul_n(r2p, vget_lane(wBias, 1)));
312 cpFloatx2_t vbr = vsub(vb2, vb1);
313
314 cpFloatx2_t v_a = vld((cpFloat_t *)&a->v);
315 cpFloatx2_t v_b = vld((cpFloat_t *)&b->v);
316 cpFloatx2_t w = vmake(a->w, b->w);
317 cpFloatx2_t v1 = vadd(v_a, vmul_n(r1p, vget_lane(w, 0)));
318 cpFloatx2_t v2 = vadd(v_b, vmul_n(r2p, vget_lane(w, 1)));
319 cpFloatx2_t vr = vsub(v2, v1);
320
321 cpFloatx2_t vbn_vrn = vpadd(vmul(vbr, n), vmul(vr, n));
322
323 cpFloatx2_t v_offset = vmake(con->bias, -con->bounce);
324 cpFloatx2_t jOld = vmake(con->jBias, con->jnAcc);
325 cpFloatx2_t jbn_jn = vmul_n(vsub(v_offset, vbn_vrn), con->nMass);
326 jbn_jn = vmax(vadd(jOld, jbn_jn), vdup_n(0.0));
327 cpFloatx2_t jApply = vsub(jbn_jn, jOld);
328
329 cpFloatx2_t t = vmul(vrev(n), perp);
330 cpFloatx2_t vrt_tmp = vmul(vadd(vr, surface_vr), t);
331 cpFloatx2_t vrt = vpadd(vrt_tmp, vrt_tmp);
332
333 cpFloatx2_t jtOld = {}; jtOld = vset_lane(con->jtAcc, jtOld, 0);
334 cpFloatx2_t jtMax = vrev(vmul_n(jbn_jn, friction));
335 cpFloatx2_t jt = vmul_n(vrt, -con->tMass);
336 jt = vmax(vneg(jtMax), vmin(vadd(jtOld, jt), jtMax));
337 cpFloatx2_t jtApply = vsub(jt, jtOld);
338
339 cpFloatx2_t i_inv = vmake(-a->i_inv, b->i_inv);
340 cpFloatx2_t nperp = vmake(1.0, -1.0);
341
342 cpFloatx2_t jBias = vmul_n(n, vget_lane(jApply, 0));
343 cpFloatx2_t jBiasCross = vmul(vrev(jBias), nperp);
344 cpFloatx2_t biasCrosses = vpadd(vmul(r1, jBiasCross), vmul(r2, jBiasCross));
345 wBias = vadd(wBias, vmul(i_inv, biasCrosses));
346
347 vBias_a = vsub(vBias_a, vmul_n(jBias, a->m_inv));
348 vBias_b = vadd(vBias_b, vmul_n(jBias, b->m_inv));
349
350 cpFloatx2_t j = vadd(vmul_n(n, vget_lane(jApply, 1)), vmul_n(t, vget_lane(jtApply, 0)));
351 cpFloatx2_t jCross = vmul(vrev(j), nperp);
352 cpFloatx2_t crosses = vpadd(vmul(r1, jCross), vmul(r2, jCross));
353 w = vadd(w, vmul(i_inv, crosses));
354
355 v_a = vsub(v_a, vmul_n(j, a->m_inv));
356 v_b = vadd(v_b, vmul_n(j, b->m_inv));
357
358 // TODO would moving these earlier help pipeline them better?
359 vst((cpFloat_t *)&a->v_bias, vBias_a);
360 vst((cpFloat_t *)&b->v_bias, vBias_b);
361 vst_lane((cpFloat_t *)&a->w_bias, wBias, 0);
362 vst_lane((cpFloat_t *)&b->w_bias, wBias, 1);
363
364 vst((cpFloat_t *)&a->v, v_a);
365 vst((cpFloat_t *)&b->v, v_b);
366 vst_lane((cpFloat_t *)&a->w, w, 0);
367 vst_lane((cpFloat_t *)&b->w, w, 1);
368
369 vst_lane((cpFloat_t *)&con->jBias, jbn_jn, 0);
370 vst_lane((cpFloat_t *)&con->jnAcc, jbn_jn, 1);
371 vst_lane((cpFloat_t *)&con->jtAcc, jt, 0);
372 }
373 }
374
375 #endif
376
377 //MARK: PThreads
378
379 // Right now using more than 2 threads probably wont help your performance any.
380 // If you are using a ridiculous number of iterations it could help though.
381 #define MAX_THREADS 2
382
383 struct ThreadContext {
384 pthread_t thread;
385 cpHastySpace *space;
386 unsigned long thread_num;
387 };
388
389 typedef void (*cpHastySpaceWorkFunction)(cpSpace *space, unsigned long worker, unsigned long worker_count);
390
391 struct cpHastySpace {
392 cpSpace space;
393
394 // Number of worker threads (including the main thread)
395 unsigned long num_threads;
396
397 // Number of worker threads currently executing. (also including the main thread)
398 unsigned long num_working;
399
400 // Number of constraints (plus contacts) that must exist per step to start the worker threads.
401 unsigned long constraint_count_threshold;
402
403 pthread_mutex_t mutex;
404 pthread_cond_t cond_work, cond_resume;
405
406 // Work function to invoke.
407 cpHastySpaceWorkFunction work;
408
409 struct ThreadContext workers[MAX_THREADS - 1];
410 };
411
412 static void *
413 WorkerThreadLoop(struct ThreadContext *context)
414 {
415 cpHastySpace *hasty = context->space;
416
417 unsigned long thread = context->thread_num;
418 unsigned long num_threads = hasty->num_threads;
419
420 for(;;){
421 pthread_mutex_lock(&hasty->mutex); {
422 if(--hasty->num_working == 0){
423 pthread_cond_signal(&hasty->cond_resume);
424 }
425
426 pthread_cond_wait(&hasty->cond_work, &hasty->mutex);
427 } pthread_mutex_unlock(&hasty->mutex);
428
429 cpHastySpaceWorkFunction func = hasty->work;
430 if(func){
431 hasty->work(&hasty->space, thread, num_threads);
432 } else {
433 break;
434 }
435 }
436
437 return NULL;
438 }
439
440 static void
441 RunWorkers(cpHastySpace *hasty, cpHastySpaceWorkFunction func)
442 {
443 hasty->num_working = hasty->num_threads - 1;
444 hasty->work = func;
445
446 if(hasty->num_working > 0){
447 pthread_mutex_lock(&hasty->mutex); {
448 pthread_cond_broadcast(&hasty->cond_work);
449 } pthread_mutex_unlock(&hasty->mutex);
450
451 func((cpSpace *)hasty, 0, hasty->num_threads);
452
453 pthread_mutex_lock(&hasty->mutex); {
454 if(hasty->num_working > 0){
455 pthread_cond_wait(&hasty->cond_resume, &hasty->mutex);
456 }
457 } pthread_mutex_unlock(&hasty->mutex);
458 } else {
459 func((cpSpace *)hasty, 0, hasty->num_threads);
460 }
461
462 hasty->work = NULL;
463 }
464
465 static void
466 Solver(cpSpace *space, unsigned long worker, unsigned long worker_count)
467 {
468 cpArray *constraints = space->constraints;
469 cpArray *arbiters = space->arbiters;
470
471 cpFloat dt = space->curr_dt;
472 unsigned long iterations = (space->iterations + worker_count - 1)/worker_count;
473
474 for(unsigned long i=0; i<iterations; i++){
475 for(int j=0; j<arbiters->num; j++){
476 cpArbiter *arb = (cpArbiter *)arbiters->arr[j];
477 #ifdef __ARM_NEON__
478 cpArbiterApplyImpulse_NEON(arb);
479 #else
480 cpArbiterApplyImpulse(arb);
481 #endif
482 }
483
484 for(int j=0; j<constraints->num; j++){
485 cpConstraint *constraint = (cpConstraint *)constraints->arr[j];
486 constraint->klass->applyImpulse(constraint, dt);
487 }
488 }
489 }
490
491 //MARK: Thread Management Functions
492
493 static void
494 HaltThreads(cpHastySpace *hasty)
495 {
496 pthread_mutex_t *mutex = &hasty->mutex;
497 pthread_mutex_lock(mutex); {
498 hasty->work = NULL; // NULL work function means break and exit
499 pthread_cond_broadcast(&hasty->cond_work);
500 } pthread_mutex_unlock(mutex);
501
502 for(unsigned long i=0; i<(hasty->num_threads-1); i++){
503 pthread_join(hasty->workers[i].thread, NULL);
504 }
505 }
506
507 void
508 cpHastySpaceSetThreads(cpSpace *space, unsigned long threads)
509 {
510 #if TARGET_IPHONE_SIMULATOR == 1
511 // Individual values appear to be written non-atomically when compiled as debug for the simulator.
512 // No idea why, so threads are disabled.
513 threads = 1;
514 #endif
515
516 cpHastySpace *hasty = (cpHastySpace *)space;
517 HaltThreads(hasty);
518
519 #ifdef __APPLE__
520 if(threads == 0){
521 size_t size = sizeof(threads);
522 sysctlbyname("hw.ncpu", &threads, &size, NULL, 0);
523 }
524 #else
525 if(threads == 0) threads = 1;
526 #endif
527
528 hasty->num_threads = (threads < MAX_THREADS ? threads : MAX_THREADS);
529 hasty->num_working = hasty->num_threads - 1;
530
531 // Create the worker threads and wait for them to signal ready.
532 if(hasty->num_working > 0){
533 pthread_mutex_lock(&hasty->mutex);
534 for(unsigned long i=0; i<(hasty->num_threads-1); i++){
535 hasty->workers[i].space = hasty;
536 hasty->workers[i].thread_num = i + 1;
537
538 pthread_create(&hasty->workers[i].thread, NULL, (void*(*)(void*))WorkerThreadLoop, &hasty->workers[i]);
539 }
540
541 pthread_cond_wait(&hasty->cond_resume, &hasty->mutex);
542 pthread_mutex_unlock(&hasty->mutex);
543 }
544 }
545
546 unsigned long
547 cpHastySpaceGetThreads(cpSpace *space)
548 {
549 return ((cpHastySpace *)space)->num_threads;
550 }
551
552 //MARK: Overriden cpSpace Functions.
553
554 cpSpace *
555 cpHastySpaceNew(void)
556 {
557 cpHastySpace *hasty = (cpHastySpace *)cpcalloc(1, sizeof(cpHastySpace));
558 cpSpaceInit((cpSpace *)hasty);
559
560 pthread_mutex_init(&hasty->mutex, NULL);
561 pthread_cond_init(&hasty->cond_work, NULL);
562 pthread_cond_init(&hasty->cond_resume, NULL);
563
564 // TODO magic number, should test this more thoroughly.
565 hasty->constraint_count_threshold = 50;
566
567 // Default to 1 thread for determinism.
568 hasty->num_threads = 1;
569 cpHastySpaceSetThreads((cpSpace *)hasty, 1);
570
571 return (cpSpace *)hasty;
572 }
573
574 void
575 cpHastySpaceFree(cpSpace *space)
576 {
577 cpHastySpace *hasty = (cpHastySpace *)space;
578
579 HaltThreads(hasty);
580
581 pthread_mutex_destroy(&hasty->mutex);
582 pthread_cond_destroy(&hasty->cond_work);
583 pthread_cond_destroy(&hasty->cond_resume);
584
585 cpSpaceFree(space);
586 }
587
588 void
589 cpHastySpaceStep(cpSpace *space, cpFloat dt)
590 {
591 // don't step if the timestep is 0!
592 if(dt == 0.0f) return;
593
594 space->stamp++;
595
596 cpFloat prev_dt = space->curr_dt;
597 space->curr_dt = dt;
598
599 cpArray *bodies = space->dynamicBodies;
600 cpArray *constraints = space->constraints;
601 cpArray *arbiters = space->arbiters;
602
603 // Reset and empty the arbiter list.
604 for(int i=0; i<arbiters->num; i++){
605 cpArbiter *arb = (cpArbiter *)arbiters->arr[i];
606 arb->state = CP_ARBITER_STATE_NORMAL;
607
608 // If both bodies are awake, unthread the arbiter from the contact graph.
609 if(!cpBodyIsSleeping(arb->body_a) && !cpBodyIsSleeping(arb->body_b)){
610 cpArbiterUnthread(arb);
611 }
612 }
613 arbiters->num = 0;
614
615 cpSpaceLock(space); {
616 // Integrate positions
617 for(int i=0; i<bodies->num; i++){
618 cpBody *body = (cpBody *)bodies->arr[i];
619 body->position_func(body, dt);
620 }
621
622 // Find colliding pairs.
623 cpSpacePushFreshContactBuffer(space);
624 cpSpatialIndexEach(space->dynamicShapes, (cpSpatialIndexIteratorFunc)cpShapeUpdateFunc, NULL);
625 cpSpatialIndexReindexQuery(space->dynamicShapes, (cpSpatialIndexQueryFunc)cpSpaceCollideShapes, space);
626 } cpSpaceUnlock(space, cpFalse);
627
628 // Rebuild the contact graph (and detect sleeping components if sleeping is enabled)
629 cpSpaceProcessComponents(space, dt);
630
631 cpSpaceLock(space); {
632 // Clear out old cached arbiters and call separate callbacks
633 cpHashSetFilter(space->cachedArbiters, (cpHashSetFilterFunc)cpSpaceArbiterSetFilter, space);
634
635 // Prestep the arbiters and constraints.
636 cpFloat slop = space->collisionSlop;
637 cpFloat biasCoef = 1.0f - cpfpow(space->collisionBias, dt);
638 for(int i=0; i<arbiters->num; i++){
639 cpArbiterPreStep((cpArbiter *)arbiters->arr[i], dt, slop, biasCoef);
640 }
641
642 for(int i=0; i<constraints->num; i++){
643 cpConstraint *constraint = (cpConstraint *)constraints->arr[i];
644
645 cpConstraintPreSolveFunc preSolve = constraint->preSolve;
646 if(preSolve) preSolve(constraint, space);
647
648 constraint->klass->preStep(constraint, dt);
649 }
650
651 // Integrate velocities.
652 cpFloat damping = cpfpow(space->damping, dt);
653 cpVect gravity = space->gravity;
654 for(int i=0; i<bodies->num; i++){
655 cpBody *body = (cpBody *)bodies->arr[i];
656 body->velocity_func(body, gravity, damping, dt);
657 }
658
659 // Apply cached impulses
660 cpFloat dt_coef = (prev_dt == 0.0f ? 0.0f : dt/prev_dt);
661 for(int i=0; i<arbiters->num; i++){
662 cpArbiterApplyCachedImpulse((cpArbiter *)arbiters->arr[i], dt_coef);
663 }
664
665 for(int i=0; i<constraints->num; i++){
666 cpConstraint *constraint = (cpConstraint *)constraints->arr[i];
667 constraint->klass->applyCachedImpulse(constraint, dt_coef);
668 }
669
670 // Run the impulse solver.
671 cpHastySpace *hasty = (cpHastySpace *)space;
672 if((unsigned long)(arbiters->num + constraints->num) > hasty->constraint_count_threshold){
673 RunWorkers(hasty, Solver);
674 } else {
675 Solver(space, 0, 1);
676 }
677
678 // Run the constraint post-solve callbacks
679 for(int i=0; i<constraints->num; i++){
680 cpConstraint *constraint = (cpConstraint *)constraints->arr[i];
681
682 cpConstraintPostSolveFunc postSolve = constraint->postSolve;
683 if(postSolve) postSolve(constraint, space);
684 }
685
686 // run the post-solve callbacks
687 for(int i=0; i<arbiters->num; i++){
688 cpArbiter *arb = (cpArbiter *) arbiters->arr[i];
689
690 cpCollisionHandler *handler = arb->handler;
691 handler->postSolveFunc(arb, space, handler->userData);
692 }
693 } cpSpaceUnlock(space, cpTrue);
694 }