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