]> gitweb.factorcode.org Git - factor.git/blob - extra/chipmunk/ffi/ffi.factor
0142b57a7727a87190cedf48b8da46fa4f699d44
[factor.git] / extra / chipmunk / ffi / ffi.factor
1 ! Copyright (C) 2010 Erik Charlebois
2 ! See http:// factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.syntax classes.struct combinators
4 combinators.short-circuit kernel math math.order sequences
5 typed specialized-arrays locals system alien.libraries ;
6 SPECIALIZED-ARRAY: void*
7 IN: chipmunk.ffi
8
9 <<
10 "chipmunk" {
11     { [ os windows? ] [ "chipmunk.dll" ] }
12     { [ os macosx? ] [ "libchipmunk.dylib"  ] }
13     { [ os unix?  ] [ "libchipmunk.so" ] }
14 } cond "cdecl" add-library
15
16 "chipmunk" deploy-library
17 >>
18 LIBRARY: chipmunk
19
20 ! chipmunk_types.h
21 TYPEDEF: double cpFloat
22 STRUCT: cpVect
23     { x cpFloat }
24     { y cpFloat } ;
25 SPECIALIZED-ARRAY: cpVect
26
27 TYPEDEF: uint cpHashValue
28 TYPEDEF: void* cpDataPointer
29 TYPEDEF: uint cpCollisionType
30 TYPEDEF: uint cpLayers
31 TYPEDEF: uint cpGroup
32
33 CONSTANT: CP_NO_GROUP 0
34 CONSTANT: CP_ALL_LAYERS HEX: ffffffff
35
36 ! cpVect.h
37 TYPED: cpv ( x y -- v: cpVect )
38     cpVect <struct-boa> ; inline
39
40 TYPED: cpvzero ( -- v: cpVect )
41     0.0 0.0 cpv ; inline
42
43 FUNCTION: cpFloat cpvlength ( cpVect v ) ;
44 FUNCTION: cpVect cpvslerp ( cpVect v1, cpVect v2, cpFloat t ) ;
45 FUNCTION: cpVect cpvslerpconst ( cpVect v1, cpVect v2, cpFloat a ) ;
46 FUNCTION: cpVect cpvforangle ( cpFloat a ) ;
47 FUNCTION: cpFloat cpvtoangle ( cpVect v ) ;
48 FUNCTION: c-string cpvstr ( cpVect v ) ;
49
50 TYPED: cpvadd ( v1: cpVect v2: cpVect -- v3: cpVect )
51     [ [ x>> ] bi@ + ]
52     [ [ y>> ] bi@ + ] 2bi cpv ; inline
53
54 TYPED: cpvneg ( v1: cpVect -- v2: cpVect )
55     [ x>> ] [ y>> ] bi [ neg ] bi@ cpv ; inline
56
57 TYPED: cpvsub ( v1: cpVect v2: cpVect -- v3: cpVect )
58     [ [ x>> ] bi@ - ]
59     [ [ y>> ] bi@ - ] 2bi cpv ; inline
60
61 TYPED: cpvmult ( v1: cpVect s -- v2: cpVect )
62     [ swap x>> * ]
63     [ swap y>> * ] 2bi cpv ; inline
64
65 TYPED: cpvdot ( v1: cpVect v2: cpVect -- s )
66     [ [ x>> ] bi@ * ]
67     [ [ y>> ] bi@ * ] 2bi + ; inline
68
69 TYPED: cpvcross ( v1: cpVect v2: cpVect -- s )
70     [ [ x>> ] [ y>> ] bi* * ]
71     [ [ y>> ] [ x>> ] bi* * ] 2bi - ; inline
72
73 TYPED: cpvperp ( v1: cpVect -- v2: cpVect )
74     [ y>> neg ] [ x>> ] bi cpv ; inline
75
76 TYPED: cpvrperp ( v1: cpVect -- v2: cpVect )
77     [ y>> ] [ x>> neg ] bi cpv ; inline
78
79 TYPED: cpvproject ( v1: cpVect v2: cpVect -- v3: cpVect )
80     [ nip ]
81     [ cpvdot ]
82     [ nip dup cpvdot ]
83     2tri / cpvmult ; inline
84
85 TYPED: cpvrotate ( v1: cpVect v2: cpVect -- v3: cpVect )
86     [
87         [ [ x>> ] bi@ * ]
88         [ [ y>> ] bi@ * ] 2bi -
89     ]
90     [
91         [ [ x>> ] [ y>> ] bi* * ]
92         [ [ y>> ] [ x>> ] bi* * ] 2bi +
93     ] 2bi cpv ; inline
94
95 TYPED: cpvunrotate ( v1: cpVect v2: cpVect -- v3: cpVect )
96     [
97         [ [ x>> ] bi@ * ]
98         [ [ y>> ] bi@ * ] 2bi +
99     ]
100     [
101         [ [ y>> ] [ x>> ] bi* * ]
102         [ [ x>> ] [ y>> ] bi* * ] 2bi -
103     ] 2bi cpv ; inline
104
105 TYPED: cpvlengthsq ( v: cpVect -- s )
106     dup cpvdot ; inline
107
108 TYPED: cpvlerp ( v1: cpVect v2: cpVect s -- v3: cpVect )
109     [ nip 1.0 swap - cpvmult ]
110     [ cpvmult nip ] 3bi cpvadd ; inline
111
112 TYPED: cpvnormalize ( v1: cpVect -- v2: cpVect )
113     dup cpvlength 1.0 swap / cpvmult ; inline
114
115 TYPED: cpvnormalize_safe ( v1: cpVect -- v2: cpVect )
116     dup [ x>> 0.0 = ] [ y>> 0.0 = ] bi and
117     [ drop cpvzero ]
118     [ cpvnormalize ] if ; inline
119
120 TYPED: cpvclamp ( v1: cpVect len -- v2: cpVect )
121     2dup
122     [ dup cpvdot ]
123     [ sq ] 2bi* >
124     [ [ cpvnormalize ] dip cpvmult ]
125     [ drop ] if ; inline
126
127 TYPED: cpvlerpconst ( v1: cpVect v2: cpVect d -- v3: cpVect )
128     [ 2drop ]
129     [ [ swap cpvsub ] dip cpvclamp ] 3bi cpvadd ; inline
130
131 TYPED: cpvdist ( v1: cpVect v2: cpVect -- dist )
132     cpvsub cpvlength ; inline
133
134 TYPED: cpvdistsq ( v1: cpVect v2: cpVect -- distsq )
135     cpvsub cpvlengthsq ; inline
136
137 TYPED: cpvnear ( v1: cpVect v2: cpVect dist -- ? )
138     [ cpvdistsq ] dip sq < ; inline
139
140 ! cpBB.h
141 STRUCT: cpBB
142     { l cpFloat }
143     { b cpFloat }
144     { r cpFloat }
145     { t cpFloat } ;
146
147 TYPED: cpBBNew ( l b r t -- cpbb: cpBB )
148     cpBB <struct-boa> ; inline
149
150 TYPED: cpBBintersects ( a: cpBB b: cpBB -- ? )
151     {
152         [ [ l>> ] [ r>> ] bi* <= ]
153         [ [ r>> ] [ l>> ] bi*  > ]
154         [ [ b>> ] [ t>> ] bi* <= ]
155         [ [ t>> ] [ b>> ] bi*  > ]
156     } 2&& ; inline
157
158 TYPED: cpBBcontainsBB ( bb: cpBB other: cpBB -- ? )
159     {
160         [ [ l>> ] bi@ < ]
161         [ [ r>> ] bi@ > ]
162         [ [ b>> ] bi@ < ]
163         [ [ t>> ] bi@ > ]
164     } 2&& ; inline
165
166 TYPED: cpBBcontainsVect ( bb: cpBB v: cpVect -- ? )
167     {
168         [ [ l>> ] [ x>> ] bi* < ]
169         [ [ r>> ] [ x>> ] bi* > ]
170         [ [ b>> ] [ y>> ] bi* < ]
171         [ [ t>> ] [ y>> ] bi* > ]
172     } 2&& ; inline
173
174 TYPED: cpBBmerge ( a: cpBB b: cpBB -- c: cpBB )
175     {
176         [ [ l>> ] bi@ min ]
177         [ [ b>> ] bi@ min ]
178         [ [ r>> ] bi@ max ]
179         [ [ t>> ] bi@ max ]
180     } 2cleave cpBBNew ; inline
181
182 TYPED: cpBBexpand ( bb: cpBB v: cpVect -- b: cpBB )
183     {
184         [ [ l>> ] [ x>> ] bi* min ]
185         [ [ b>> ] [ y>> ] bi* min ]
186         [ [ r>> ] [ x>> ] bi* max ]
187         [ [ t>> ] [ y>> ] bi* max ]
188     } 2cleave cpBBNew ; inline
189
190 FUNCTION: cpVect cpBBClampVect ( cpBB bb, cpVect v ) ;
191 FUNCTION: cpVect cpBBWrapVect ( cpBB bb, cpVect v ) ;
192
193 ! cpBody.h
194 C-TYPE: cpBody
195 CALLBACK: void cpBodyVelocityFunc ( cpBody* body, cpVect gravity, cpFloat damping, cpFloat dt ) ;
196 CALLBACK: void cpBodyPositionFunc ( cpBody* body, cpFloat dt ) ;
197
198 STRUCT: cpBody
199     { velocity_func cpBodyVelocityFunc }
200     { position_func cpBodyPositionFunc }
201     { m             cpFloat            }
202     { m_inv         cpFloat            }
203     { i             cpFloat            }
204     { i_inv         cpFloat            }
205     { p             cpVect             }
206     { v             cpVect             }
207     { f             cpVect             }
208     { a             cpFloat            }
209     { w             cpFloat            }
210     { t             cpFloat            }
211     { rot           cpVect             }
212     { data          cpDataPointer      }
213     { v_limit       cpFloat            }
214     { w_limit       cpFloat            }
215     { v_bias        cpVect             }
216     { w_bias        cpFloat            } ;
217
218 FUNCTION: cpBody* cpBodyAlloc ( ) ;
219 FUNCTION: cpBody* cpBodyInit ( cpBody* body, cpFloat m, cpFloat i ) ;
220 FUNCTION: cpBody* cpBodyNew ( cpFloat m, cpFloat i ) ;
221 FUNCTION: void cpBodyDestroy ( cpBody* body ) ;
222 FUNCTION: void cpBodyFree ( cpBody* body ) ;
223 FUNCTION: void cpBodySetMass ( cpBody* body, cpFloat m ) ;
224 FUNCTION: void cpBodySetMoment ( cpBody* body, cpFloat i ) ;
225 FUNCTION: void cpBodySetAngle ( cpBody* body, cpFloat a ) ;
226 FUNCTION: void cpBodySlew ( cpBody* body, cpVect pos, cpFloat dt ) ;
227 FUNCTION: void cpBodyUpdateVelocity ( cpBody* body, cpVect gravity, cpFloat damping, cpFloat dt ) ;
228 FUNCTION: void cpBodyUpdatePosition ( cpBody* body, cpFloat dt ) ;
229
230 TYPED: cpBodyLocal2World ( body: cpBody v: cpVect -- v2: cpVect )
231     [ drop p>> ]
232     [ swap rot>> cpvrotate ] 2bi cpvadd ; inline
233
234 TYPED: cpBodyWorld2Local ( body: cpBody v: cpVect -- v2: cpVect )
235     [ swap p>> cpvsub ]
236     [ drop rot>> ] 2bi cpvunrotate ; inline
237
238 TYPED: cpBodyApplyImpulse ( body: cpBody j: cpVect r: cpVect -- )
239     [
240         drop
241         [ drop dup v>> ]
242         [ swap m_inv>> cpvmult ] 2bi cpvadd >>v drop
243     ]
244     [
245         [ 2drop dup w_bias>> ]
246         [ swap cpvcross [ i_inv>> ] dip * ] 3bi + >>w_bias drop
247     ] 3bi ; inline
248
249 FUNCTION: void cpBodyResetForces ( cpBody* body ) ;
250 FUNCTION: void cpBodyApplyForce ( cpBody* body, cpVect f, cpVect r ) ;
251 FUNCTION: void cpApplyDampedSpring ( cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2, cpFloat rlen, cpFloat k, cpFloat dmp, cpFloat dt ) ;
252
253 ! cpArray.h
254 STRUCT: cpArray
255     { num int    }
256     { max int    }
257     { arr void** } ;
258
259 CALLBACK: void cpArrayIter ( void* ptr, void* data ) ;
260
261 FUNCTION: cpArray* cpArrayAlloc ( ) ;
262 FUNCTION: cpArray* cpArrayInit ( cpArray* arr, int size ) ;
263 FUNCTION: cpArray* cpArrayNew ( int size ) ;
264 FUNCTION: void cpArrayDestroy ( cpArray* arr ) ;
265 FUNCTION: void cpArrayFree ( cpArray* arr ) ;
266 FUNCTION: void cpArrayPush ( cpArray* arr, void* object ) ;
267 FUNCTION: void cpArrayDeleteIndex ( cpArray* arr, int idx ) ;
268 FUNCTION: void cpArrayDeleteObj ( cpArray* arr, void* obj ) ;
269 FUNCTION: void cpArrayEach ( cpArray* arr, cpArrayIter iterFunc, void* data ) ;
270 FUNCTION: int cpArrayContains ( cpArray* arr, void* ptr ) ;
271
272 ! cpHashSet.h
273 STRUCT: cpHashSetBin
274     { elt  void*         }
275     { hash cpHashValue   }
276     { next cpHashSetBin* } ;
277
278 CALLBACK: int cpHashSetEqlFunc ( void* ptr, void* elt ) ;
279 CALLBACK: void* cpHashSetTransFunc ( void* ptr, void* data ) ;
280 CALLBACK: void cpHashSetIterFunc ( void* elt, void* data ) ;
281 CALLBACK: int cpHashSetFilterFunc ( void* elt, void* data ) ;
282
283 STRUCT: cpHashSet
284     { entries       int                }
285     { size          int                }
286     { eql           cpHashSetEqlFunc   }
287     { trans         cpHashSetTransFunc }
288     { default_value void*              }
289     { table         cpHashSetBin**     } ;
290
291 FUNCTION: void cpHashSetDestroy ( cpHashSet* set ) ;
292 FUNCTION: void cpHashSetFree ( cpHashSet* set ) ;
293 FUNCTION: cpHashSet* cpHashSetAlloc ( ) ;
294 FUNCTION: cpHashSet* cpHashSetInit ( cpHashSet* set, int size, cpHashSetEqlFunc eqlFunc, cpHashSetTransFunc trans ) ;
295 FUNCTION: cpHashSet* cpHashSetNew ( int size, cpHashSetEqlFunc eqlFunc, cpHashSetTransFunc trans ) ;
296 FUNCTION: void* cpHashSetInsert ( cpHashSet* set, cpHashValue hash, void* ptr, void* data ) ;
297 FUNCTION: void* cpHashSetRemove ( cpHashSet* set, cpHashValue hash, void* ptr ) ;
298 FUNCTION: void* cpHashSetFind ( cpHashSet* set, cpHashValue hash, void* ptr ) ;
299 FUNCTION: void cpHashSetEach ( cpHashSet* set, cpHashSetIterFunc func, void* data ) ;
300 FUNCTION: void cpHashSetFilter ( cpHashSet* set, cpHashSetFilterFunc func, void* data ) ;
301
302 ! cpSpaceHash.h
303 STRUCT: cpHandle
304     { obj    void* }
305     { retain int   }
306     { stamp  int   } ;
307
308 STRUCT: cpSpaceHashBin
309     { handle cpHandle*       }
310     { next   cpSpaceHashBin* } ;
311
312 CALLBACK: cpBB cpSpaceHashBBFunc ( void* obj ) ;
313
314 STRUCT: cpSpaceHash
315     { numcells  int               }
316     { celldim   cpFloat           }
317     { bbfunc    cpSpaceHashBBFunc }
318     { handleSet cpHashSet*        }
319     { table     cpSpaceHashBin**  }
320     { bins      cpSpaceHashBin*   }
321     { stamp     int               } ;
322
323 FUNCTION: cpSpaceHash* cpSpaceHashAlloc ( ) ;
324 FUNCTION: cpSpaceHash* cpSpaceHashInit ( cpSpaceHash* hash, cpFloat celldim, int cells, cpSpaceHashBBFunc bbfunc ) ;
325 FUNCTION: cpSpaceHash* cpSpaceHashNew ( cpFloat celldim, int cells, cpSpaceHashBBFunc bbfunc ) ;
326 FUNCTION: void cpSpaceHashDestroy ( cpSpaceHash* hash ) ;
327 FUNCTION: void cpSpaceHashFree ( cpSpaceHash* hash ) ;
328 FUNCTION: void cpSpaceHashResize ( cpSpaceHash* hash, cpFloat celldim, int numcells ) ;
329 FUNCTION: void cpSpaceHashInsert ( cpSpaceHash* hash, void* obj, cpHashValue id, cpBB bb ) ;
330 FUNCTION: void cpSpaceHashRemove ( cpSpaceHash* hash, void* obj, cpHashValue id ) ;
331 CALLBACK: void cpSpaceHashIterator ( void* obj, void* data ) ;
332 FUNCTION: void cpSpaceHashEach ( cpSpaceHash* hash, cpSpaceHashIterator func, void* data ) ;
333 FUNCTION: void cpSpaceHashRehash ( cpSpaceHash* hash ) ;
334 FUNCTION: void cpSpaceHashRehashObject ( cpSpaceHash* hash, void* obj, cpHashValue id ) ;
335 CALLBACK: void cpSpaceHashQueryFunc ( void* obj1, void* obj2, void* data ) ;
336 FUNCTION: void cpSpaceHashPointQuery ( cpSpaceHash* hash, cpVect point, cpSpaceHashQueryFunc func, void* data ) ;
337 FUNCTION: void cpSpaceHashQuery ( cpSpaceHash* hash, void* obj, cpBB bb, cpSpaceHashQueryFunc func, void* data ) ;
338 FUNCTION: void cpSpaceHashQueryRehash ( cpSpaceHash* hash, cpSpaceHashQueryFunc func, void* data ) ;
339 CALLBACK: cpFloat cpSpaceHashSegmentQueryFunc ( void* obj1, void* obj2, void* data ) ;
340 FUNCTION: void cpSpaceHashSegmentQuery ( cpSpaceHash* hash, void* obj, cpVect a, cpVect b, cpFloat t_exit, cpSpaceHashSegmentQueryFunc func, void* data ) ;
341
342 ! cpShape.h
343 C-TYPE: cpShape
344 C-TYPE: cpShapeClass
345
346 STRUCT: cpSegmentQueryInfo
347     { shape cpShape* }
348     { t     cpFloat  }
349     { n     cpVect   } ;
350
351 C-ENUM:
352     CP_CIRCLE_SHAPE
353     CP_SEGMENT_SHAPE
354     CP_POLY_SHAPE
355     CP_NUM_SHAPES ;
356 TYPEDEF: int cpShapeType
357
358 CALLBACK: cpBB cacheData_cb ( cpShape* shape, cpVect p, cpVect rot ) ;
359 CALLBACK: void destroy_cb ( cpShape* shape ) ;
360 CALLBACK: int pointQuery_cb ( cpShape* shape, cpVect p ) ;
361 CALLBACK: void segmentQuery_cb ( cpShape* shape, cpVect a, cpVect b, cpSegmentQueryInfo* info ) ;
362
363 STRUCT: cpShapeClass
364     { type         cpShapeType     }
365     { cacheData    cacheData_cb    }
366     { destroy      destroy_cb      }
367     { pointQuery   pointQuery_cb   }
368     { segmentQuery segmentQuery_cb } ;
369
370 STRUCT: cpShape
371     { klass          cpShapeClass*   }
372     { body           cpBody*         }
373     { bb             cpBB            }
374     { sensor         int             }
375     { e              cpFloat         }
376     { u              cpFloat         }
377     { surface_v      cpVect          }
378     { data           cpDataPointer   }
379     { collision_type cpCollisionType }
380     { group          cpGroup         }
381     { layers         cpLayers        }
382     { hashid         cpHashValue     } ;
383
384 FUNCTION: cpShape* cpShapeInit ( cpShape* shape, cpShapeClass* klass, cpBody* body ) ;
385 FUNCTION: void cpShapeDestroy ( cpShape* shape ) ;
386 FUNCTION: void cpShapeFree ( cpShape* shape ) ;
387 FUNCTION: cpBB cpShapeCacheBB ( cpShape* shape ) ;
388 FUNCTION: int cpShapePointQuery ( cpShape* shape, cpVect p ) ;
389
390 STRUCT: cpCircleShape
391     { shape cpShape }
392     { c     cpVect  }
393     { r     cpFloat }
394     { tc    cpVect  } ;
395
396 FUNCTION: cpCircleShape* cpCircleShapeAlloc ( ) ;
397 FUNCTION: cpCircleShape* cpCircleShapeInit ( cpCircleShape* circle, cpBody* body, cpFloat radius, cpVect offset ) ;
398 FUNCTION: cpShape* cpCircleShapeNew ( cpBody* body, cpFloat radius, cpVect offset ) ;
399
400 STRUCT: cpSegmentShape
401     { shape cpShape }
402     { a     cpVect  }
403     { b     cpVect  }
404     { n     cpVect  }
405     { r     cpFloat }
406     { ta    cpVect  }
407     { tb    cpVect  }
408     { tn    cpVect  } ;
409
410 FUNCTION: cpSegmentShape* cpSegmentShapeAlloc ( ) ;
411 FUNCTION: cpSegmentShape* cpSegmentShapeInit ( cpSegmentShape* seg, cpBody* body, cpVect a, cpVect b, cpFloat radius ) ;
412 FUNCTION: cpShape* cpSegmentShapeNew ( cpBody* body, cpVect a, cpVect b, cpFloat radius ) ;
413 FUNCTION: void cpResetShapeIdCounter ( ) ;
414 FUNCTION: void cpSegmentQueryInfoPrint ( cpSegmentQueryInfo* info ) ;
415 FUNCTION: int cpShapeSegmentQuery ( cpShape* shape, cpVect a, cpVect b, cpSegmentQueryInfo* info ) ;
416
417 TYPED: cpSegmentQueryHitPoint ( start: cpVect end: cpVect info: cpSegmentQueryInfo -- hit-point: cpVect )
418     t>> cpvlerp ; inline
419
420 TYPED: cpSegmentQueryHitDist ( start: cpVect end: cpVect info: cpSegmentQueryInfo -- hit-dist )
421     t>> [ cpvdist ] dip * ; inline
422
423 ! cpPolyShape.h
424 STRUCT: cpPolyShapeAxis
425     { n cpVect  }
426     { d cpFloat } ;
427 SPECIALIZED-ARRAY: cpPolyShapeAxis
428
429 STRUCT: cpPolyShape
430     { shape    cpShape          }
431     { numVerts int              }
432     { verts    cpVect*          }
433     { axes     cpPolyShapeAxis* }
434     { tVerts   cpVect*          }
435     { tAxes    cpPolyShapeAxis* } ;
436
437 FUNCTION: cpPolyShape* cpPolyShapeAlloc ( ) ;
438 FUNCTION: cpPolyShape* cpPolyShapeInit ( cpPolyShape* poly, cpBody* body, int numVerts, cpVect* verts, cpVect offset ) ;
439 FUNCTION: cpShape* cpPolyShapeNew ( cpBody* body, int numVerts, cpVect* verts, cpVect offset ) ;
440 FUNCTION: int cpPolyValidate ( cpVect* verts, int numVerts ) ;
441 FUNCTION: int cpPolyShapeGetNumVerts ( cpShape* shape ) ;
442 FUNCTION: cpVect cpPolyShapeGetVert ( cpShape* shape, int idx ) ;
443
444 TYPED: cpPolyShapeValueOnAxis ( poly: cpPolyShape n: cpVect d -- min-dist )
445     swap rot [ numVerts>> ] [ tVerts>> swap <direct-cpVect-array> ] bi swap
446     [ cpvdot ] curry [ min ] reduce swap - ; inline
447
448 TYPED: cpPolyShapeContainsVert ( poly: cpPolyShape v: cpVect -- ? )
449     swap [ numVerts>> ] [ tAxes>> swap <direct-cpPolyShapeAxis-array> ] bi swap
450     [
451         [ [ n>> ] dip cpvdot ] [ drop d>> ] 2bi -
452     ] curry [ max ] reduce 0.0 <= ; inline
453
454 TYPED: cpPolyShapeContainsVertPartial ( poly: cpPolyShape v: cpVect n: cpVect -- ? )
455     rot [ numVerts>> ] [ tAxes>> swap <direct-cpPolyShapeAxis-array> ] bi -rot
456     [| axis v n |
457         axis n>> n cpvdot 0.0 < 0
458         [ 0.0 ]
459         [ axis n>> v cpvdot axis d>> - ]
460         if
461     ] 2curry [ max ] reduce 0.0 <= ; inline
462
463 ! cpArbiter.h
464 C-TYPE: cpArbiter
465 C-TYPE: cpSpace
466 C-TYPE: cpCollisionHandler
467
468 STRUCT: cpContact
469     { p      cpVect      }
470     { n      cpVect      }
471     { dist   cpFloat     }
472     { r1     cpVect      }
473     { r2     cpVect      }
474     { nMass  cpFloat     }
475     { tMass  cpFloat     }
476     { bounce cpFloat     }
477     { jnAcc  cpFloat     }
478     { jtAcc  cpFloat     }
479     { jBias  cpFloat     }
480     { bias   cpFloat     }
481     { hash   cpHashValue } ;
482
483 FUNCTION: cpContact* cpContactInit ( cpContact* con, cpVect p, cpVect n, cpFloat dist, cpHashValue hash ) ;
484
485 C-ENUM:
486     cpArbiterStateNormal
487     cpArbiterStateFirstColl
488     cpArbiterStateIgnore ;
489 TYPEDEF: int cpArbiterState
490
491 STRUCT: cpArbiter
492     { numContacts int                 }
493     { contacts    cpContact*          }
494     { a           cpShape*            }
495     { b           cpShape*            }
496     { e           cpFloat             }
497     { u           cpFloat             }
498     { surface_vr  cpVect              }
499     { stamp       int                 }
500     { handler     cpCollisionHandler* }
501     { swappedColl char                }
502     { state       char                } ;
503
504 FUNCTION: cpArbiter* cpArbiterAlloc ( ) ;
505 FUNCTION: cpArbiter* cpArbiterInit ( cpArbiter* arb, cpShape* a, cpShape* b ) ;
506 FUNCTION: cpArbiter* cpArbiterNew ( cpShape* a, cpShape* b ) ;
507 FUNCTION: void cpArbiterDestroy ( cpArbiter* arb ) ;
508 FUNCTION: void cpArbiterFree ( cpArbiter* arb ) ;
509 FUNCTION: void cpArbiterUpdate ( cpArbiter* arb, cpContact* contacts, int numContacts, cpCollisionHandler* handler, cpShape* a, cpShape* b ) ;
510 FUNCTION: void cpArbiterPreStep ( cpArbiter* arb, cpFloat dt_inv ) ;
511 FUNCTION: void cpArbiterApplyCachedImpulse ( cpArbiter* arb ) ;
512 FUNCTION: void cpArbiterApplyImpulse ( cpArbiter* arb, cpFloat eCoef ) ;
513 FUNCTION: cpVect cpArbiterTotalImpulse ( cpArbiter* arb ) ;
514 FUNCTION: cpVect cpArbiterTotalImpulseWithFriction ( cpArbiter* arb ) ;
515 FUNCTION: void cpArbiterIgnore ( cpArbiter* arb ) ;
516
517 TYPED: cpArbiterGetShapes ( arb: cpArbiter -- a: cpShape b: cpShape )
518     dup swappedColl>> 0 = [
519         [ a>> ] [ b>> ] bi
520     ] [
521         [ b>> ] [ a>> ] bi
522     ] if ; inline
523
524 TYPED: cpArbiterIsFirstContact ( arb: cpArbiter -- ? )
525     state>> cpArbiterStateFirstColl = ; inline
526
527 TYPED: cpArbiterGetNormal ( arb: cpArbiter i -- n: cpVect )
528     [
529         swap
530         [ numContacts>> ]
531         [ contacts>> swap <direct-void*-array> ] bi nth cpContact memory>struct n>>
532     ]
533     [
534         drop swappedColl>> 0 = [ ] [ cpvneg ] if
535     ] 2bi ; inline
536
537 TYPED: cpArbiterGetPoint ( arb: cpArbiter i -- p: cpVect )
538     swap
539     [ numContacts>> ]
540     [ contacts>> swap <direct-void*-array> ] bi
541     nth cpContact memory>struct p>> ; inline
542
543 ! cpCollision.h
544 FUNCTION: int cpCollideShapes ( cpShape* a, cpShape* b, cpContact** arr ) ;
545
546 ! cpConstraint.h
547
548 C-TYPE: cpConstraintClass
549 C-TYPE: cpConstraint
550
551 CALLBACK: void cpConstraintPreStepFunction ( cpConstraint* constraint, cpFloat dt, cpFloat dt_inv ) ;
552 CALLBACK: void cpConstraintApplyImpulseFunction ( cpConstraint* constraint ) ;
553 CALLBACK: cpFloat cpConstraintGetImpulseFunction ( cpConstraint* constraint ) ;
554
555 STRUCT: cpConstraintClass
556     { preStep      cpConstraintPreStepFunction      }
557     { applyImpulse cpConstraintApplyImpulseFunction }
558     { getImpulse   cpConstraintGetImpulseFunction   } ;
559
560 STRUCT: cpConstraint
561     { klass    cpConstraintClass* }
562     { a        cpBody*            }
563     { b        cpBody*            }
564     { maxForce cpFloat            }
565     { biasCoef cpFloat            }
566     { maxBias  cpFloat            }
567     { data     cpDataPointer      } ;
568
569 FUNCTION: void cpConstraintDestroy ( cpConstraint* constraint ) ;
570 FUNCTION: void cpConstraintFree ( cpConstraint* constraint ) ;
571 FUNCTION: void cpConstraintCheckCast ( cpConstraint* constraint, cpConstraintClass* klass ) ;
572
573 ! cpPinJoint.h
574 FUNCTION: cpConstraintClass* cpPinJointGetClass ( ) ;
575
576 STRUCT: cpPinJoint
577     { constraint cpConstraint }
578     { anchr1     cpVect       }
579     { anchr2     cpVect       }
580     { dist       cpFloat      }
581     { r1         cpVect       }
582     { r2         cpVect       }
583     { n          cpVect       }
584     { nMass      cpFloat      }
585     { jnAcc      cpFloat      }
586     { jnMax      cpFloat      }
587     { bias       cpFloat      } ;
588
589 FUNCTION: cpPinJoint* cpPinJointAlloc ( ) ;
590 FUNCTION: cpPinJoint* cpPinJointInit ( cpPinJoint* joint, cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2 ) ;
591 FUNCTION: cpConstraint* cpPinJointNew ( cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2 ) ;
592
593 ! cpSlideJoint.h
594 FUNCTION: cpConstraintClass* cpSlideJointGetClass ( ) ;
595
596 STRUCT: cpSlideJoint
597     { constraint cpConstraint }
598     { anchr1     cpVect       }
599     { anchr2     cpVect       }
600     { min        cpFloat      }
601     { max        cpFloat      }
602     { r1         cpVect       }
603     { r2         cpVect       }
604     { n          cpVect       }
605     { nMass      cpFloat      }
606     { jnAcc      cpFloat      }
607     { jnMax      cpFloat      }
608     { bias       cpFloat      } ;
609
610 FUNCTION: cpSlideJoint* cpSlideJointAlloc ( ) ;
611 FUNCTION: cpSlideJoint* cpSlideJointInit ( cpSlideJoint* joint, cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2, cpFloat min, cpFloat max ) ;
612 FUNCTION: cpConstraint* cpSlideJointNew ( cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2, cpFloat min, cpFloat max ) ;
613
614 ! cpPivotJoint.h
615 FUNCTION: cpConstraintClass* cpPivotJointGetClass ( ) ;
616
617 STRUCT: cpPivotJoint
618     { constraint cpConstraint }
619     { anchr1     cpVect       }
620     { anchr2     cpVect       }
621     { r1         cpVect       }
622     { r2         cpVect       }
623     { k1         cpVect       }
624     { k2         cpVect       }
625     { jAcc       cpVect       }
626     { jMaxLen    cpFloat      }
627     { bias       cpVect       } ;
628
629 FUNCTION: cpPivotJoint* cpPivotJointAlloc ( ) ;
630 FUNCTION: cpPivotJoint* cpPivotJointInit ( cpPivotJoint* joint, cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2 ) ;
631 FUNCTION: cpConstraint* cpPivotJointNew ( cpBody* a, cpBody* b, cpVect pivot ) ;
632 FUNCTION: cpConstraint* cpPivotJointNew2 ( cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2 ) ;
633
634 ! cpGrooveJoint.h
635 FUNCTION: cpConstraintClass* cpGrooveJointGetClass ( ) ;
636
637 STRUCT: cpGrooveJoint
638     { constraint   cpConstraint   }
639     { grv_n        cpVect         }
640     { grv_a        cpVect         }
641     { grv_b        cpVect         }
642     { anchr2       cpVect         }
643     { grv_tn       cpVect         }
644     { clamp        cpFloat        }
645     { r1           cpVect         }
646     { r2           cpVect         }
647     { k1           cpVect         }
648     { k2           cpVect         }
649     { jAcc         cpVect         }
650     { jMaxLen      cpFloat        }
651     { bias         cpVect         } ;
652
653 FUNCTION: cpGrooveJoint* cpGrooveJointAlloc ( ) ;
654 FUNCTION: cpGrooveJoint* cpGrooveJointInit ( cpGrooveJoint* joint, cpBody* a, cpBody* b, cpVect groove_a, cpVect groove_b, cpVect anchr2 ) ;
655 FUNCTION: cpConstraint* cpGrooveJointNew ( cpBody* a, cpBody* b, cpVect groove_a, cpVect groove_b, cpVect anchr2 ) ;
656
657 ! cpDampedSpring.h
658 CALLBACK: cpFloat cpDampedSpringForceFunc ( cpConstraint* spring, cpFloat dist ) ;
659 FUNCTION: cpConstraintClass* cpDampedSpringGetClass ( ) ;
660
661 STRUCT: cpDampedSpring
662     { constraint      cpConstraint            }
663     { anchr1          cpVect                  }
664     { anchr2          cpVect                  }
665     { restLength      cpFloat                 }
666     { stiffness       cpFloat                 }
667     { damping         cpFloat                 }
668     { springForceFunc cpDampedSpringForceFunc }
669     { dt              cpFloat                 }
670     { target_vrn      cpFloat                 }
671     { r1              cpVect                  }
672     { r2              cpVect                  }
673     { nMass           cpFloat                 }
674     { n               cpVect                  } ;
675
676 FUNCTION: cpDampedSpring* cpDampedSpringAlloc ( ) ;
677 FUNCTION: cpDampedSpring* cpDampedSpringInit ( cpDampedSpring* joint, cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2, cpFloat restLength, cpFloat stiffness, cpFloat damping ) ;
678 FUNCTION: cpConstraint* cpDampedSpringNew ( cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2, cpFloat restLength, cpFloat stiffness, cpFloat damping ) ;
679
680 ! cpDampedRotarySpring.h
681 CALLBACK: cpFloat cpDampedRotarySpringTorqueFunc ( cpConstraint* spring, cpFloat relativeAngle ) ;
682 FUNCTION: cpConstraintClass* cpDampedRotarySpringGetClass ( ) ;
683
684 STRUCT: cpDampedRotarySpring
685     { constraint       cpConstraint                   }
686     { restAngle        cpFloat                        }
687     { stiffness        cpFloat                        }
688     { damping          cpFloat                        }
689     { springTorqueFunc cpDampedRotarySpringTorqueFunc }
690     { dt               cpFloat                        }
691     { target_wrn       cpFloat                        }
692     { iSum             cpFloat                        } ;
693
694 FUNCTION: cpDampedRotarySpring* cpDampedRotarySpringAlloc ( ) ;
695 FUNCTION: cpDampedRotarySpring* cpDampedRotarySpringInit ( cpDampedRotarySpring* joint, cpBody* a, cpBody* b, cpFloat restAngle, cpFloat stiffness, cpFloat damping ) ;
696 FUNCTION: cpConstraint* cpDampedRotarySpringNew ( cpBody* a, cpBody* b, cpFloat restAngle, cpFloat stiffness, cpFloat damping ) ;
697
698 ! cpRotaryLimitJoint.h
699 FUNCTION: cpConstraintClass* cpRotaryLimitJointGetClass ( ) ;
700
701 STRUCT: cpRotaryLimitJoint
702     { constraint cpConstraint   }
703     { min        cpFloat        }
704     { max        cpFloat        }
705     { iSum       cpFloat        }
706     { bias       cpFloat        }
707     { jAcc       cpFloat        }
708     { jMax       cpFloat        } ;
709
710 FUNCTION: cpRotaryLimitJoint* cpRotaryLimitJointAlloc ( ) ;
711 FUNCTION: cpRotaryLimitJoint* cpRotaryLimitJointInit ( cpRotaryLimitJoint* joint, cpBody* a, cpBody* b, cpFloat min, cpFloat max ) ;
712 FUNCTION: cpConstraint* cpRotaryLimitJointNew ( cpBody* a, cpBody* b, cpFloat min, cpFloat max ) ;
713
714 ! cpRatchetJoint.h
715 FUNCTION: cpConstraintClass* cpRatchetJointGetClass ( ) ;
716
717 STRUCT: cpRatchetJoint
718     { constraint cpConstraint }
719     { angle      cpFloat      }
720     { phase      cpFloat      }
721     { ratchet    cpFloat      }
722     { iSum       cpFloat      }
723     { bias       cpFloat      }
724     { jAcc       cpFloat      }
725     { jMax       cpFloat      } ;
726
727 FUNCTION: cpRatchetJoint* cpRatchetJointAlloc ( ) ;
728 FUNCTION: cpRatchetJoint* cpRatchetJointInit ( cpRatchetJoint* joint, cpBody* a, cpBody* b, cpFloat phase, cpFloat ratchet ) ;
729 FUNCTION: cpConstraint* cpRatchetJointNew ( cpBody* a, cpBody* b, cpFloat phase, cpFloat ratchet ) ;
730
731 ! cpGearJoint.h
732 FUNCTION: cpConstraintClass* cpGearJointGetClass ( ) ;
733
734 STRUCT: cpGearJoint
735     { constraint cpConstraint }
736     { phase      cpFloat      }
737     { ratio      cpFloat      }
738     { ratio_inv  cpFloat      }
739     { iSum       cpFloat      }
740     { bias       cpFloat      }
741     { jAcc       cpFloat      }
742     { jMax       cpFloat      } ;
743
744 FUNCTION: cpGearJoint* cpGearJointAlloc ( ) ;
745 FUNCTION: cpGearJoint* cpGearJointInit ( cpGearJoint* joint, cpBody* a, cpBody* b, cpFloat phase, cpFloat ratio ) ;
746 FUNCTION: cpConstraint* cpGearJointNew ( cpBody* a, cpBody* b, cpFloat phase, cpFloat ratio ) ;
747 FUNCTION: void cpGearJointSetRatio ( cpConstraint* constraint, cpFloat value ) ;
748
749 ! cpSimpleMotor.h
750 FUNCTION: cpConstraintClass* cpSimpleMotorGetClass ( ) ;
751
752 STRUCT: cpSimpleMotor
753     { constraint cpConstraint }
754     { rate       cpFloat      }
755     { iSum       cpFloat      }
756     { jAcc       cpFloat      }
757     { jMax       cpFloat      } ;
758
759 FUNCTION: cpSimpleMotor* cpSimpleMotorAlloc ( ) ;
760 FUNCTION: cpSimpleMotor* cpSimpleMotorInit ( cpSimpleMotor* joint, cpBody* a, cpBody* b, cpFloat rate ) ;
761 FUNCTION: cpConstraint* cpSimpleMotorNew ( cpBody* a, cpBody* b, cpFloat rate ) ;
762
763 ! cpSpace.h
764 C-TYPE: cpSpace
765
766 CALLBACK: int cpCollisionBeginFunc ( cpArbiter* arb, cpSpace* space, void* data ) ;
767 CALLBACK: int cpCollisionPreSolveFunc ( cpArbiter* arb, cpSpace* space, void* data ) ;
768 CALLBACK: void cpCollisionPostSolveFunc ( cpArbiter* arb, cpSpace* space, void* data ) ;
769 CALLBACK: void cpCollisionSeparateFunc ( cpArbiter* arb, cpSpace* space, void* data ) ;
770
771 STRUCT: cpCollisionHandler
772     { a         cpCollisionType          }
773     { b         cpCollisionType          }
774     { begin     cpCollisionBeginFunc     }
775     { preSolve  cpCollisionPreSolveFunc  }
776     { postSolve cpCollisionPostSolveFunc }
777     { separate  cpCollisionSeparateFunc  }
778     { data      void*                    } ;
779
780 STRUCT: cpSpace
781     { iterations        int                }
782     { elasticIterations int                }
783     { gravity           cpVect             }
784     { damping           cpFloat            }
785     { stamp             int                }
786     { staticShapes      cpSpaceHash*       }
787     { activeShapes      cpSpaceHash*       }
788     { bodies            cpArray*           }
789     { arbiters          cpArray*           }
790     { contactSet        cpHashSet*         }
791     { constraints       cpArray*           }
792     { collFuncSet       cpHashSet*         }
793     { defaultHandler    cpCollisionHandler }
794     { postStepCallbacks cpHashSet*         } ;
795
796 FUNCTION: cpSpace* cpSpaceAlloc ( ) ;
797 FUNCTION: cpSpace* cpSpaceInit ( cpSpace* space ) ;
798 FUNCTION: cpSpace* cpSpaceNew ( ) ;
799 FUNCTION: void cpSpaceDestroy ( cpSpace* space ) ;
800 FUNCTION: void cpSpaceFree ( cpSpace* space ) ;
801 FUNCTION: void cpSpaceFreeChildren ( cpSpace* space ) ;
802 FUNCTION: void cpSpaceSetDefaultCollisionHandler (
803     cpSpace*                 space,
804     cpCollisionBeginFunc     begin,
805     cpCollisionPreSolveFunc  preSolve,
806     cpCollisionPostSolveFunc postSolve,
807     cpCollisionSeparateFunc  separate,
808     void*                    data ) ;
809 FUNCTION: void cpSpaceAddCollisionHandler (
810     cpSpace*                 space,
811     cpCollisionType          a,
812     cpCollisionType          b,
813     cpCollisionBeginFunc     begin,
814     cpCollisionPreSolveFunc  preSolve,
815     cpCollisionPostSolveFunc postSolve,
816     cpCollisionSeparateFunc  separate,
817     void*                    data ) ;
818 FUNCTION: void cpSpaceRemoveCollisionHandler ( cpSpace* space, cpCollisionType a, cpCollisionType b ) ;
819 FUNCTION: cpShape* cpSpaceAddShape ( cpSpace* space, cpShape* shape ) ;
820 FUNCTION: cpShape* cpSpaceAddStaticShape ( cpSpace* space, cpShape* shape ) ;
821 FUNCTION: cpBody* cpSpaceAddBody ( cpSpace* space, cpBody* body ) ;
822 FUNCTION: cpConstraint* cpSpaceAddConstraint ( cpSpace* space, cpConstraint* constraint ) ;
823 FUNCTION: void cpSpaceRemoveShape ( cpSpace* space, cpShape* shape ) ;
824 FUNCTION: void cpSpaceRemoveStaticShape ( cpSpace* space, cpShape* shape ) ;
825 FUNCTION: void cpSpaceRemoveBody ( cpSpace* space, cpBody* body ) ;
826 FUNCTION: void cpSpaceRemoveConstraint ( cpSpace* space, cpConstraint* constraint ) ;
827 CALLBACK: void cpPostStepFunc ( cpSpace* space, void* obj, void* data ) ;
828 FUNCTION: void cpSpaceAddPostStepCallback ( cpSpace* space, cpPostStepFunc func, void* obj, void* data ) ;
829 CALLBACK: void cpSpacePointQueryFunc ( cpShape* shape, void* data ) ;
830 FUNCTION: void cpSpacePointQuery ( cpSpace* space, cpVect point, cpLayers layers, cpGroup group, cpSpacePointQueryFunc func, void* data ) ;
831 FUNCTION: cpShape* cpSpacePointQueryFirst ( cpSpace* space, cpVect point, cpLayers layers, cpGroup group ) ;
832 CALLBACK: void cpSpaceSegmentQueryFunc ( cpShape* shape, cpFloat t, cpVect n, void* data ) ;
833 FUNCTION: int cpSpaceSegmentQuery ( cpSpace* space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSpaceSegmentQueryFunc func, void* data ) ;
834 FUNCTION: cpShape* cpSpaceSegmentQueryFirst ( cpSpace* space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSegmentQueryInfo* out ) ;
835 CALLBACK: void cpSpaceBBQueryFunc ( cpShape* shape, void* data ) ;
836 FUNCTION: void cpSpaceBBQuery ( cpSpace* space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryFunc func, void* data ) ;
837 CALLBACK: void cpSpaceBodyIterator ( cpBody* body, void* data ) ;
838 FUNCTION: void cpSpaceEachBody ( cpSpace* space, cpSpaceBodyIterator func, void* data ) ;
839 FUNCTION: void cpSpaceResizeStaticHash ( cpSpace* space, cpFloat dim, int count ) ;
840 FUNCTION: void cpSpaceResizeActiveHash ( cpSpace* space, cpFloat dim, int count ) ;
841 FUNCTION: void cpSpaceRehashStatic ( cpSpace* space ) ;
842 FUNCTION: void cpSpaceStep ( cpSpace* space, cpFloat dt ) ;
843
844 ! chipmunk.h
845 FUNCTION: void cpInitChipmunk ( ) ;
846 FUNCTION: cpFloat cpMomentForCircle ( cpFloat m, cpFloat r1, cpFloat r2, cpVect offset ) ;
847 FUNCTION: cpFloat cpMomentForSegment ( cpFloat m, cpVect a, cpVect b ) ;
848 FUNCTION: cpFloat cpMomentForPoly ( cpFloat m, int numVerts, cpVect* verts, cpVect offset ) ;
849