]> gitweb.factorcode.org Git - factor.git/blob - extra/chipmunk/ffi/ffi.factor
Change C-ENUM: to always take a type. Use f for anonymous enums. Update all uses.
[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: cpShapeType
352     CP_CIRCLE_SHAPE
353     CP_SEGMENT_SHAPE
354     CP_POLY_SHAPE
355     CP_NUM_SHAPES ;
356
357 CALLBACK: cpBB cacheData_cb ( cpShape* shape, cpVect p, cpVect rot ) ;
358 CALLBACK: void destroy_cb ( cpShape* shape ) ;
359 CALLBACK: int pointQuery_cb ( cpShape* shape, cpVect p ) ;
360 CALLBACK: void segmentQuery_cb ( cpShape* shape, cpVect a, cpVect b, cpSegmentQueryInfo* info ) ;
361
362 STRUCT: cpShapeClass
363     { type         cpShapeType     }
364     { cacheData    cacheData_cb    }
365     { destroy      destroy_cb      }
366     { pointQuery   pointQuery_cb   }
367     { segmentQuery segmentQuery_cb } ;
368
369 STRUCT: cpShape
370     { klass          cpShapeClass*   }
371     { body           cpBody*         }
372     { bb             cpBB            }
373     { sensor         int             }
374     { e              cpFloat         }
375     { u              cpFloat         }
376     { surface_v      cpVect          }
377     { data           cpDataPointer   }
378     { collision_type cpCollisionType }
379     { group          cpGroup         }
380     { layers         cpLayers        }
381     { hashid         cpHashValue     } ;
382
383 FUNCTION: cpShape* cpShapeInit ( cpShape* shape, cpShapeClass* klass, cpBody* body ) ;
384 FUNCTION: void cpShapeDestroy ( cpShape* shape ) ;
385 FUNCTION: void cpShapeFree ( cpShape* shape ) ;
386 FUNCTION: cpBB cpShapeCacheBB ( cpShape* shape ) ;
387 FUNCTION: int cpShapePointQuery ( cpShape* shape, cpVect p ) ;
388
389 STRUCT: cpCircleShape
390     { shape cpShape }
391     { c     cpVect  }
392     { r     cpFloat }
393     { tc    cpVect  } ;
394
395 FUNCTION: cpCircleShape* cpCircleShapeAlloc ( ) ;
396 FUNCTION: cpCircleShape* cpCircleShapeInit ( cpCircleShape* circle, cpBody* body, cpFloat radius, cpVect offset ) ;
397 FUNCTION: cpShape* cpCircleShapeNew ( cpBody* body, cpFloat radius, cpVect offset ) ;
398
399 STRUCT: cpSegmentShape
400     { shape cpShape }
401     { a     cpVect  }
402     { b     cpVect  }
403     { n     cpVect  }
404     { r     cpFloat }
405     { ta    cpVect  }
406     { tb    cpVect  }
407     { tn    cpVect  } ;
408
409 FUNCTION: cpSegmentShape* cpSegmentShapeAlloc ( ) ;
410 FUNCTION: cpSegmentShape* cpSegmentShapeInit ( cpSegmentShape* seg, cpBody* body, cpVect a, cpVect b, cpFloat radius ) ;
411 FUNCTION: cpShape* cpSegmentShapeNew ( cpBody* body, cpVect a, cpVect b, cpFloat radius ) ;
412 FUNCTION: void cpResetShapeIdCounter ( ) ;
413 FUNCTION: void cpSegmentQueryInfoPrint ( cpSegmentQueryInfo* info ) ;
414 FUNCTION: int cpShapeSegmentQuery ( cpShape* shape, cpVect a, cpVect b, cpSegmentQueryInfo* info ) ;
415
416 TYPED: cpSegmentQueryHitPoint ( start: cpVect end: cpVect info: cpSegmentQueryInfo -- hit-point: cpVect )
417     t>> cpvlerp ; inline
418
419 TYPED: cpSegmentQueryHitDist ( start: cpVect end: cpVect info: cpSegmentQueryInfo -- hit-dist )
420     t>> [ cpvdist ] dip * ; inline
421
422 ! cpPolyShape.h
423 STRUCT: cpPolyShapeAxis
424     { n cpVect  }
425     { d cpFloat } ;
426 SPECIALIZED-ARRAY: cpPolyShapeAxis
427
428 STRUCT: cpPolyShape
429     { shape    cpShape          }
430     { numVerts int              }
431     { verts    cpVect*          }
432     { axes     cpPolyShapeAxis* }
433     { tVerts   cpVect*          }
434     { tAxes    cpPolyShapeAxis* } ;
435
436 FUNCTION: cpPolyShape* cpPolyShapeAlloc ( ) ;
437 FUNCTION: cpPolyShape* cpPolyShapeInit ( cpPolyShape* poly, cpBody* body, int numVerts, cpVect* verts, cpVect offset ) ;
438 FUNCTION: cpShape* cpPolyShapeNew ( cpBody* body, int numVerts, cpVect* verts, cpVect offset ) ;
439 FUNCTION: int cpPolyValidate ( cpVect* verts, int numVerts ) ;
440 FUNCTION: int cpPolyShapeGetNumVerts ( cpShape* shape ) ;
441 FUNCTION: cpVect cpPolyShapeGetVert ( cpShape* shape, int idx ) ;
442
443 TYPED: cpPolyShapeValueOnAxis ( poly: cpPolyShape n: cpVect d -- min-dist )
444     swap rot [ numVerts>> ] [ tVerts>> swap <direct-cpVect-array> ] bi swap
445     [ cpvdot ] curry [ min ] reduce swap - ; inline
446
447 TYPED: cpPolyShapeContainsVert ( poly: cpPolyShape v: cpVect -- ? )
448     swap [ numVerts>> ] [ tAxes>> swap <direct-cpPolyShapeAxis-array> ] bi swap
449     [
450         [ [ n>> ] dip cpvdot ] [ drop d>> ] 2bi -
451     ] curry [ max ] reduce 0.0 <= ; inline
452
453 TYPED: cpPolyShapeContainsVertPartial ( poly: cpPolyShape v: cpVect n: cpVect -- ? )
454     rot [ numVerts>> ] [ tAxes>> swap <direct-cpPolyShapeAxis-array> ] bi -rot
455     [| axis v n |
456         axis n>> n cpvdot 0.0 < 0
457         [ 0.0 ]
458         [ axis n>> v cpvdot axis d>> - ]
459         if
460     ] 2curry [ max ] reduce 0.0 <= ; inline
461
462 ! cpArbiter.h
463 C-TYPE: cpArbiter
464 C-TYPE: cpSpace
465 C-TYPE: cpCollisionHandler
466
467 STRUCT: cpContact
468     { p      cpVect      }
469     { n      cpVect      }
470     { dist   cpFloat     }
471     { r1     cpVect      }
472     { r2     cpVect      }
473     { nMass  cpFloat     }
474     { tMass  cpFloat     }
475     { bounce cpFloat     }
476     { jnAcc  cpFloat     }
477     { jtAcc  cpFloat     }
478     { jBias  cpFloat     }
479     { bias   cpFloat     }
480     { hash   cpHashValue } ;
481
482 FUNCTION: cpContact* cpContactInit ( cpContact* con, cpVect p, cpVect n, cpFloat dist, cpHashValue hash ) ;
483
484 C-ENUM: cpArbiterState
485     cpArbiterStateNormal
486     cpArbiterStateFirstColl
487     cpArbiterStateIgnore ;
488
489 STRUCT: cpArbiter
490     { numContacts int                 }
491     { contacts    cpContact*          }
492     { a           cpShape*            }
493     { b           cpShape*            }
494     { e           cpFloat             }
495     { u           cpFloat             }
496     { surface_vr  cpVect              }
497     { stamp       int                 }
498     { handler     cpCollisionHandler* }
499     { swappedColl char                }
500     { state       char                } ;
501
502 FUNCTION: cpArbiter* cpArbiterAlloc ( ) ;
503 FUNCTION: cpArbiter* cpArbiterInit ( cpArbiter* arb, cpShape* a, cpShape* b ) ;
504 FUNCTION: cpArbiter* cpArbiterNew ( cpShape* a, cpShape* b ) ;
505 FUNCTION: void cpArbiterDestroy ( cpArbiter* arb ) ;
506 FUNCTION: void cpArbiterFree ( cpArbiter* arb ) ;
507 FUNCTION: void cpArbiterUpdate ( cpArbiter* arb, cpContact* contacts, int numContacts, cpCollisionHandler* handler, cpShape* a, cpShape* b ) ;
508 FUNCTION: void cpArbiterPreStep ( cpArbiter* arb, cpFloat dt_inv ) ;
509 FUNCTION: void cpArbiterApplyCachedImpulse ( cpArbiter* arb ) ;
510 FUNCTION: void cpArbiterApplyImpulse ( cpArbiter* arb, cpFloat eCoef ) ;
511 FUNCTION: cpVect cpArbiterTotalImpulse ( cpArbiter* arb ) ;
512 FUNCTION: cpVect cpArbiterTotalImpulseWithFriction ( cpArbiter* arb ) ;
513 FUNCTION: void cpArbiterIgnore ( cpArbiter* arb ) ;
514
515 TYPED: cpArbiterGetShapes ( arb: cpArbiter -- a: cpShape b: cpShape )
516     dup swappedColl>> 0 = [
517         [ a>> ] [ b>> ] bi
518     ] [
519         [ b>> ] [ a>> ] bi
520     ] if ; inline
521
522 TYPED: cpArbiterIsFirstContact ( arb: cpArbiter -- ? )
523     state>> cpArbiterStateFirstColl = ; inline
524
525 TYPED: cpArbiterGetNormal ( arb: cpArbiter i -- n: cpVect )
526     [
527         swap
528         [ numContacts>> ]
529         [ contacts>> swap <direct-void*-array> ] bi nth cpContact memory>struct n>>
530     ]
531     [
532         drop swappedColl>> 0 = [ ] [ cpvneg ] if
533     ] 2bi ; inline
534
535 TYPED: cpArbiterGetPoint ( arb: cpArbiter i -- p: cpVect )
536     swap
537     [ numContacts>> ]
538     [ contacts>> swap <direct-void*-array> ] bi
539     nth cpContact memory>struct p>> ; inline
540
541 ! cpCollision.h
542 FUNCTION: int cpCollideShapes ( cpShape* a, cpShape* b, cpContact** arr ) ;
543
544 ! cpConstraint.h
545
546 C-TYPE: cpConstraintClass
547 C-TYPE: cpConstraint
548
549 CALLBACK: void cpConstraintPreStepFunction ( cpConstraint* constraint, cpFloat dt, cpFloat dt_inv ) ;
550 CALLBACK: void cpConstraintApplyImpulseFunction ( cpConstraint* constraint ) ;
551 CALLBACK: cpFloat cpConstraintGetImpulseFunction ( cpConstraint* constraint ) ;
552
553 STRUCT: cpConstraintClass
554     { preStep      cpConstraintPreStepFunction      }
555     { applyImpulse cpConstraintApplyImpulseFunction }
556     { getImpulse   cpConstraintGetImpulseFunction   } ;
557
558 STRUCT: cpConstraint
559     { klass    cpConstraintClass* }
560     { a        cpBody*            }
561     { b        cpBody*            }
562     { maxForce cpFloat            }
563     { biasCoef cpFloat            }
564     { maxBias  cpFloat            }
565     { data     cpDataPointer      } ;
566
567 FUNCTION: void cpConstraintDestroy ( cpConstraint* constraint ) ;
568 FUNCTION: void cpConstraintFree ( cpConstraint* constraint ) ;
569 FUNCTION: void cpConstraintCheckCast ( cpConstraint* constraint, cpConstraintClass* klass ) ;
570
571 ! cpPinJoint.h
572 FUNCTION: cpConstraintClass* cpPinJointGetClass ( ) ;
573
574 STRUCT: cpPinJoint
575     { constraint cpConstraint }
576     { anchr1     cpVect       }
577     { anchr2     cpVect       }
578     { dist       cpFloat      }
579     { r1         cpVect       }
580     { r2         cpVect       }
581     { n          cpVect       }
582     { nMass      cpFloat      }
583     { jnAcc      cpFloat      }
584     { jnMax      cpFloat      }
585     { bias       cpFloat      } ;
586
587 FUNCTION: cpPinJoint* cpPinJointAlloc ( ) ;
588 FUNCTION: cpPinJoint* cpPinJointInit ( cpPinJoint* joint, cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2 ) ;
589 FUNCTION: cpConstraint* cpPinJointNew ( cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2 ) ;
590
591 ! cpSlideJoint.h
592 FUNCTION: cpConstraintClass* cpSlideJointGetClass ( ) ;
593
594 STRUCT: cpSlideJoint
595     { constraint cpConstraint }
596     { anchr1     cpVect       }
597     { anchr2     cpVect       }
598     { min        cpFloat      }
599     { max        cpFloat      }
600     { r1         cpVect       }
601     { r2         cpVect       }
602     { n          cpVect       }
603     { nMass      cpFloat      }
604     { jnAcc      cpFloat      }
605     { jnMax      cpFloat      }
606     { bias       cpFloat      } ;
607
608 FUNCTION: cpSlideJoint* cpSlideJointAlloc ( ) ;
609 FUNCTION: cpSlideJoint* cpSlideJointInit ( cpSlideJoint* joint, cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2, cpFloat min, cpFloat max ) ;
610 FUNCTION: cpConstraint* cpSlideJointNew ( cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2, cpFloat min, cpFloat max ) ;
611
612 ! cpPivotJoint.h
613 FUNCTION: cpConstraintClass* cpPivotJointGetClass ( ) ;
614
615 STRUCT: cpPivotJoint
616     { constraint cpConstraint }
617     { anchr1     cpVect       }
618     { anchr2     cpVect       }
619     { r1         cpVect       }
620     { r2         cpVect       }
621     { k1         cpVect       }
622     { k2         cpVect       }
623     { jAcc       cpVect       }
624     { jMaxLen    cpFloat      }
625     { bias       cpVect       } ;
626
627 FUNCTION: cpPivotJoint* cpPivotJointAlloc ( ) ;
628 FUNCTION: cpPivotJoint* cpPivotJointInit ( cpPivotJoint* joint, cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2 ) ;
629 FUNCTION: cpConstraint* cpPivotJointNew ( cpBody* a, cpBody* b, cpVect pivot ) ;
630 FUNCTION: cpConstraint* cpPivotJointNew2 ( cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2 ) ;
631
632 ! cpGrooveJoint.h
633 FUNCTION: cpConstraintClass* cpGrooveJointGetClass ( ) ;
634
635 STRUCT: cpGrooveJoint
636     { constraint   cpConstraint   }
637     { grv_n        cpVect         }
638     { grv_a        cpVect         }
639     { grv_b        cpVect         }
640     { anchr2       cpVect         }
641     { grv_tn       cpVect         }
642     { clamp        cpFloat        }
643     { r1           cpVect         }
644     { r2           cpVect         }
645     { k1           cpVect         }
646     { k2           cpVect         }
647     { jAcc         cpVect         }
648     { jMaxLen      cpFloat        }
649     { bias         cpVect         } ;
650
651 FUNCTION: cpGrooveJoint* cpGrooveJointAlloc ( ) ;
652 FUNCTION: cpGrooveJoint* cpGrooveJointInit ( cpGrooveJoint* joint, cpBody* a, cpBody* b, cpVect groove_a, cpVect groove_b, cpVect anchr2 ) ;
653 FUNCTION: cpConstraint* cpGrooveJointNew ( cpBody* a, cpBody* b, cpVect groove_a, cpVect groove_b, cpVect anchr2 ) ;
654
655 ! cpDampedSpring.h
656 CALLBACK: cpFloat cpDampedSpringForceFunc ( cpConstraint* spring, cpFloat dist ) ;
657 FUNCTION: cpConstraintClass* cpDampedSpringGetClass ( ) ;
658
659 STRUCT: cpDampedSpring
660     { constraint      cpConstraint            }
661     { anchr1          cpVect                  }
662     { anchr2          cpVect                  }
663     { restLength      cpFloat                 }
664     { stiffness       cpFloat                 }
665     { damping         cpFloat                 }
666     { springForceFunc cpDampedSpringForceFunc }
667     { dt              cpFloat                 }
668     { target_vrn      cpFloat                 }
669     { r1              cpVect                  }
670     { r2              cpVect                  }
671     { nMass           cpFloat                 }
672     { n               cpVect                  } ;
673
674 FUNCTION: cpDampedSpring* cpDampedSpringAlloc ( ) ;
675 FUNCTION: cpDampedSpring* cpDampedSpringInit ( cpDampedSpring* joint, cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2, cpFloat restLength, cpFloat stiffness, cpFloat damping ) ;
676 FUNCTION: cpConstraint* cpDampedSpringNew ( cpBody* a, cpBody* b, cpVect anchr1, cpVect anchr2, cpFloat restLength, cpFloat stiffness, cpFloat damping ) ;
677
678 ! cpDampedRotarySpring.h
679 CALLBACK: cpFloat cpDampedRotarySpringTorqueFunc ( cpConstraint* spring, cpFloat relativeAngle ) ;
680 FUNCTION: cpConstraintClass* cpDampedRotarySpringGetClass ( ) ;
681
682 STRUCT: cpDampedRotarySpring
683     { constraint       cpConstraint                   }
684     { restAngle        cpFloat                        }
685     { stiffness        cpFloat                        }
686     { damping          cpFloat                        }
687     { springTorqueFunc cpDampedRotarySpringTorqueFunc }
688     { dt               cpFloat                        }
689     { target_wrn       cpFloat                        }
690     { iSum             cpFloat                        } ;
691
692 FUNCTION: cpDampedRotarySpring* cpDampedRotarySpringAlloc ( ) ;
693 FUNCTION: cpDampedRotarySpring* cpDampedRotarySpringInit ( cpDampedRotarySpring* joint, cpBody* a, cpBody* b, cpFloat restAngle, cpFloat stiffness, cpFloat damping ) ;
694 FUNCTION: cpConstraint* cpDampedRotarySpringNew ( cpBody* a, cpBody* b, cpFloat restAngle, cpFloat stiffness, cpFloat damping ) ;
695
696 ! cpRotaryLimitJoint.h
697 FUNCTION: cpConstraintClass* cpRotaryLimitJointGetClass ( ) ;
698
699 STRUCT: cpRotaryLimitJoint
700     { constraint cpConstraint   }
701     { min        cpFloat        }
702     { max        cpFloat        }
703     { iSum       cpFloat        }
704     { bias       cpFloat        }
705     { jAcc       cpFloat        }
706     { jMax       cpFloat        } ;
707
708 FUNCTION: cpRotaryLimitJoint* cpRotaryLimitJointAlloc ( ) ;
709 FUNCTION: cpRotaryLimitJoint* cpRotaryLimitJointInit ( cpRotaryLimitJoint* joint, cpBody* a, cpBody* b, cpFloat min, cpFloat max ) ;
710 FUNCTION: cpConstraint* cpRotaryLimitJointNew ( cpBody* a, cpBody* b, cpFloat min, cpFloat max ) ;
711
712 ! cpRatchetJoint.h
713 FUNCTION: cpConstraintClass* cpRatchetJointGetClass ( ) ;
714
715 STRUCT: cpRatchetJoint
716     { constraint cpConstraint }
717     { angle      cpFloat      }
718     { phase      cpFloat      }
719     { ratchet    cpFloat      }
720     { iSum       cpFloat      }
721     { bias       cpFloat      }
722     { jAcc       cpFloat      }
723     { jMax       cpFloat      } ;
724
725 FUNCTION: cpRatchetJoint* cpRatchetJointAlloc ( ) ;
726 FUNCTION: cpRatchetJoint* cpRatchetJointInit ( cpRatchetJoint* joint, cpBody* a, cpBody* b, cpFloat phase, cpFloat ratchet ) ;
727 FUNCTION: cpConstraint* cpRatchetJointNew ( cpBody* a, cpBody* b, cpFloat phase, cpFloat ratchet ) ;
728
729 ! cpGearJoint.h
730 FUNCTION: cpConstraintClass* cpGearJointGetClass ( ) ;
731
732 STRUCT: cpGearJoint
733     { constraint cpConstraint }
734     { phase      cpFloat      }
735     { ratio      cpFloat      }
736     { ratio_inv  cpFloat      }
737     { iSum       cpFloat      }
738     { bias       cpFloat      }
739     { jAcc       cpFloat      }
740     { jMax       cpFloat      } ;
741
742 FUNCTION: cpGearJoint* cpGearJointAlloc ( ) ;
743 FUNCTION: cpGearJoint* cpGearJointInit ( cpGearJoint* joint, cpBody* a, cpBody* b, cpFloat phase, cpFloat ratio ) ;
744 FUNCTION: cpConstraint* cpGearJointNew ( cpBody* a, cpBody* b, cpFloat phase, cpFloat ratio ) ;
745 FUNCTION: void cpGearJointSetRatio ( cpConstraint* constraint, cpFloat value ) ;
746
747 ! cpSimpleMotor.h
748 FUNCTION: cpConstraintClass* cpSimpleMotorGetClass ( ) ;
749
750 STRUCT: cpSimpleMotor
751     { constraint cpConstraint }
752     { rate       cpFloat      }
753     { iSum       cpFloat      }
754     { jAcc       cpFloat      }
755     { jMax       cpFloat      } ;
756
757 FUNCTION: cpSimpleMotor* cpSimpleMotorAlloc ( ) ;
758 FUNCTION: cpSimpleMotor* cpSimpleMotorInit ( cpSimpleMotor* joint, cpBody* a, cpBody* b, cpFloat rate ) ;
759 FUNCTION: cpConstraint* cpSimpleMotorNew ( cpBody* a, cpBody* b, cpFloat rate ) ;
760
761 ! cpSpace.h
762 C-TYPE: cpSpace
763
764 CALLBACK: int cpCollisionBeginFunc ( cpArbiter* arb, cpSpace* space, void* data ) ;
765 CALLBACK: int cpCollisionPreSolveFunc ( cpArbiter* arb, cpSpace* space, void* data ) ;
766 CALLBACK: void cpCollisionPostSolveFunc ( cpArbiter* arb, cpSpace* space, void* data ) ;
767 CALLBACK: void cpCollisionSeparateFunc ( cpArbiter* arb, cpSpace* space, void* data ) ;
768
769 STRUCT: cpCollisionHandler
770     { a         cpCollisionType          }
771     { b         cpCollisionType          }
772     { begin     cpCollisionBeginFunc     }
773     { preSolve  cpCollisionPreSolveFunc  }
774     { postSolve cpCollisionPostSolveFunc }
775     { separate  cpCollisionSeparateFunc  }
776     { data      void*                    } ;
777
778 STRUCT: cpSpace
779     { iterations        int                }
780     { elasticIterations int                }
781     { gravity           cpVect             }
782     { damping           cpFloat            }
783     { stamp             int                }
784     { staticShapes      cpSpaceHash*       }
785     { activeShapes      cpSpaceHash*       }
786     { bodies            cpArray*           }
787     { arbiters          cpArray*           }
788     { contactSet        cpHashSet*         }
789     { constraints       cpArray*           }
790     { collFuncSet       cpHashSet*         }
791     { defaultHandler    cpCollisionHandler }
792     { postStepCallbacks cpHashSet*         } ;
793
794 FUNCTION: cpSpace* cpSpaceAlloc ( ) ;
795 FUNCTION: cpSpace* cpSpaceInit ( cpSpace* space ) ;
796 FUNCTION: cpSpace* cpSpaceNew ( ) ;
797 FUNCTION: void cpSpaceDestroy ( cpSpace* space ) ;
798 FUNCTION: void cpSpaceFree ( cpSpace* space ) ;
799 FUNCTION: void cpSpaceFreeChildren ( cpSpace* space ) ;
800 FUNCTION: void cpSpaceSetDefaultCollisionHandler (
801     cpSpace*                 space,
802     cpCollisionBeginFunc     begin,
803     cpCollisionPreSolveFunc  preSolve,
804     cpCollisionPostSolveFunc postSolve,
805     cpCollisionSeparateFunc  separate,
806     void*                    data ) ;
807 FUNCTION: void cpSpaceAddCollisionHandler (
808     cpSpace*                 space,
809     cpCollisionType          a,
810     cpCollisionType          b,
811     cpCollisionBeginFunc     begin,
812     cpCollisionPreSolveFunc  preSolve,
813     cpCollisionPostSolveFunc postSolve,
814     cpCollisionSeparateFunc  separate,
815     void*                    data ) ;
816 FUNCTION: void cpSpaceRemoveCollisionHandler ( cpSpace* space, cpCollisionType a, cpCollisionType b ) ;
817 FUNCTION: cpShape* cpSpaceAddShape ( cpSpace* space, cpShape* shape ) ;
818 FUNCTION: cpShape* cpSpaceAddStaticShape ( cpSpace* space, cpShape* shape ) ;
819 FUNCTION: cpBody* cpSpaceAddBody ( cpSpace* space, cpBody* body ) ;
820 FUNCTION: cpConstraint* cpSpaceAddConstraint ( cpSpace* space, cpConstraint* constraint ) ;
821 FUNCTION: void cpSpaceRemoveShape ( cpSpace* space, cpShape* shape ) ;
822 FUNCTION: void cpSpaceRemoveStaticShape ( cpSpace* space, cpShape* shape ) ;
823 FUNCTION: void cpSpaceRemoveBody ( cpSpace* space, cpBody* body ) ;
824 FUNCTION: void cpSpaceRemoveConstraint ( cpSpace* space, cpConstraint* constraint ) ;
825 CALLBACK: void cpPostStepFunc ( cpSpace* space, void* obj, void* data ) ;
826 FUNCTION: void cpSpaceAddPostStepCallback ( cpSpace* space, cpPostStepFunc func, void* obj, void* data ) ;
827 CALLBACK: void cpSpacePointQueryFunc ( cpShape* shape, void* data ) ;
828 FUNCTION: void cpSpacePointQuery ( cpSpace* space, cpVect point, cpLayers layers, cpGroup group, cpSpacePointQueryFunc func, void* data ) ;
829 FUNCTION: cpShape* cpSpacePointQueryFirst ( cpSpace* space, cpVect point, cpLayers layers, cpGroup group ) ;
830 CALLBACK: void cpSpaceSegmentQueryFunc ( cpShape* shape, cpFloat t, cpVect n, void* data ) ;
831 FUNCTION: int cpSpaceSegmentQuery ( cpSpace* space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSpaceSegmentQueryFunc func, void* data ) ;
832 FUNCTION: cpShape* cpSpaceSegmentQueryFirst ( cpSpace* space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSegmentQueryInfo* out ) ;
833 CALLBACK: void cpSpaceBBQueryFunc ( cpShape* shape, void* data ) ;
834 FUNCTION: void cpSpaceBBQuery ( cpSpace* space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryFunc func, void* data ) ;
835 CALLBACK: void cpSpaceBodyIterator ( cpBody* body, void* data ) ;
836 FUNCTION: void cpSpaceEachBody ( cpSpace* space, cpSpaceBodyIterator func, void* data ) ;
837 FUNCTION: void cpSpaceResizeStaticHash ( cpSpace* space, cpFloat dim, int count ) ;
838 FUNCTION: void cpSpaceResizeActiveHash ( cpSpace* space, cpFloat dim, int count ) ;
839 FUNCTION: void cpSpaceRehashStatic ( cpSpace* space ) ;
840 FUNCTION: void cpSpaceStep ( cpSpace* space, cpFloat dt ) ;
841
842 ! chipmunk.h
843 FUNCTION: void cpInitChipmunk ( ) ;
844 FUNCTION: cpFloat cpMomentForCircle ( cpFloat m, cpFloat r1, cpFloat r2, cpVect offset ) ;
845 FUNCTION: cpFloat cpMomentForSegment ( cpFloat m, cpVect a, cpVect b ) ;
846 FUNCTION: cpFloat cpMomentForPoly ( cpFloat m, int numVerts, cpVect* verts, cpVect offset ) ;
847