This repository has been archived by the owner on Oct 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpSpace.c
701 lines (561 loc) · 19.6 KB
/
cpSpace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "chipmunk/chipmunk_private.h"
//MARK: Contact Set Helpers
// Equal function for arbiterSet.
static cpBool
arbiterSetEql(cpShape **shapes, cpArbiter *arb)
{
cpShape *a = shapes[0];
cpShape *b = shapes[1];
return ((a == arb->a && b == arb->b) || (b == arb->a && a == arb->b));
}
//MARK: Collision Handler Set HelperFunctions
// Equals function for collisionHandlers.
static cpBool
handlerSetEql(cpCollisionHandler *check, cpCollisionHandler *pair)
{
return (
(check->typeA == pair->typeA && check->typeB == pair->typeB) ||
(check->typeB == pair->typeA && check->typeA == pair->typeB)
);
}
// Transformation function for collisionHandlers.
static void *
handlerSetTrans(cpCollisionHandler *handler, void *unused)
{
cpCollisionHandler *copy = (cpCollisionHandler *)cpcalloc(1, sizeof(cpCollisionHandler));
(*copy) = (*handler);
return copy;
}
//MARK: Misc Helper Funcs
// Default collision functions.
static cpBool
DefaultBegin(cpArbiter *arb, cpSpace *space, void *data){
cpBool retA = cpArbiterCallWildcardBeginA(arb, space);
cpBool retB = cpArbiterCallWildcardBeginB(arb, space);
return retA && retB;
}
static cpBool
DefaultPreSolve(cpArbiter *arb, cpSpace *space, void *data){
cpBool retA = cpArbiterCallWildcardPreSolveA(arb, space);
cpBool retB = cpArbiterCallWildcardPreSolveB(arb, space);
return retA && retB;
}
static void
DefaultPostSolve(cpArbiter *arb, cpSpace *space, void *data){
cpArbiterCallWildcardPostSolveA(arb, space);
cpArbiterCallWildcardPostSolveB(arb, space);
}
static void
DefaultSeparate(cpArbiter *arb, cpSpace *space, void *data){
cpArbiterCallWildcardSeparateA(arb, space);
cpArbiterCallWildcardSeparateB(arb, space);
}
// Use the wildcard identifier since the default handler should never match any type pair.
static cpCollisionHandler cpCollisionHandlerDefault = {
CP_WILDCARD_COLLISION_TYPE, CP_WILDCARD_COLLISION_TYPE,
DefaultBegin, DefaultPreSolve, DefaultPostSolve, DefaultSeparate, NULL
};
static cpBool AlwaysCollide(cpArbiter *arb, cpSpace *space, void *data){return cpTrue;}
static void DoNothing(cpArbiter *arb, cpSpace *space, void *data){}
cpCollisionHandler cpCollisionHandlerDoNothing = {
CP_WILDCARD_COLLISION_TYPE, CP_WILDCARD_COLLISION_TYPE,
AlwaysCollide, AlwaysCollide, DoNothing, DoNothing, NULL
};
// function to get the estimated velocity of a shape for the cpBBTree.
static cpVect ShapeVelocityFunc(cpShape *shape){return shape->body->v;}
// Used for disposing of collision handlers.
static void FreeWrap(void *ptr, void *unused){cpfree(ptr);}
//MARK: Memory Management Functions
cpSpace *
cpSpaceAlloc(void)
{
return (cpSpace *)cpcalloc(1, sizeof(cpSpace));
}
cpSpace*
cpSpaceInit(cpSpace *space)
{
#ifndef NDEBUG
static cpBool done = cpFalse;
if(!done){
printf("Initializing cpSpace - Chipmunk v%s (Debug Enabled)\n", cpVersionString);
printf("Compile with -DNDEBUG defined to disable debug mode and runtime assertion checks\n");
done = cpTrue;
}
#endif
space->iterations = 10;
space->gravity = cpvzero;
space->damping = 1.0f;
space->collisionSlop = 0.1f;
space->collisionBias = cpfpow(1.0f - 0.1f, 60.0f);
space->collisionPersistence = 3;
space->locked = 0;
space->stamp = 0;
space->shapeIDCounter = 0;
space->staticShapes = cpBBTreeNew((cpSpatialIndexBBFunc)cpShapeGetBB, NULL);
space->dynamicShapes = cpBBTreeNew((cpSpatialIndexBBFunc)cpShapeGetBB, space->staticShapes);
cpBBTreeSetVelocityFunc(space->dynamicShapes, (cpBBTreeVelocityFunc)ShapeVelocityFunc);
space->allocatedBuffers = cpArrayNew(0);
space->dynamicBodies = cpArrayNew(0);
space->staticBodies = cpArrayNew(0);
space->sleepingComponents = cpArrayNew(0);
space->rousedBodies = cpArrayNew(0);
space->sleepTimeThreshold = INFINITY;
space->idleSpeedThreshold = 0.0f;
space->arbiters = cpArrayNew(0);
space->pooledArbiters = cpArrayNew(0);
space->contactBuffersHead = NULL;
space->cachedArbiters = cpHashSetNew(0, (cpHashSetEqlFunc)arbiterSetEql);
space->constraints = cpArrayNew(0);
space->usesWildcards = cpFalse;
space->defaultHandler = cpCollisionHandlerDoNothing;
space->collisionHandlers = cpHashSetNew(0, (cpHashSetEqlFunc)handlerSetEql);
space->postStepCallbacks = cpArrayNew(0);
space->skipPostStep = cpFalse;
cpBody *staticBody = cpBodyInit(&space->_staticBody, 0.0f, 0.0f);
cpBodySetType(staticBody, CP_BODY_TYPE_STATIC);
cpSpaceSetStaticBody(space, staticBody);
return space;
}
cpSpace*
cpSpaceNew(void)
{
return cpSpaceInit(cpSpaceAlloc());
}
static void cpBodyActivateWrap(cpBody *body, void *unused){cpBodyActivate(body);}
void
cpSpaceDestroy(cpSpace *space)
{
cpSpaceEachBody(space, (cpSpaceBodyIteratorFunc)cpBodyActivateWrap, NULL);
cpSpatialIndexFree(space->staticShapes);
cpSpatialIndexFree(space->dynamicShapes);
cpArrayFree(space->dynamicBodies);
cpArrayFree(space->staticBodies);
cpArrayFree(space->sleepingComponents);
cpArrayFree(space->rousedBodies);
cpArrayFree(space->constraints);
cpHashSetFree(space->cachedArbiters);
cpArrayFree(space->arbiters);
cpArrayFree(space->pooledArbiters);
if(space->allocatedBuffers){
cpArrayFreeEach(space->allocatedBuffers, cpfree);
cpArrayFree(space->allocatedBuffers);
}
if(space->postStepCallbacks){
cpArrayFreeEach(space->postStepCallbacks, cpfree);
cpArrayFree(space->postStepCallbacks);
}
if(space->collisionHandlers) cpHashSetEach(space->collisionHandlers, FreeWrap, NULL);
cpHashSetFree(space->collisionHandlers);
}
void
cpSpaceFree(cpSpace *space)
{
if(space){
cpSpaceDestroy(space);
cpfree(space);
}
}
//MARK: Basic properties:
int
cpSpaceGetIterations(const cpSpace *space)
{
return space->iterations;
}
void
cpSpaceSetIterations(cpSpace *space, int iterations)
{
cpAssertHard(iterations > 0, "Iterations must be positive and non-zero.");
space->iterations = iterations;
}
cpVect
cpSpaceGetGravity(const cpSpace *space)
{
return space->gravity;
}
void
cpSpaceSetGravity(cpSpace *space, cpVect gravity)
{
space->gravity = gravity;
}
cpFloat
cpSpaceGetDamping(const cpSpace *space)
{
return space->damping;
}
void
cpSpaceSetDamping(cpSpace *space, cpFloat damping)
{
cpAssertHard(damping >= 0.0, "Damping must be positive.");
space->damping = damping;
}
cpFloat
cpSpaceGetIdleSpeedThreshold(const cpSpace *space)
{
return space->idleSpeedThreshold;
}
void
cpSpaceSetIdleSpeedThreshold(cpSpace *space, cpFloat idleSpeedThreshold)
{
space->idleSpeedThreshold = idleSpeedThreshold;
}
cpFloat
cpSpaceGetSleepTimeThreshold(const cpSpace *space)
{
return space->sleepTimeThreshold;
}
void
cpSpaceSetSleepTimeThreshold(cpSpace *space, cpFloat sleepTimeThreshold)
{
space->sleepTimeThreshold = sleepTimeThreshold;
}
cpFloat
cpSpaceGetCollisionSlop(const cpSpace *space)
{
return space->collisionSlop;
}
void
cpSpaceSetCollisionSlop(cpSpace *space, cpFloat collisionSlop)
{
space->collisionSlop = collisionSlop;
}
cpFloat
cpSpaceGetCollisionBias(const cpSpace *space)
{
return space->collisionBias;
}
void
cpSpaceSetCollisionBias(cpSpace *space, cpFloat collisionBias)
{
space->collisionBias = collisionBias;
}
cpTimestamp
cpSpaceGetCollisionPersistence(const cpSpace *space)
{
return space->collisionPersistence;
}
void
cpSpaceSetCollisionPersistence(cpSpace *space, cpTimestamp collisionPersistence)
{
space->collisionPersistence = collisionPersistence;
}
cpDataPointer
cpSpaceGetUserData(const cpSpace *space)
{
return space->userData;
}
void
cpSpaceSetUserData(cpSpace *space, cpDataPointer userData)
{
space->userData = userData;
}
cpBody *
cpSpaceGetStaticBody(const cpSpace *space)
{
return space->staticBody;
}
cpFloat
cpSpaceGetCurrentTimeStep(const cpSpace *space)
{
return space->curr_dt;
}
void
cpSpaceSetStaticBody(cpSpace *space, cpBody *body)
{
if(space->staticBody != NULL){
cpAssertHard(space->staticBody->shapeList == NULL, "Internal Error: Changing the designated static body while the old one still had shapes attached.");
space->staticBody->space = NULL;
}
space->staticBody = body;
body->space = space;
}
cpBool
cpSpaceIsLocked(cpSpace *space)
{
return (space->locked > 0);
}
//MARK: Collision Handler Function Management
static void
cpSpaceUseWildcardDefaultHandler(cpSpace *space)
{
// Spaces default to using the slightly faster "do nothing" default handler until wildcards are potentially needed.
if(!space->usesWildcards){
space->usesWildcards = cpTrue;
space->defaultHandler = cpCollisionHandlerDefault;
}
}
cpCollisionHandler *cpSpaceAddDefaultCollisionHandler(cpSpace *space)
{
cpSpaceUseWildcardDefaultHandler(space);
return &space->defaultHandler;
}
cpCollisionHandler *cpSpaceAddCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b)
{
cpHashValue hash = CP_HASH_PAIR(a, b);
// TODO should use space->defaultHandler values instead?
cpCollisionHandler temp = {a, b, DefaultBegin, DefaultPreSolve, DefaultPostSolve, DefaultSeparate, NULL};
cpHashSet *handlers = space->collisionHandlers;
cpCollisionHandler *handler = (cpCollisionHandler*)cpHashSetFind(handlers, hash, &temp);
return (handler ? handler : (cpCollisionHandler*)cpHashSetInsert(handlers, hash, &temp, (cpHashSetTransFunc)handlerSetTrans, NULL));
}
cpCollisionHandler *
cpSpaceAddWildcardHandler(cpSpace *space, cpCollisionType type)
{
cpSpaceUseWildcardDefaultHandler(space);
cpHashValue hash = CP_HASH_PAIR(type, CP_WILDCARD_COLLISION_TYPE);
cpCollisionHandler temp = {type, CP_WILDCARD_COLLISION_TYPE, AlwaysCollide, AlwaysCollide, DoNothing, DoNothing, NULL};
cpHashSet *handlers = space->collisionHandlers;
cpCollisionHandler *handler = (cpCollisionHandler*)cpHashSetFind(handlers, hash, &temp);
return (handler ? handler : (cpCollisionHandler*)cpHashSetInsert(handlers, hash, &temp, (cpHashSetTransFunc)handlerSetTrans, NULL));
}
//MARK: Body, Shape, and Joint Management
cpShape *
cpSpaceAddShape(cpSpace *space, cpShape *shape)
{
cpBody *body = shape->body;
cpAssertHard(shape->space != space, "You have already added this shape to this space. You must not add it a second time.");
cpAssertHard(!shape->space, "You have already added this shape to another space. You cannot add it to a second.");
// cpAssertHard(body->space == space, "The shape's body must be added to the space before the shape.");
cpAssertSpaceUnlocked(space);
cpBool isStatic = (cpBodyGetType(body) == CP_BODY_TYPE_STATIC);
if(!isStatic) cpBodyActivate(body);
cpBodyAddShape(body, shape);
shape->hashid = space->shapeIDCounter++;
cpShapeUpdate(shape, body->transform);
cpSpatialIndexInsert(isStatic ? space->staticShapes : space->dynamicShapes, shape, shape->hashid);
shape->space = space;
return shape;
}
cpBody *
cpSpaceAddBody(cpSpace *space, cpBody *body)
{
cpAssertHard(body->space != space, "You have already added this body to this space. You must not add it a second time.");
cpAssertHard(!body->space, "You have already added this body to another space. You cannot add it to a second.");
cpAssertSpaceUnlocked(space);
cpArrayPush(cpSpaceArrayForBodyType(space, cpBodyGetType(body)), body);
body->space = space;
return body;
}
cpConstraint *
cpSpaceAddConstraint(cpSpace *space, cpConstraint *constraint)
{
cpAssertHard(constraint->space != space, "You have already added this constraint to this space. You must not add it a second time.");
cpAssertHard(!constraint->space, "You have already added this constraint to another space. You cannot add it to a second.");
cpAssertSpaceUnlocked(space);
cpBody *a = constraint->a, *b = constraint->b;
cpAssertHard(a != NULL && b != NULL, "Constraint is attached to a NULL body.");
// cpAssertHard(a->space == space && b->space == space, "The constraint's bodies must be added to the space before the constraint.");
cpBodyActivate(a);
cpBodyActivate(b);
cpArrayPush(space->constraints, constraint);
// Push onto the heads of the bodies' constraint lists
constraint->next_a = a->constraintList; a->constraintList = constraint;
constraint->next_b = b->constraintList; b->constraintList = constraint;
constraint->space = space;
return constraint;
}
struct arbiterFilterContext {
cpSpace *space;
cpBody *body;
cpShape *shape;
};
static cpBool
cachedArbitersFilter(cpArbiter *arb, struct arbiterFilterContext *context)
{
cpShape *shape = context->shape;
cpBody *body = context->body;
// Match on the filter shape, or if it's NULL the filter body
if(
(body == arb->body_a && (shape == arb->a || shape == NULL)) ||
(body == arb->body_b && (shape == arb->b || shape == NULL))
){
// Call separate when removing shapes.
if(shape && arb->state != CP_ARBITER_STATE_CACHED){
// Invalidate the arbiter since one of the shapes was removed.
arb->state = CP_ARBITER_STATE_INVALIDATED;
cpCollisionHandler *handler = arb->handler;
handler->separateFunc(arb, context->space, handler->userData);
}
cpArbiterUnthread(arb);
cpArrayDeleteObj(context->space->arbiters, arb);
cpArrayPush(context->space->pooledArbiters, arb);
return cpFalse;
}
return cpTrue;
}
void
cpSpaceFilterArbiters(cpSpace *space, cpBody *body, cpShape *filter)
{
cpSpaceLock(space); {
struct arbiterFilterContext context = {space, body, filter};
cpHashSetFilter(space->cachedArbiters, (cpHashSetFilterFunc)cachedArbitersFilter, &context);
} cpSpaceUnlock(space, cpTrue);
}
void
cpSpaceRemoveShape(cpSpace *space, cpShape *shape)
{
cpBody *body = shape->body;
cpAssertHard(cpSpaceContainsShape(space, shape), "Cannot remove a shape that was not added to the space. (Removed twice maybe?)");
cpAssertSpaceUnlocked(space);
cpBool isStatic = (cpBodyGetType(body) == CP_BODY_TYPE_STATIC);
if(isStatic){
cpBodyActivateStatic(body, shape);
} else {
cpBodyActivate(body);
}
cpBodyRemoveShape(body, shape);
cpSpaceFilterArbiters(space, body, shape);
cpSpatialIndexRemove(isStatic ? space->staticShapes : space->dynamicShapes, shape, shape->hashid);
shape->space = NULL;
shape->hashid = 0;
}
void
cpSpaceRemoveBody(cpSpace *space, cpBody *body)
{
cpAssertHard(body != cpSpaceGetStaticBody(space), "Cannot remove the designated static body for the space.");
cpAssertHard(cpSpaceContainsBody(space, body), "Cannot remove a body that was not added to the space. (Removed twice maybe?)");
// cpAssertHard(body->shapeList == NULL, "Cannot remove a body from the space before removing the bodies attached to it.");
// cpAssertHard(body->constraintList == NULL, "Cannot remove a body from the space before removing the constraints attached to it.");
cpAssertSpaceUnlocked(space);
cpBodyActivate(body);
// cpSpaceFilterArbiters(space, body, NULL);
cpArrayDeleteObj(cpSpaceArrayForBodyType(space, cpBodyGetType(body)), body);
body->space = NULL;
}
void
cpSpaceRemoveConstraint(cpSpace *space, cpConstraint *constraint)
{
cpAssertHard(cpSpaceContainsConstraint(space, constraint), "Cannot remove a constraint that was not added to the space. (Removed twice maybe?)");
cpAssertSpaceUnlocked(space);
cpBodyActivate(constraint->a);
cpBodyActivate(constraint->b);
cpArrayDeleteObj(space->constraints, constraint);
cpBodyRemoveConstraint(constraint->a, constraint);
cpBodyRemoveConstraint(constraint->b, constraint);
constraint->space = NULL;
}
cpBool cpSpaceContainsShape(cpSpace *space, cpShape *shape)
{
return (shape->space == space);
}
cpBool cpSpaceContainsBody(cpSpace *space, cpBody *body)
{
return (body->space == space);
}
cpBool cpSpaceContainsConstraint(cpSpace *space, cpConstraint *constraint)
{
return (constraint->space == space);
}
//MARK: Iteration
void
cpSpaceEachBody(cpSpace *space, cpSpaceBodyIteratorFunc func, void *data)
{
cpSpaceLock(space); {
cpArray *bodies = space->dynamicBodies;
for(int i=0; i<bodies->num; i++){
func((cpBody *)bodies->arr[i], data);
}
cpArray *otherBodies = space->staticBodies;
for(int i=0; i<otherBodies->num; i++){
func((cpBody *)otherBodies->arr[i], data);
}
cpArray *components = space->sleepingComponents;
for(int i=0; i<components->num; i++){
cpBody *root = (cpBody *)components->arr[i];
cpBody *body = root;
while(body){
cpBody *next = body->sleeping.next;
func(body, data);
body = next;
}
}
} cpSpaceUnlock(space, cpTrue);
}
typedef struct spaceShapeContext {
cpSpaceShapeIteratorFunc func;
void *data;
} spaceShapeContext;
static void
spaceEachShapeIterator(cpShape *shape, spaceShapeContext *context)
{
context->func(shape, context->data);
}
void
cpSpaceEachShape(cpSpace *space, cpSpaceShapeIteratorFunc func, void *data)
{
cpSpaceLock(space); {
spaceShapeContext context = {func, data};
cpSpatialIndexEach(space->dynamicShapes, (cpSpatialIndexIteratorFunc)spaceEachShapeIterator, &context);
cpSpatialIndexEach(space->staticShapes, (cpSpatialIndexIteratorFunc)spaceEachShapeIterator, &context);
} cpSpaceUnlock(space, cpTrue);
}
void
cpSpaceEachConstraint(cpSpace *space, cpSpaceConstraintIteratorFunc func, void *data)
{
cpSpaceLock(space); {
cpArray *constraints = space->constraints;
for(int i=0; i<constraints->num; i++){
func((cpConstraint *)constraints->arr[i], data);
}
} cpSpaceUnlock(space, cpTrue);
}
//MARK: Spatial Index Management
void
cpSpaceReindexStatic(cpSpace *space)
{
cpAssertHard(!space->locked, "You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.");
cpSpatialIndexEach(space->staticShapes, (cpSpatialIndexIteratorFunc)&cpShapeUpdateFunc, NULL);
cpSpatialIndexReindex(space->staticShapes);
}
void
cpSpaceReindexShape(cpSpace *space, cpShape *shape)
{
cpAssertHard(!space->locked, "You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.");
cpShapeCacheBB(shape);
// attempt to rehash the shape in both hashes
cpSpatialIndexReindexObject(space->dynamicShapes, shape, shape->hashid);
cpSpatialIndexReindexObject(space->staticShapes, shape, shape->hashid);
}
void
cpSpaceReindexShapesForBody(cpSpace *space, cpBody *body)
{
CP_BODY_FOREACH_SHAPE(body, shape) cpSpaceReindexShape(space, shape);
}
static void
copyShapes(cpShape *shape, cpSpatialIndex *index)
{
cpSpatialIndexInsert(index, shape, shape->hashid);
}
void
cpSpaceUseSpatialHash(cpSpace *space, cpFloat dim, int count)
{
cpSpatialIndex *staticShapes = cpSpaceHashNew(dim, count, (cpSpatialIndexBBFunc)cpShapeGetBB, NULL);
cpSpatialIndex *dynamicShapes = cpSpaceHashNew(dim, count, (cpSpatialIndexBBFunc)cpShapeGetBB, staticShapes);
cpSpatialIndexEach(space->staticShapes, (cpSpatialIndexIteratorFunc)copyShapes, staticShapes);
cpSpatialIndexEach(space->dynamicShapes, (cpSpatialIndexIteratorFunc)copyShapes, dynamicShapes);
cpSpatialIndexFree(space->staticShapes);
cpSpatialIndexFree(space->dynamicShapes);
space->staticShapes = staticShapes;
space->dynamicShapes = dynamicShapes;
}