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