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