Debian Codegraph @

src/cpHastySpace.c

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