chipmunk 5.2.2 → 5.3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -19
- data/README +67 -60
- data/Rakefile +85 -50
- data/ext/chipmunk/extconf.rb +42 -35
- data/ext/chipmunk/rb_chipmunk.c +250 -125
- data/ext/chipmunk/rb_chipmunk.h +105 -96
- data/ext/chipmunk/rb_cpArbiter.c +253 -225
- data/ext/chipmunk/rb_cpBB.c +210 -174
- data/ext/chipmunk/rb_cpBody.c +504 -347
- data/ext/chipmunk/rb_cpConstraint.c +336 -346
- data/ext/chipmunk/rb_cpShape.c +433 -455
- data/ext/chipmunk/rb_cpSpace.c +711 -544
- data/ext/chipmunk/rb_cpVect.c +346 -321
- data/lib/chipmunk.rb +183 -30
- metadata +12 -66
- data/ext/chipmunk/chipmunk.c +0 -105
- data/ext/chipmunk/cpArbiter.c +0 -274
- data/ext/chipmunk/cpArray.c +0 -128
- data/ext/chipmunk/cpBB.c +0 -47
- data/ext/chipmunk/cpBody.c +0 -188
- data/ext/chipmunk/cpCollision.c +0 -391
- data/ext/chipmunk/cpConstraint.c +0 -54
- data/ext/chipmunk/cpDampedRotarySpring.c +0 -106
- data/ext/chipmunk/cpDampedSpring.c +0 -117
- data/ext/chipmunk/cpGearJoint.c +0 -114
- data/ext/chipmunk/cpGrooveJoint.c +0 -138
- data/ext/chipmunk/cpHashSet.c +0 -253
- data/ext/chipmunk/cpPinJoint.c +0 -117
- data/ext/chipmunk/cpPivotJoint.c +0 -114
- data/ext/chipmunk/cpPolyShape.c +0 -241
- data/ext/chipmunk/cpRatchetJoint.c +0 -128
- data/ext/chipmunk/cpRotaryLimitJoint.c +0 -122
- data/ext/chipmunk/cpShape.c +0 -400
- data/ext/chipmunk/cpSimpleMotor.c +0 -99
- data/ext/chipmunk/cpSlideJoint.c +0 -131
- data/ext/chipmunk/cpSpace.c +0 -899
- data/ext/chipmunk/cpSpaceHash.c +0 -541
- data/ext/chipmunk/cpVect.c +0 -71
- data/ext/chipmunk/include/chipmunk/chipmunk.h +0 -148
- data/ext/chipmunk/include/chipmunk/chipmunk_ffi.h +0 -42
- data/ext/chipmunk/include/chipmunk/chipmunk_types.h +0 -80
- data/ext/chipmunk/include/chipmunk/chipmunk_unsafe.h +0 -54
- data/ext/chipmunk/include/chipmunk/constraints/cpConstraint.h +0 -92
- data/ext/chipmunk/include/chipmunk/constraints/cpDampedRotarySpring.h +0 -46
- data/ext/chipmunk/include/chipmunk/constraints/cpDampedSpring.h +0 -53
- data/ext/chipmunk/include/chipmunk/constraints/cpGearJoint.h +0 -41
- data/ext/chipmunk/include/chipmunk/constraints/cpGrooveJoint.h +0 -44
- data/ext/chipmunk/include/chipmunk/constraints/cpPinJoint.h +0 -43
- data/ext/chipmunk/include/chipmunk/constraints/cpPivotJoint.h +0 -42
- data/ext/chipmunk/include/chipmunk/constraints/cpRatchetJoint.h +0 -40
- data/ext/chipmunk/include/chipmunk/constraints/cpRotaryLimitJoint.h +0 -39
- data/ext/chipmunk/include/chipmunk/constraints/cpSimpleMotor.h +0 -37
- data/ext/chipmunk/include/chipmunk/constraints/cpSlideJoint.h +0 -44
- data/ext/chipmunk/include/chipmunk/constraints/util.h +0 -116
- data/ext/chipmunk/include/chipmunk/cpArbiter.h +0 -136
- data/ext/chipmunk/include/chipmunk/cpArray.h +0 -46
- data/ext/chipmunk/include/chipmunk/cpBB.h +0 -74
- data/ext/chipmunk/include/chipmunk/cpBody.h +0 -160
- data/ext/chipmunk/include/chipmunk/cpCollision.h +0 -23
- data/ext/chipmunk/include/chipmunk/cpHashSet.h +0 -82
- data/ext/chipmunk/include/chipmunk/cpPolyShape.h +0 -103
- data/ext/chipmunk/include/chipmunk/cpShape.h +0 -174
- data/ext/chipmunk/include/chipmunk/cpSpace.h +0 -180
- data/ext/chipmunk/include/chipmunk/cpSpaceHash.h +0 -109
- data/ext/chipmunk/include/chipmunk/cpVect.h +0 -157
- data/ext/chipmunk/prime.h +0 -68
- data/lib/chipmunk/version.rb +0 -3
data/ext/chipmunk/cpSpaceHash.c
DELETED
@@ -1,541 +0,0 @@
|
|
1
|
-
/* Copyright (c) 2007 Scott Lembcke
|
2
|
-
*
|
3
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
* of this software and associated documentation files (the "Software"), to deal
|
5
|
-
* in the Software without restriction, including without limitation the rights
|
6
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
* copies of the Software, and to permit persons to whom the Software is
|
8
|
-
* furnished to do so, subject to the following conditions:
|
9
|
-
*
|
10
|
-
* The above copyright notice and this permission notice shall be included in
|
11
|
-
* all copies or substantial portions of the Software.
|
12
|
-
*
|
13
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
-
* SOFTWARE.
|
20
|
-
*/
|
21
|
-
|
22
|
-
#include <math.h>
|
23
|
-
#include <stdlib.h>
|
24
|
-
#include <stdio.h>
|
25
|
-
|
26
|
-
#include "chipmunk.h"
|
27
|
-
#include "prime.h"
|
28
|
-
|
29
|
-
static cpHandle*
|
30
|
-
cpHandleInit(cpHandle *hand, void *obj)
|
31
|
-
{
|
32
|
-
hand->obj = obj;
|
33
|
-
hand->retain = 0;
|
34
|
-
hand->stamp = 0;
|
35
|
-
|
36
|
-
return hand;
|
37
|
-
}
|
38
|
-
|
39
|
-
static inline void
|
40
|
-
cpHandleRetain(cpHandle *hand)
|
41
|
-
{
|
42
|
-
hand->retain++;
|
43
|
-
}
|
44
|
-
|
45
|
-
static inline void
|
46
|
-
cpHandleRelease(cpHandle *hand, cpArray *pooledHandles)
|
47
|
-
{
|
48
|
-
hand->retain--;
|
49
|
-
if(hand->retain == 0) cpArrayPush(pooledHandles, hand);
|
50
|
-
}
|
51
|
-
|
52
|
-
cpSpaceHash*
|
53
|
-
cpSpaceHashAlloc(void)
|
54
|
-
{
|
55
|
-
return (cpSpaceHash *)cpcalloc(1, sizeof(cpSpaceHash));
|
56
|
-
}
|
57
|
-
|
58
|
-
// Frees the old table, and allocates a new one.
|
59
|
-
static void
|
60
|
-
cpSpaceHashAllocTable(cpSpaceHash *hash, int numcells)
|
61
|
-
{
|
62
|
-
cpfree(hash->table);
|
63
|
-
|
64
|
-
hash->numcells = numcells;
|
65
|
-
hash->table = (cpSpaceHashBin **)cpcalloc(numcells, sizeof(cpSpaceHashBin *));
|
66
|
-
}
|
67
|
-
|
68
|
-
// Equality function for the handleset.
|
69
|
-
static int
|
70
|
-
handleSetEql(void *obj, void *elt)
|
71
|
-
{
|
72
|
-
cpHandle *hand = (cpHandle *)elt;
|
73
|
-
return (obj == hand->obj);
|
74
|
-
}
|
75
|
-
|
76
|
-
// Transformation function for the handleset.
|
77
|
-
static void *
|
78
|
-
handleSetTrans(void *obj, cpSpaceHash *hash)
|
79
|
-
{
|
80
|
-
if(hash->pooledHandles->num == 0){
|
81
|
-
// handle pool is exhausted, make more
|
82
|
-
int count = CP_BUFFER_BYTES/sizeof(cpHandle);
|
83
|
-
cpAssert(count, "Buffer size is too small.");
|
84
|
-
|
85
|
-
cpHandle *buffer = (cpHandle *)cpmalloc(CP_BUFFER_BYTES);
|
86
|
-
cpArrayPush(hash->allocatedBuffers, buffer);
|
87
|
-
|
88
|
-
for(int i=0; i<count; i++) cpArrayPush(hash->pooledHandles, buffer + i);
|
89
|
-
}
|
90
|
-
|
91
|
-
cpHandle *hand = cpHandleInit(cpArrayPop(hash->pooledHandles), obj);
|
92
|
-
cpHandleRetain(hand);
|
93
|
-
|
94
|
-
return hand;
|
95
|
-
}
|
96
|
-
|
97
|
-
cpSpaceHash*
|
98
|
-
cpSpaceHashInit(cpSpaceHash *hash, cpFloat celldim, int numcells, cpSpaceHashBBFunc bbfunc)
|
99
|
-
{
|
100
|
-
cpSpaceHashAllocTable(hash, next_prime(numcells));
|
101
|
-
hash->celldim = celldim;
|
102
|
-
hash->bbfunc = bbfunc;
|
103
|
-
|
104
|
-
hash->handleSet = cpHashSetNew(0, handleSetEql, (cpHashSetTransFunc)handleSetTrans);
|
105
|
-
hash->pooledHandles = cpArrayNew(0);
|
106
|
-
|
107
|
-
hash->pooledBins = NULL;
|
108
|
-
hash->allocatedBuffers = cpArrayNew(0);
|
109
|
-
|
110
|
-
hash->stamp = 1;
|
111
|
-
|
112
|
-
return hash;
|
113
|
-
}
|
114
|
-
|
115
|
-
cpSpaceHash*
|
116
|
-
cpSpaceHashNew(cpFloat celldim, int cells, cpSpaceHashBBFunc bbfunc)
|
117
|
-
{
|
118
|
-
return cpSpaceHashInit(cpSpaceHashAlloc(), celldim, cells, bbfunc);
|
119
|
-
}
|
120
|
-
|
121
|
-
static inline void
|
122
|
-
recycleBin(cpSpaceHash *hash, cpSpaceHashBin *bin)
|
123
|
-
{
|
124
|
-
bin->next = hash->pooledBins;
|
125
|
-
hash->pooledBins = bin;
|
126
|
-
}
|
127
|
-
|
128
|
-
static inline void
|
129
|
-
clearHashCell(cpSpaceHash *hash, int idx)
|
130
|
-
{
|
131
|
-
cpSpaceHashBin *bin = hash->table[idx];
|
132
|
-
while(bin){
|
133
|
-
cpSpaceHashBin *next = bin->next;
|
134
|
-
|
135
|
-
// Release the lock on the handle.
|
136
|
-
cpHandleRelease(bin->handle, hash->pooledHandles);
|
137
|
-
recycleBin(hash, bin);
|
138
|
-
|
139
|
-
bin = next;
|
140
|
-
}
|
141
|
-
|
142
|
-
hash->table[idx] = NULL;
|
143
|
-
}
|
144
|
-
|
145
|
-
// Clear all cells in the hashtable.
|
146
|
-
static void
|
147
|
-
clearHash(cpSpaceHash *hash)
|
148
|
-
{
|
149
|
-
for(int i=0; i<hash->numcells; i++)
|
150
|
-
clearHashCell(hash, i);
|
151
|
-
}
|
152
|
-
|
153
|
-
static void freeWrap(void *ptr, void *unused){cpfree(ptr);}
|
154
|
-
|
155
|
-
void
|
156
|
-
cpSpaceHashDestroy(cpSpaceHash *hash)
|
157
|
-
{
|
158
|
-
clearHash(hash);
|
159
|
-
|
160
|
-
cpHashSetFree(hash->handleSet);
|
161
|
-
|
162
|
-
cpArrayEach(hash->allocatedBuffers, freeWrap, NULL);
|
163
|
-
cpArrayFree(hash->allocatedBuffers);
|
164
|
-
cpArrayFree(hash->pooledHandles);
|
165
|
-
|
166
|
-
cpfree(hash->table);
|
167
|
-
}
|
168
|
-
|
169
|
-
void
|
170
|
-
cpSpaceHashFree(cpSpaceHash *hash)
|
171
|
-
{
|
172
|
-
if(hash){
|
173
|
-
cpSpaceHashDestroy(hash);
|
174
|
-
cpfree(hash);
|
175
|
-
}
|
176
|
-
}
|
177
|
-
|
178
|
-
void
|
179
|
-
cpSpaceHashResize(cpSpaceHash *hash, cpFloat celldim, int numcells)
|
180
|
-
{
|
181
|
-
// Clear the hash to release the old handle locks.
|
182
|
-
clearHash(hash);
|
183
|
-
|
184
|
-
hash->celldim = celldim;
|
185
|
-
cpSpaceHashAllocTable(hash, next_prime(numcells));
|
186
|
-
}
|
187
|
-
|
188
|
-
// Return true if the chain contains the handle.
|
189
|
-
static inline int
|
190
|
-
containsHandle(cpSpaceHashBin *bin, cpHandle *hand)
|
191
|
-
{
|
192
|
-
while(bin){
|
193
|
-
if(bin->handle == hand) return 1;
|
194
|
-
bin = bin->next;
|
195
|
-
}
|
196
|
-
|
197
|
-
return 0;
|
198
|
-
}
|
199
|
-
|
200
|
-
// Get a recycled or new bin.
|
201
|
-
static inline cpSpaceHashBin *
|
202
|
-
getEmptyBin(cpSpaceHash *hash)
|
203
|
-
{
|
204
|
-
cpSpaceHashBin *bin = hash->pooledBins;
|
205
|
-
|
206
|
-
if(bin){
|
207
|
-
hash->pooledBins = bin->next;
|
208
|
-
return bin;
|
209
|
-
} else {
|
210
|
-
// Pool is exhausted, make more
|
211
|
-
int count = CP_BUFFER_BYTES/sizeof(cpSpaceHashBin);
|
212
|
-
cpAssert(count, "Buffer size is too small.");
|
213
|
-
|
214
|
-
cpSpaceHashBin *buffer = (cpSpaceHashBin *)cpmalloc(CP_BUFFER_BYTES);
|
215
|
-
cpArrayPush(hash->allocatedBuffers, buffer);
|
216
|
-
|
217
|
-
// push all but the first one, return the first instead
|
218
|
-
for(int i=1; i<count; i++) recycleBin(hash, buffer + i);
|
219
|
-
return buffer;
|
220
|
-
}
|
221
|
-
}
|
222
|
-
|
223
|
-
// The hash function itself.
|
224
|
-
static inline cpHashValue
|
225
|
-
hash_func(cpHashValue x, cpHashValue y, cpHashValue n)
|
226
|
-
{
|
227
|
-
return (x*1640531513ul ^ y*2654435789ul) % n;
|
228
|
-
}
|
229
|
-
|
230
|
-
// Much faster than (int)floor(f)
|
231
|
-
// Profiling showed floor() to be a sizable performance hog
|
232
|
-
static inline int
|
233
|
-
floor_int(cpFloat f)
|
234
|
-
{
|
235
|
-
int i = (int)f;
|
236
|
-
return (f < 0.0f && f != i ? i - 1 : i);
|
237
|
-
}
|
238
|
-
|
239
|
-
static inline void
|
240
|
-
hashHandle(cpSpaceHash *hash, cpHandle *hand, cpBB bb)
|
241
|
-
{
|
242
|
-
// Find the dimensions in cell coordinates.
|
243
|
-
cpFloat dim = hash->celldim;
|
244
|
-
int l = floor_int(bb.l/dim); // Fix by ShiftZ
|
245
|
-
int r = floor_int(bb.r/dim);
|
246
|
-
int b = floor_int(bb.b/dim);
|
247
|
-
int t = floor_int(bb.t/dim);
|
248
|
-
|
249
|
-
int n = hash->numcells;
|
250
|
-
for(int i=l; i<=r; i++){
|
251
|
-
for(int j=b; j<=t; j++){
|
252
|
-
int idx = hash_func(i,j,n);
|
253
|
-
cpSpaceHashBin *bin = hash->table[idx];
|
254
|
-
|
255
|
-
// Don't add an object twice to the same cell.
|
256
|
-
if(containsHandle(bin, hand)) continue;
|
257
|
-
|
258
|
-
cpHandleRetain(hand);
|
259
|
-
// Insert a new bin for the handle in this cell.
|
260
|
-
cpSpaceHashBin *newBin = getEmptyBin(hash);
|
261
|
-
newBin->handle = hand;
|
262
|
-
newBin->next = bin;
|
263
|
-
hash->table[idx] = newBin;
|
264
|
-
}
|
265
|
-
}
|
266
|
-
}
|
267
|
-
|
268
|
-
void
|
269
|
-
cpSpaceHashInsert(cpSpaceHash *hash, void *obj, cpHashValue hashid, cpBB bb)
|
270
|
-
{
|
271
|
-
cpHandle *hand = (cpHandle *)cpHashSetInsert(hash->handleSet, hashid, obj, hash);
|
272
|
-
hashHandle(hash, hand, bb);
|
273
|
-
}
|
274
|
-
|
275
|
-
void
|
276
|
-
cpSpaceHashRehashObject(cpSpaceHash *hash, void *obj, cpHashValue hashid)
|
277
|
-
{
|
278
|
-
cpHandle *hand = (cpHandle *)cpHashSetFind(hash->handleSet, hashid, obj);
|
279
|
-
hashHandle(hash, hand, hash->bbfunc(obj));
|
280
|
-
}
|
281
|
-
|
282
|
-
// Hashset iterator function for rehashing the spatial hash. (hash hash hash hash?)
|
283
|
-
static void
|
284
|
-
handleRehashHelper(void *elt, void *data)
|
285
|
-
{
|
286
|
-
cpHandle *hand = (cpHandle *)elt;
|
287
|
-
cpSpaceHash *hash = (cpSpaceHash *)data;
|
288
|
-
|
289
|
-
hashHandle(hash, hand, hash->bbfunc(hand->obj));
|
290
|
-
}
|
291
|
-
|
292
|
-
void
|
293
|
-
cpSpaceHashRehash(cpSpaceHash *hash)
|
294
|
-
{
|
295
|
-
clearHash(hash);
|
296
|
-
|
297
|
-
// Rehash all of the handles.
|
298
|
-
cpHashSetEach(hash->handleSet, &handleRehashHelper, hash);
|
299
|
-
}
|
300
|
-
|
301
|
-
void
|
302
|
-
cpSpaceHashRemove(cpSpaceHash *hash, void *obj, cpHashValue hashid)
|
303
|
-
{
|
304
|
-
cpHandle *hand = (cpHandle *)cpHashSetRemove(hash->handleSet, hashid, obj);
|
305
|
-
|
306
|
-
if(hand){
|
307
|
-
hand->obj = NULL;
|
308
|
-
cpHandleRelease(hand, hash->pooledHandles);
|
309
|
-
}
|
310
|
-
}
|
311
|
-
|
312
|
-
// Used by the cpSpaceHashEach() iterator.
|
313
|
-
typedef struct eachPair {
|
314
|
-
cpSpaceHashIterator func;
|
315
|
-
void *data;
|
316
|
-
} eachPair;
|
317
|
-
|
318
|
-
// Calls the user iterator function. (Gross I know.)
|
319
|
-
static void
|
320
|
-
eachHelper(void *elt, void *data)
|
321
|
-
{
|
322
|
-
cpHandle *hand = (cpHandle *)elt;
|
323
|
-
eachPair *pair = (eachPair *)data;
|
324
|
-
|
325
|
-
pair->func(hand->obj, pair->data);
|
326
|
-
}
|
327
|
-
|
328
|
-
// Iterate over the objects in the spatial hash.
|
329
|
-
void
|
330
|
-
cpSpaceHashEach(cpSpaceHash *hash, cpSpaceHashIterator func, void *data)
|
331
|
-
{
|
332
|
-
// Bundle the callback up to send to the hashset iterator.
|
333
|
-
eachPair pair = {func, data};
|
334
|
-
|
335
|
-
cpHashSetEach(hash->handleSet, &eachHelper, &pair);
|
336
|
-
}
|
337
|
-
|
338
|
-
// Calls the callback function for the objects in a given chain.
|
339
|
-
static inline void
|
340
|
-
query(cpSpaceHash *hash, cpSpaceHashBin *bin, void *obj, cpSpaceHashQueryFunc func, void *data)
|
341
|
-
{
|
342
|
-
for(; bin; bin = bin->next){
|
343
|
-
cpHandle *hand = bin->handle;
|
344
|
-
void *other = hand->obj;
|
345
|
-
|
346
|
-
// Skip over certain conditions
|
347
|
-
if(
|
348
|
-
// Have we already tried this pair in this query?
|
349
|
-
hand->stamp == hash->stamp
|
350
|
-
// Is obj the same as other?
|
351
|
-
|| obj == other
|
352
|
-
// Has other been removed since the last rehash?
|
353
|
-
|| !other
|
354
|
-
) continue;
|
355
|
-
|
356
|
-
func(obj, other, data);
|
357
|
-
|
358
|
-
// Stamp that the handle was checked already against this object.
|
359
|
-
hand->stamp = hash->stamp;
|
360
|
-
}
|
361
|
-
}
|
362
|
-
|
363
|
-
void
|
364
|
-
cpSpaceHashPointQuery(cpSpaceHash *hash, cpVect point, cpSpaceHashQueryFunc func, void *data)
|
365
|
-
{
|
366
|
-
cpFloat dim = hash->celldim;
|
367
|
-
int idx = hash_func(floor_int(point.x/dim), floor_int(point.y/dim), hash->numcells); // Fix by ShiftZ
|
368
|
-
|
369
|
-
query(hash, hash->table[idx], &point, func, data);
|
370
|
-
|
371
|
-
// Increment the stamp.
|
372
|
-
// Only one cell is checked, but query() requires it anyway.
|
373
|
-
hash->stamp++;
|
374
|
-
}
|
375
|
-
|
376
|
-
void
|
377
|
-
cpSpaceHashQuery(cpSpaceHash *hash, void *obj, cpBB bb, cpSpaceHashQueryFunc func, void *data)
|
378
|
-
{
|
379
|
-
// Get the dimensions in cell coordinates.
|
380
|
-
cpFloat dim = hash->celldim;
|
381
|
-
int l = floor_int(bb.l/dim); // Fix by ShiftZ
|
382
|
-
int r = floor_int(bb.r/dim);
|
383
|
-
int b = floor_int(bb.b/dim);
|
384
|
-
int t = floor_int(bb.t/dim);
|
385
|
-
|
386
|
-
int n = hash->numcells;
|
387
|
-
|
388
|
-
// Iterate over the cells and query them.
|
389
|
-
for(int i=l; i<=r; i++){
|
390
|
-
for(int j=b; j<=t; j++){
|
391
|
-
int idx = hash_func(i,j,n);
|
392
|
-
query(hash, hash->table[idx], obj, func, data);
|
393
|
-
}
|
394
|
-
}
|
395
|
-
|
396
|
-
// Increment the stamp.
|
397
|
-
hash->stamp++;
|
398
|
-
}
|
399
|
-
|
400
|
-
// Similar to struct eachPair above.
|
401
|
-
typedef struct queryRehashPair {
|
402
|
-
cpSpaceHash *hash;
|
403
|
-
cpSpaceHashQueryFunc func;
|
404
|
-
void *data;
|
405
|
-
} queryRehashPair;
|
406
|
-
|
407
|
-
// Hashset iterator func used with cpSpaceHashQueryRehash().
|
408
|
-
static void
|
409
|
-
handleQueryRehashHelper(void *elt, void *data)
|
410
|
-
{
|
411
|
-
cpHandle *hand = (cpHandle *)elt;
|
412
|
-
|
413
|
-
// Unpack the user callback data.
|
414
|
-
queryRehashPair *pair = (queryRehashPair *)data;
|
415
|
-
cpSpaceHash *hash = pair->hash;
|
416
|
-
cpSpaceHashQueryFunc func = pair->func;
|
417
|
-
|
418
|
-
cpFloat dim = hash->celldim;
|
419
|
-
int n = hash->numcells;
|
420
|
-
|
421
|
-
void *obj = hand->obj;
|
422
|
-
cpBB bb = hash->bbfunc(obj);
|
423
|
-
|
424
|
-
int l = floor_int(bb.l/dim);
|
425
|
-
int r = floor_int(bb.r/dim);
|
426
|
-
int b = floor_int(bb.b/dim);
|
427
|
-
int t = floor_int(bb.t/dim);
|
428
|
-
|
429
|
-
for(int i=l; i<=r; i++){
|
430
|
-
for(int j=b; j<=t; j++){
|
431
|
-
// // exit the loops if the object has been deleted in func().
|
432
|
-
// if(!hand->obj) goto break_out;
|
433
|
-
|
434
|
-
int idx = hash_func(i,j,n);
|
435
|
-
cpSpaceHashBin *bin = hash->table[idx];
|
436
|
-
|
437
|
-
if(containsHandle(bin, hand)) continue;
|
438
|
-
|
439
|
-
cpHandleRetain(hand); // this MUST be done first in case the object is removed in func()
|
440
|
-
query(hash, bin, obj, func, pair->data);
|
441
|
-
|
442
|
-
cpSpaceHashBin *newBin = getEmptyBin(hash);
|
443
|
-
newBin->handle = hand;
|
444
|
-
newBin->next = bin;
|
445
|
-
hash->table[idx] = newBin;
|
446
|
-
}
|
447
|
-
}
|
448
|
-
|
449
|
-
// break_out:
|
450
|
-
// Increment the stamp for each object we hash.
|
451
|
-
hash->stamp++;
|
452
|
-
}
|
453
|
-
|
454
|
-
void
|
455
|
-
cpSpaceHashQueryRehash(cpSpaceHash *hash, cpSpaceHashQueryFunc func, void *data)
|
456
|
-
{
|
457
|
-
clearHash(hash);
|
458
|
-
|
459
|
-
queryRehashPair pair = {hash, func, data};
|
460
|
-
cpHashSetEach(hash->handleSet, &handleQueryRehashHelper, &pair);
|
461
|
-
}
|
462
|
-
|
463
|
-
static inline cpFloat
|
464
|
-
segmentQuery(cpSpaceHash *hash, cpSpaceHashBin *bin, void *obj, cpSpaceHashSegmentQueryFunc func, void *data)
|
465
|
-
{
|
466
|
-
cpFloat t = 1.0f;
|
467
|
-
|
468
|
-
for(; bin; bin = bin->next){
|
469
|
-
cpHandle *hand = bin->handle;
|
470
|
-
void *other = hand->obj;
|
471
|
-
|
472
|
-
// Skip over certain conditions
|
473
|
-
if(
|
474
|
-
// Have we already tried this pair in this query?
|
475
|
-
hand->stamp == hash->stamp
|
476
|
-
// Has other been removed since the last rehash?
|
477
|
-
|| !other
|
478
|
-
) continue;
|
479
|
-
|
480
|
-
// Stamp that the handle was checked already against this object.
|
481
|
-
hand->stamp = hash->stamp;
|
482
|
-
|
483
|
-
t = cpfmin(t, func(obj, other, data));
|
484
|
-
}
|
485
|
-
|
486
|
-
return t;
|
487
|
-
}
|
488
|
-
|
489
|
-
// modified from http://playtechs.blogspot.com/2007/03/raytracing-on-grid.html
|
490
|
-
void cpSpaceHashSegmentQuery(cpSpaceHash *hash, void *obj, cpVect a, cpVect b, cpFloat t_exit, cpSpaceHashSegmentQueryFunc func, void *data)
|
491
|
-
{
|
492
|
-
a = cpvmult(a, 1.0f/hash->celldim);
|
493
|
-
b = cpvmult(b, 1.0f/hash->celldim);
|
494
|
-
|
495
|
-
cpFloat dt_dx = 1.0f/cpfabs(b.x - a.x), dt_dy = 1.0f/cpfabs(b.y - a.y);
|
496
|
-
|
497
|
-
int cell_x = floor_int(a.x), cell_y = floor_int(a.y);
|
498
|
-
|
499
|
-
cpFloat t = 0;
|
500
|
-
|
501
|
-
int x_inc, y_inc;
|
502
|
-
cpFloat temp_v, temp_h;
|
503
|
-
|
504
|
-
if (b.x > a.x){
|
505
|
-
x_inc = 1;
|
506
|
-
temp_h = (cpffloor(a.x + 1.0f) - a.x);
|
507
|
-
} else {
|
508
|
-
x_inc = -1;
|
509
|
-
temp_h = (a.x - cpffloor(a.x));
|
510
|
-
}
|
511
|
-
|
512
|
-
if (b.y > a.y){
|
513
|
-
y_inc = 1;
|
514
|
-
temp_v = (cpffloor(a.y + 1.0f) - a.y);
|
515
|
-
} else {
|
516
|
-
y_inc = -1;
|
517
|
-
temp_v = (a.y - cpffloor(a.y));
|
518
|
-
}
|
519
|
-
|
520
|
-
// fix NANs in horizontal directions
|
521
|
-
cpFloat next_h = (temp_h ? temp_h*dt_dx : dt_dx);
|
522
|
-
cpFloat next_v = (temp_v ? temp_v*dt_dy : dt_dy);
|
523
|
-
|
524
|
-
int n = hash->numcells;
|
525
|
-
while(t < t_exit){
|
526
|
-
int idx = hash_func(cell_x, cell_y, n);
|
527
|
-
t_exit = cpfmin(t_exit, segmentQuery(hash, hash->table[idx], obj, func, data));
|
528
|
-
|
529
|
-
if (next_v < next_h){
|
530
|
-
cell_y += y_inc;
|
531
|
-
t = next_v;
|
532
|
-
next_v += dt_dy;
|
533
|
-
} else {
|
534
|
-
cell_x += x_inc;
|
535
|
-
t = next_h;
|
536
|
-
next_h += dt_dx;
|
537
|
-
}
|
538
|
-
}
|
539
|
-
|
540
|
-
hash->stamp++;
|
541
|
-
}
|