forked from matkuki/chipmunk7_demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chipmunk.nim
2600 lines (1877 loc) · 117 KB
/
chipmunk.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2015 Oleh Prypin <blaxpirit@gmail.com>
# Licensed under terms of MIT license (see LICENSE)
##[
!!! THIS IS A MODIFIED VERSION OF OLEH PRYPIN'S CHIPMUNK7 WRAPPER !!!
It can be compiled with the '--define:chipmunkUnsafe' option to show
extra information about the chipmunk spaces, BUT IT USES SOME OBJECT
FIELDS THAT ARE NOT PART OF THE PUBLIC API AND CAN CHANGE AT ANY TIME!
]##
{.deadCodeElim: on.}
{.warning[SmallLshouldNotBeUsed]: off.}
when defined chipmunkDestructors:
# Destroy is called automatically, based on scope
{.experimental.}
{.pragma: destroy, override.}
else:
# Call destroy() manually, beware of memory leaks!
{.pragma: destroy.}
{.passL: "-lpthread".}
when defined(windows):
const lib = "chipmunk.dll"
elif defined(mac):
const lib = "libchipmunk.dylib"
else:
const lib = "libchipmunk.so"
import math
proc `div`(x, y: cdouble): cdouble = x / y
converter toBool[T](x: ptr T): bool = x != nil
{.push dynlib: lib.}
when defined(chipmunkUnsafe):
const
CP_POLY_SHAPE_INLINE_ALLOC* = 6
type
Float* = cdouble
## Chipmunk's floating point type.
HashValue* = pointer
## Hash value type.
CollisionID* = uint32
## Type used internally to cache colliding object info for cpCollideShapes().
## Should be at least 32 bits.
DataPointer* = pointer
## Type used for user data pointers.
CollisionType* = pointer
## Type used for cpSpace.collision_type.
Group* = pointer
## Type used for cpShape.group.
Bitmask* = cuint
## Type used for cpShapeFilter category and mask.
Timestamp* = cuint
## Type used for various timestamps in Chipmunk.
Vect* {.bycopy.} = object
## Chipmunk's 2D vector type.
x*: Float
y*: Float
Transform* {.bycopy.} = object
## Column major affine transform.
a*: Float
b*: Float
c*: Float
d*: Float
tx*: Float
ty*: Float
Mat2x2* {.bycopy.} = object
a*: Float
b*: Float
c*: Float
d*: Float
ArrayObj* {.inheritable, bycopy, pure.} = object
num*: cint
max*: cint
arr*: ptr pointer
Array* = ptr ArrayObj
HashSet* = ptr object
BodyInnerStruct* {.bycopy.} = object
root*: Body
next*: Body
idleTime*: Float
BodyObj* {.bycopy, pure.} = object
velocity_func*: BodyVelocityFunc
position_func*: BodyPositionFunc
m*: Float
m_inv*: Float
i*: Float
i_inv*: Float
cog*: Vect
p*: Vect
v*: Vect
f*: Vect
a*: Float
w*: Float
t*: Float
transform*: Transform
userData*: DataPointer
v_bias*: Vect
w_bias*: Float
space*: Space
shapeList*: Shape
arbiterList*: Arbiter
constraintList*: Constraint
sleeping*: BodyInnerStruct
Body* = ptr BodyObj
SplittingPlane* = object
v0*: Vect
n*: Vect
ShapeCacheDataImpl* = proc (shape: Shape; transform: Transform): BB {.cdecl.}
ShapeDestroyImpl* = proc (shape: Shape) {.cdecl.}
ShapePointQueryImpl* = proc (shape: Shape; p: Vect; info: PointQueryInfo) {.cdecl.}
ShapeSegmentQueryImpl* = proc (shape: Shape; a: Vect; b: Vect; radius: Float; info: SegmentQueryInfo) {.cdecl.}
ShapeMassInfo* {.bycopy, pure.} = object
m*: Float
i*: Float
cog*: Vect
area*: Float
ShapeType* {.bycopy, size: sizeof(cint).} = enum
CP_CIRCLE_SHAPE,
CP_SEGMENT_SHAPE,
CP_POLY_SHAPE,
CP_NUM_SHAPES
ShapeClassObj* {.bycopy, pure.} = object
`type`*: ShapeType
cacheData*: ShapeCacheDataImpl
destroy*: ShapeDestroyImpl
pointQuery*: ShapePointQueryImpl
segmentQuery*: ShapeSegmentQueryImpl
ShapeClass* = ptr ShapeClassObj
ShapeObj {.bycopy, inheritable, pure.} = object
klass*: ShapeClass
space*: Space
body: Body # Already in the public API
massInfo*: ShapeMassInfo
bb*: BB
sensor*: bool
e*: Float
u*: Float
surfaceV*: Vect
userData*: DataPointer
`type`*: CollisionType
filter*: ShapeFilter
next*: Shape
prev*: Shape
hashid*: HashValue
Shape* = ptr ShapeObj
CircleShapeObj* {.bycopy, pure.} = object of Shape
c*: Vect
tc*: Vect
r*: Float
CircleShape* = ptr CircleShapeObj
SegmentShapeObj* {.bycopy, pure.} = object of Shape
a*: Vect
b*: Vect
n*: Vect
ta*: Vect
tb*: Vect
tn*: Vect
r*: Float
a_tangent*: Vect
b_tangent*: Vect
SegmentShape* = ptr SegmentShapeObj
PolyShapeObj* {.bycopy, pure.} = object of Shape
r*: Float
count*: cint
planes*: ptr SplittingPlane
internal_planes*: array[2 * CP_POLY_SHAPE_INLINE_ALLOC, SplittingPlane]
PolyShape* = ptr PolyShapeObj
ConstraintPreStepImpl* = proc (constraint: Constraint; dt: Float) {.cdecl.}
ConstraintApplyCachedImpulseImpl* = proc (constraint: Constraint; dt_coef: Float) {.cdecl.}
ConstraintApplyImpulseImpl* = proc (constraint: Constraint; dt: Float) {.cdecl.}
ConstraintGetImpulseImpl* = proc (constraint: Constraint): Float {.cdecl.}
ConstraintClass* = object
preStep*: ConstraintPreStepImpl
applyCachedImpulse*: ConstraintApplyCachedImpulseImpl
applyImpulse*: ConstraintApplyImpulseImpl
getImpulse*: ConstraintGetImpulseImpl
ConstraintObj {.bycopy, inheritable, pure.} = object
klass*: ConstraintClass
space*: Space
a*: Body
b*: Body
next_a*: Constraint
next_b*: Constraint
maxForce: Float # Already in the public API
errorBias: Float # Already in the public API
maxBias: Float # Already in the public API
collideBodies: bool
preSolve*: ConstraintPreSolveFunc
postSolve*: ConstraintPostSolveFunc
userData*: DataPointer
Constraint* = ptr ConstraintObj
PinJoint* = ptr object of Constraint
SlideJoint* = ptr object of Constraint
PivotJoint* = ptr object of Constraint
GrooveJoint* = ptr object of Constraint
DampedSpring* = ptr object of Constraint
DampedRotarySpring* = ptr object of Constraint
RotaryLimitJoint* = ptr object of Constraint
RatchetJoint* = ptr object of Constraint
GearJoint* = ptr object of Constraint
SimpleMotorJoint* = ptr object
ArbiterThread* {.bycopy, pure.} = object
next*: Arbiter
prev*: Arbiter
ArbiterState* = enum
## Arbiter is active and its the first collision.
CP_ARBITER_STATE_FIRST_COLLISION,
## Arbiter is active and its not the first collision.
CP_ARBITER_STATE_NORMAL,
## Collision has been explicitly ignored.
## Either by returning false from a begin collision handler or calling cpArbiterIgnore().
CP_ARBITER_STATE_IGNORE,
## Collison is no longer active. A space will cache an arbiter for up to cpSpace.collisionPersistence more steps.
CP_ARBITER_STATE_CACHED,
## Collison arbiter is invalid because one of the shapes was removed.
CP_ARBITER_STATE_INVALIDATED
ArbiterObj* {.bycopy, pure.} = object
e*: Float
u*: Float
surface_vr*: Vect
data*: DataPointer
a*: Shape
b*: Shape
body_a*: Body
body_b*: Body
thread_a*: ArbiterThread
thread_b*: ArbiterThread
count*: cint
contacts*: Contact
n*: Vect
handler*: CollisionHandler
handlerA*: CollisionHandler
handlerB*: CollisionHandler
swapped*: bool
stamp*: Timestamp
state*: ArbiterState
Arbiter* = ptr ArbiterObj
Contact* {.bycopy, pure.} = object
r1*: Vect
r2*: Vect
nMass*: Float
tMass*: Float
bounce*: Float
jnAcc*: Float
jtAcc*: Float
jBias*: Float
bias*: Float
hash*: HashValue
ContactBufferHeaderObj* {.bycopy, pure.} = object
stamp*: Timestamp
next*: ContactBufferHeader
numContacts*: cuint
ContactBufferHeader* = ptr ContactBufferHeaderObj
CollisionInfo* {.bycopy, pure.} = object
a*: Shape
b*: Shape
id*: CollisionID
n*: Vect
count*: cint
arr*: Contact
SpaceObj* {.bycopy, pure.} = object
iterations: cint
gravity: Vect
damping: Float
idleSpeedThreshold: Float
sleepTimeThreshold: Float
collisionSlop: Float
collisionBias: Float
collisionPersistence: Timestamp
userData: DataPointer
stamp*: Timestamp
curr_dt*: Float
dynamicBodies*: Array
staticBodies*: Array
rousedBodies*: Array
sleepingComponents*: Array
shapeIDCounter*: HashValue
staticShapes*: SpatialIndex
dynamicShapes*: SpatialIndex
constraints*: Array
arbiters*: Array
contactBuffersHead*: ContactBufferHeader
cachedArbiters*: HashSet
pooledArbiters*: Array
allocatedBuffers*: Array
locked*: cuint
usesWildcards*: bool
collisionHandlers*: HashSet
defaultHandler*: CollisionHandler
skipPostStep*: bool
postStepCallbacks*: Array
Space* = ptr SpaceObj
BB* {.bycopy, pure.} = object
## Chipmunk's axis-aligned 2D bounding box type. (left, bottom, right, top)
l*: Float
b*: Float
r*: Float
t*: Float
SpatialIndexBBFunc* = proc (obj: pointer): BB {.cdecl.}
## Spatial index bounding box callback function type.
## The spatial index calls this function and passes you a pointer to an object you added
## when it needs to get the bounding box associated with that object.
SpatialIndexIteratorFunc* = proc (obj: pointer; data: pointer) {.cdecl.}
## Spatial index/object iterator callback function type.
SpatialIndexQueryFunc* = proc (obj1: pointer; obj2: pointer; id: CollisionID; data: pointer): CollisionID {.cdecl.}
## Spatial query callback function type.
SpatialIndexSegmentQueryFunc* = proc (obj1: pointer; obj2: pointer; data: pointer): Float {.cdecl.}
## Spatial segment query callback function type.
SpatialIndexObj {.inheritable, bycopy, pure.} = object
klass*: ptr SpatialIndexClass
bbfunc*: SpatialIndexBBFunc
staticIndex*: SpatialIndex
dynamicIndex*: SpatialIndex
SpatialIndex* = ptr SpatialIndexObj
SpaceHash* = ptr object of SpatialIndex
BBTree* = ptr object of SpatialIndex
BBTreeVelocityFunc* = proc (obj: pointer): Vect {.cdecl.}
## Bounding box tree velocity callback function.
## This function should return an estimate for the object's velocity.
Sweep1D* = ptr object of SpatialIndex
SpatialIndexDestroyImpl* = proc (index: SpatialIndex) {.cdecl.}
SpatialIndexCountImpl* = proc (index: SpatialIndex): cint {.cdecl.}
SpatialIndexEachImpl* = proc (index: SpatialIndex; `func`: SpatialIndexIteratorFunc; data: pointer) {.cdecl.}
SpatialIndexContainsImpl* = proc (index: SpatialIndex; obj: pointer; hashid: HashValue): bool {.cdecl.}
SpatialIndexInsertImpl* = proc (index: SpatialIndex; obj: pointer; hashid: HashValue) {.cdecl.}
SpatialIndexRemoveImpl* = proc (index: SpatialIndex; obj: pointer; hashid: HashValue) {.cdecl.}
SpatialIndexReindexImpl* = proc (index: SpatialIndex) {.cdecl.}
SpatialIndexReindexObjectImpl* = proc (index: SpatialIndex; obj: pointer; hashid: HashValue) {.cdecl.}
SpatialIndexReindexQueryImpl* = proc (index: SpatialIndex; `func`: SpatialIndexQueryFunc; data: pointer) {.cdecl.}
SpatialIndexQueryImpl* = proc (index: SpatialIndex; obj: pointer; bb: BB; `func`: SpatialIndexQueryFunc; data: pointer) {.cdecl.}
SpatialIndexSegmentQueryImpl* = proc (index: SpatialIndex; obj: pointer; a: Vect; b: Vect; t_exit: Float; `func`: SpatialIndexSegmentQueryFunc; data: pointer) {.cdecl.}
SpatialIndexClass* {.bycopy, pure.} = object
destroy*: SpatialIndexDestroyImpl
count*: SpatialIndexCountImpl
each*: SpatialIndexEachImpl
contains*: SpatialIndexContainsImpl
insert*: SpatialIndexInsertImpl
remove*: SpatialIndexRemoveImpl
reindex*: SpatialIndexReindexImpl
reindexObject*: SpatialIndexReindexObjectImpl
reindexQuery*: SpatialIndexReindexQueryImpl
query*: SpatialIndexQueryImpl
segmentQuery*: SpatialIndexSegmentQueryImpl
ContactPoint* {.bycopy, pure.} = object
## Used in ContactPointSet
pointA*: Vect
pointB*: Vect
## The position of the contact on the surface of each shape.
distance*: Float
## Penetration distance of the two shapes. Overlapping means it will be negative.
## This value is calculated as cpvdot(cpvsub(point2, point1), normal) and is ignored by cpArbiterSetContactPointSet().
ContactPointSet* {.bycopy, pure.} = object
## A struct that wraps up the important collision data for an arbiter.
count*: cint
## The number of contact points in the set.
normal*: Vect
## The normal of the collision.
points*: array[2, ContactPoint]
## The array of contact points.
BodyType* {.size: sizeof(cint).} = enum
BODY_TYPE_DYNAMIC,
## A dynamic body is one that is affected by gravity, forces, and collisions.
## This is the default body type.
BODY_TYPE_KINEMATIC,
## A kinematic body is an infinite mass, user controlled body that is not affected by gravity, forces or collisions.
## Instead the body only moves based on it's velocity.
## Dynamic bodies collide normally with kinematic bodies, though the kinematic body will be unaffected.
## Collisions between two kinematic bodies, or a kinematic body and a static body produce collision callbacks, but no collision response.
BODY_TYPE_STATIC
## A static body is a body that never (or rarely) moves. If you move a static body, you must call one of the cpSpaceReindex*() functions.
## Chipmunk uses this information to optimize the collision detection.
## Static bodies do not produce collision callbacks when colliding with other static bodies.
BodyVelocityFunc* = proc (body: Body; gravity: Vect; damping: Float; dt: Float) {.cdecl.}
## Rigid body velocity update function type.
BodyPositionFunc* = proc (body: Body; dt: Float) {.cdecl.}
## Rigid body position update function type.
BodyShapeIteratorFunc* = proc (body: Body; shape: Shape; data: pointer) {.cdecl.}
## Call `func` once for each shape attached to `body` and added to the space.
BodyConstraintIteratorFunc* = proc (body: Body; constraint: Constraint; data: pointer) {.cdecl.}
## Body/constraint iterator callback function type.
BodyArbiterIteratorFunc* = proc (body: Body; arbiter: Arbiter; data: pointer) {.cdecl.}
## Body/arbiter iterator callback function type.
PointQueryInfo* {.bycopy, pure.} = object
## Point query info struct.
shape*: Shape
## The nearest shape, NULL if no shape was within range.
point*: Vect
## The closest point on the shape's surface. (in world space coordinates)
distance*: Float
## The distance to the point. The distance is negative if the point is inside the shape.
gradient*: Vect
## The gradient of the signed distance function.
## The value should be similar to info.p/info.d, but accurate even for very small values of info.d.
SegmentQueryInfo* {.bycopy, pure.} = object
## Segment query info struct.
shape*: Shape
## The shape that was hit, or NULL if no collision occured.
point*: Vect
## The point of impact.
normal*: Vect
## The normal of the surface hit.
alpha*: Float
## The normalized distance along the query segment in the range [0, 1].
ShapeFilter* {.bycopy, pure.} = object
## Fast collision filtering type that is used to determine if two objects collide before calling collision or query callbacks.
group*: Group
## Two objects with the same non-zero group value do not collide.
## This is generally used to group objects in a composite object together to disable self collisions.
categories*: Bitmask
## A bitmask of user definable categories that this object belongs to.
## The category/mask combinations of both objects in a collision must agree for a collision to occur.
mask*: Bitmask
## A bitmask of user definable category types that this object object collides with.
## The category/mask combinations of both objects in a collision must agree for a collision to occur.
ConstraintPreSolveFunc* = proc (constraint: Constraint; space: Space) {.cdecl.}
## Callback function type that gets called before solving a joint.
ConstraintPostSolveFunc* = proc (constraint: Constraint; space: Space) {.cdecl.}
## Callback function type that gets called after solving a joint.
DampedSpringForceFunc* = proc (spring: Constraint; dist: Float): Float {.cdecl.}
## Function type used for damped spring force callbacks.
DampedRotarySpringTorqueFunc* = proc (spring: Constraint; relativeAngle: Float): Float {.cdecl.}
## Function type used for damped rotary spring force callbacks.
SimpleMotor* = ptr object of Constraint
## Opaque struct type for damped rotary springs.
CollisionBeginFunc* = proc (arb: Arbiter; space: Space; userData: DataPointer): bool {.cdecl.}
## Collision begin event function callback type.
## Returning false from a begin callback causes the collision to be ignored until
## the the separate callback is called when the objects stop colliding.
CollisionPreSolveFunc* = proc (arb: Arbiter; space: Space; userData: DataPointer): bool {.cdecl.}
## Collision pre-solve event function callback type.
## Returning false from a pre-step callback causes the collision to be ignored until the next step.
CollisionPostSolveFunc* = proc (arb: Arbiter; space: Space; userData: DataPointer) {.cdecl.}
## Collision post-solve event function callback type.
CollisionSeparateFunc* = proc (arb: Arbiter; space: Space; userData: DataPointer) {.cdecl.}
## Collision separate event function callback type.
CollisionHandler* {.bycopy, pure.} = object
## Struct that holds function callback pointers to configure custom collision handling.
## Collision handlers have a pair of types; when a collision occurs between two shapes that have these types, the collision handler functions are triggered.
typeA*: CollisionType
## Collision type identifier of the first shape that this handler recognizes.
## In the collision handler callback, the shape with this type will be the first argument. Read only.
typeB*: CollisionType
## Collision type identifier of the second shape that this handler recognizes.
## In the collision handler callback, the shape with this type will be the second argument. Read only.
beginFunc*: CollisionBeginFunc
## This function is called when two shapes with types that match this collision handler begin colliding.
preSolveFunc*: CollisionPreSolveFunc
## This function is called each step when two shapes with types that match this collision handler are colliding.
## It's called before the collision solver runs so that you can affect a collision's outcome.
postSolveFunc*: CollisionPostSolveFunc
## This function is called each step when two shapes with types that match this collision handler are colliding.
## It's called after the collision solver runs so that you can read back information about the collision to trigger events in your game.
separateFunc*: CollisionSeparateFunc
## This function is called when two shapes with types that match this collision handler stop colliding.
userData*: DataPointer
## This is a user definable context pointer that is passed to all of the collision handler functions.
PostStepFunc* = proc (space: Space; key: pointer; data: pointer) {.cdecl.}
## Post Step callback function type.
SpacePointQueryFunc* = proc (shape: Shape; point: Vect; distance: Float; gradient: Vect; data: pointer) {.cdecl.}
## Nearest point query callback function type.
SpaceSegmentQueryFunc* = proc (shape: Shape; point: Vect; normal: Vect; alpha: Float; data: pointer) {.cdecl.}
## Segment query callback function type.
SpaceBBQueryFunc* = proc (shape: Shape; data: pointer) {.cdecl.}
## Rectangle Query callback function type.
SpaceShapeQueryFunc* = proc (shape: Shape; points: ptr ContactPointSet; data: pointer) {.cdecl.}
## Shape query callback function type.
SpaceBodyIteratorFunc* = proc (body: Body; data: pointer) {.cdecl.}
## Space/body iterator callback function type.
SpaceShapeIteratorFunc* = proc (shape: Shape; data: pointer) {.cdecl.}
## Space/body iterator callback function type.
SpaceConstraintIteratorFunc* = proc (constraint: Constraint; data: pointer) {.cdecl.}
## Space/constraint iterator callback function type.
SpaceDebugColor* {.bycopy, pure.} = object
## Color type to use with the space debug drawing API.
r*: cfloat
g*: cfloat
b*: cfloat
a*: cfloat
SpaceDebugDrawCircleImpl* = proc (pos: Vect; angle: Float; radius: Float; outlineColor: SpaceDebugColor; fillColor: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a filled, stroked circle.
SpaceDebugDrawSegmentImpl* = proc (a: Vect; b: Vect; color: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a line segment.
SpaceDebugDrawFatSegmentImpl* = proc (a: Vect; b: Vect; radius: Float; outlineColor: SpaceDebugColor; fillColor: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a thick line segment.
SpaceDebugDrawPolygonImpl* = proc (count: cint; verts: ptr Vect; radius: Float; outlineColor: SpaceDebugColor; fillColor: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a convex polygon.
SpaceDebugDrawDotImpl* = proc (size: Float; pos: Vect; color: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a dot.
SpaceDebugDrawColorForShapeImpl* = proc (shape: Shape; data: DataPointer): SpaceDebugColor {.cdecl.}
## Callback type for a function that returns a color for a given shape. This gives you an opportunity to color shapes based on how they are used in your engine.
SpaceDebugDrawFlags* {.size: sizeof(cint).} = enum
SPACE_DEBUG_DRAW_SHAPES = 0,
SPACE_DEBUG_DRAW_CONSTRAINTS = 1,
SPACE_DEBUG_DRAW_COLLISION_POINTS = 2
SpaceDebugDrawOptions* {.bycopy, pure.} = object
## Struct used with cpSpaceDebugDraw() containing drawing callbacks and other drawing settings.
drawCircle*: SpaceDebugDrawCircleImpl
## Function that will be invoked to draw circles.
drawSegment*: SpaceDebugDrawSegmentImpl
## Function that will be invoked to draw line segments.
drawFatSegment*: SpaceDebugDrawFatSegmentImpl
## Function that will be invoked to draw thick line segments.
drawPolygon*: SpaceDebugDrawPolygonImpl
## Function that will be invoked to draw convex polygons.
drawDot*: SpaceDebugDrawDotImpl
## Function that will be invoked to draw dots.
flags*: set[SpaceDebugDrawFlags]
## Flags that request which things to draw (collision shapes, constraints, contact points).
shapeOutlineColor*: SpaceDebugColor
## Outline color passed to the drawing function.
colorForShape*: SpaceDebugDrawColorForShapeImpl
## Function that decides what fill color to draw shapes using.
constraintColor*: SpaceDebugColor
## Color passed to drawing functions for constraints.
collisionPointColor*: SpaceDebugColor
## Color passed to drawing functions for collision points.
data*: DataPointer
## User defined context pointer passed to all of the callback functions as the 'data' argument.
else:
type
Float* = cdouble
## Chipmunk's floating point type.
HashValue* = pointer
## Hash value type.
CollisionID* = uint32
## Type used internally to cache colliding object info for cpCollideShapes().
## Should be at least 32 bits.
DataPointer* = pointer
## Type used for user data pointers.
CollisionType* = pointer
## Type used for cpSpace.collision_type.
Group* = pointer
## Type used for cpShape.group.
Bitmask* = cuint
## Type used for cpShapeFilter category and mask.
Timestamp* = cuint
## Type used for various timestamps in Chipmunk.
Vect* {.bycopy.} = object
## Chipmunk's 2D vector type.
x*: Float
y*: Float
Transform* {.bycopy.} = object
## Column major affine transform.
a*: Float
b*: Float
c*: Float
d*: Float
tx*: Float
ty*: Float
Mat2x2* {.bycopy.} = object
a*: Float
b*: Float
c*: Float
d*: Float
Array* = ptr object
HashSet* = ptr object
Body* = ptr object
ShapeObj {.inheritable.} = object
Shape* = ptr ShapeObj
CircleShape* = ptr object of Shape
SegmentShape* = ptr object of Shape
PolyShape* = ptr object of Shape
ConstraintObj {.inheritable, bycopy, pure.} = object
Constraint* = ptr ConstraintObj
PinJoint* = ptr object of Constraint
SlideJoint* = ptr object of Constraint
PivotJoint* = ptr object of Constraint
GrooveJoint* = ptr object of Constraint
DampedSpring* = ptr object of Constraint
DampedRotarySpring* = ptr object of Constraint
RotaryLimitJoint* = ptr object of Constraint
RatchetJoint* = ptr object of Constraint
GearJoint* = ptr object of Constraint
SimpleMotorJoint* = ptr object
Arbiter* = ptr object
Space* = ptr object
BB* {.bycopy.} = object
## Chipmunk's axis-aligned 2D bounding box type. (left, bottom, right, top)
l*: Float
b*: Float
r*: Float
t*: Float
SpatialIndexBBFunc* = proc (obj: pointer): BB {.cdecl.}
## Spatial index bounding box callback function type.
## The spatial index calls this function and passes you a pointer to an object you added
## when it needs to get the bounding box associated with that object.
SpatialIndexIteratorFunc* = proc (obj: pointer; data: pointer) {.cdecl.}
## Spatial index/object iterator callback function type.
SpatialIndexQueryFunc* = proc (obj1: pointer; obj2: pointer; id: CollisionID; data: pointer): CollisionID {.cdecl.}
## Spatial query callback function type.
SpatialIndexSegmentQueryFunc* = proc (obj1: pointer; obj2: pointer; data: pointer): Float {.cdecl.}
## Spatial segment query callback function type.
SpatialIndexObj {.inheritable.} = object
klass*: ptr SpatialIndexClass
bbfunc*: SpatialIndexBBFunc
staticIndex*: SpatialIndex
dynamicIndex*: SpatialIndex
SpatialIndex* = ptr SpatialIndexObj
SpaceHash* = ptr object of SpatialIndex
BBTree* = ptr object of SpatialIndex
BBTreeVelocityFunc* = proc (obj: pointer): Vect {.cdecl.}
## Bounding box tree velocity callback function.
## This function should return an estimate for the object's velocity.
Sweep1D* = ptr object of SpatialIndex
SpatialIndexDestroyImpl* = proc (index: SpatialIndex) {.cdecl.}
SpatialIndexCountImpl* = proc (index: SpatialIndex): cint {.cdecl.}
SpatialIndexEachImpl* = proc (index: SpatialIndex; `func`: SpatialIndexIteratorFunc; data: pointer) {.cdecl.}
SpatialIndexContainsImpl* = proc (index: SpatialIndex; obj: pointer; hashid: HashValue): bool {.cdecl.}
SpatialIndexInsertImpl* = proc (index: SpatialIndex; obj: pointer; hashid: HashValue) {.cdecl.}
SpatialIndexRemoveImpl* = proc (index: SpatialIndex; obj: pointer; hashid: HashValue) {.cdecl.}
SpatialIndexReindexImpl* = proc (index: SpatialIndex) {.cdecl.}
SpatialIndexReindexObjectImpl* = proc (index: SpatialIndex; obj: pointer; hashid: HashValue) {.cdecl.}
SpatialIndexReindexQueryImpl* = proc (index: SpatialIndex; `func`: SpatialIndexQueryFunc; data: pointer) {.cdecl.}
SpatialIndexQueryImpl* = proc (index: SpatialIndex; obj: pointer; bb: BB; `func`: SpatialIndexQueryFunc; data: pointer) {.cdecl.}
SpatialIndexSegmentQueryImpl* = proc (index: SpatialIndex; obj: pointer; a: Vect; b: Vect; t_exit: Float; `func`: SpatialIndexSegmentQueryFunc; data: pointer) {.cdecl.}
SpatialIndexClass* {.bycopy.} = object
destroy*: SpatialIndexDestroyImpl
count*: SpatialIndexCountImpl
each*: SpatialIndexEachImpl
contains*: SpatialIndexContainsImpl
insert*: SpatialIndexInsertImpl
remove*: SpatialIndexRemoveImpl
reindex*: SpatialIndexReindexImpl
reindexObject*: SpatialIndexReindexObjectImpl
reindexQuery*: SpatialIndexReindexQueryImpl
query*: SpatialIndexQueryImpl
segmentQuery*: SpatialIndexSegmentQueryImpl
ContactPoint* {.bycopy.} = object
## Used in ContactPointSet
pointA*: Vect
pointB*: Vect
## The position of the contact on the surface of each shape.
distance*: Float
## Penetration distance of the two shapes. Overlapping means it will be negative.
## This value is calculated as cpvdot(cpvsub(point2, point1), normal) and is ignored by cpArbiterSetContactPointSet().
ContactPointSet* {.bycopy.} = object
## A struct that wraps up the important collision data for an arbiter.
count*: cint
## The number of contact points in the set.
normal*: Vect
## The normal of the collision.
points*: array[2, ContactPoint]
## The array of contact points.
BodyType* {.size: sizeof(cint).} = enum
BODY_TYPE_DYNAMIC,
## A dynamic body is one that is affected by gravity, forces, and collisions.
## This is the default body type.
BODY_TYPE_KINEMATIC,
## A kinematic body is an infinite mass, user controlled body that is not affected by gravity, forces or collisions.
## Instead the body only moves based on it's velocity.
## Dynamic bodies collide normally with kinematic bodies, though the kinematic body will be unaffected.
## Collisions between two kinematic bodies, or a kinematic body and a static body produce collision callbacks, but no collision response.
BODY_TYPE_STATIC
## A static body is a body that never (or rarely) moves. If you move a static body, you must call one of the cpSpaceReindex*() functions.
## Chipmunk uses this information to optimize the collision detection.
## Static bodies do not produce collision callbacks when colliding with other static bodies.
BodyVelocityFunc* = proc (body: Body; gravity: Vect; damping: Float; dt: Float) {.cdecl.}
## Rigid body velocity update function type.
BodyPositionFunc* = proc (body: Body; dt: Float) {.cdecl.}
## Rigid body position update function type.
BodyShapeIteratorFunc* = proc (body: Body; shape: Shape; data: pointer) {.cdecl.}
## Call `func` once for each shape attached to `body` and added to the space.
BodyConstraintIteratorFunc* = proc (body: Body; constraint: Constraint; data: pointer) {.cdecl.}
## Body/constraint iterator callback function type.
BodyArbiterIteratorFunc* = proc (body: Body; arbiter: Arbiter; data: pointer) {.cdecl.}
## Body/arbiter iterator callback function type.
PointQueryInfo* {.bycopy.} = object
## Point query info struct.
shape*: Shape
## The nearest shape, NULL if no shape was within range.
point*: Vect
## The closest point on the shape's surface. (in world space coordinates)
distance*: Float
## The distance to the point. The distance is negative if the point is inside the shape.
gradient*: Vect
## The gradient of the signed distance function.
## The value should be similar to info.p/info.d, but accurate even for very small values of info.d.
SegmentQueryInfo* {.bycopy.} = object
## Segment query info struct.
shape*: Shape
## The shape that was hit, or NULL if no collision occured.
point*: Vect
## The point of impact.
normal*: Vect
## The normal of the surface hit.
alpha*: Float
## The normalized distance along the query segment in the range [0, 1].
ShapeFilter* {.bycopy.} = object
## Fast collision filtering type that is used to determine if two objects collide before calling collision or query callbacks.
group*: Group
## Two objects with the same non-zero group value do not collide.
## This is generally used to group objects in a composite object together to disable self collisions.
categories*: Bitmask
## A bitmask of user definable categories that this object belongs to.
## The category/mask combinations of both objects in a collision must agree for a collision to occur.
mask*: Bitmask
## A bitmask of user definable category types that this object object collides with.
## The category/mask combinations of both objects in a collision must agree for a collision to occur.
ConstraintPreSolveFunc* = proc (constraint: Constraint; space: Space) {.cdecl.}
## Callback function type that gets called before solving a joint.
ConstraintPostSolveFunc* = proc (constraint: Constraint; space: Space) {.cdecl.}
## Callback function type that gets called after solving a joint.
DampedSpringForceFunc* = proc (spring: Constraint; dist: Float): Float {.cdecl.}
## Function type used for damped spring force callbacks.
DampedRotarySpringTorqueFunc* = proc (spring: Constraint; relativeAngle: Float): Float {.cdecl.}
## Function type used for damped rotary spring force callbacks.
SimpleMotor* = ptr object of Constraint
## Opaque struct type for damped rotary springs.
CollisionBeginFunc* = proc (arb: Arbiter; space: Space; userData: DataPointer): bool {.cdecl.}
## Collision begin event function callback type.
## Returning false from a begin callback causes the collision to be ignored until
## the the separate callback is called when the objects stop colliding.
CollisionPreSolveFunc* = proc (arb: Arbiter; space: Space; userData: DataPointer): bool {.cdecl.}
## Collision pre-solve event function callback type.
## Returning false from a pre-step callback causes the collision to be ignored until the next step.
CollisionPostSolveFunc* = proc (arb: Arbiter; space: Space; userData: DataPointer) {.cdecl.}
## Collision post-solve event function callback type.
CollisionSeparateFunc* = proc (arb: Arbiter; space: Space; userData: DataPointer) {.cdecl.}
## Collision separate event function callback type.
CollisionHandler* {.bycopy.} = object
## Struct that holds function callback pointers to configure custom collision handling.
## Collision handlers have a pair of types; when a collision occurs between two shapes that have these types, the collision handler functions are triggered.
typeA*: CollisionType
## Collision type identifier of the first shape that this handler recognizes.
## In the collision handler callback, the shape with this type will be the first argument. Read only.
typeB*: CollisionType
## Collision type identifier of the second shape that this handler recognizes.
## In the collision handler callback, the shape with this type will be the second argument. Read only.
beginFunc*: CollisionBeginFunc
## This function is called when two shapes with types that match this collision handler begin colliding.
preSolveFunc*: CollisionPreSolveFunc
## This function is called each step when two shapes with types that match this collision handler are colliding.
## It's called before the collision solver runs so that you can affect a collision's outcome.
postSolveFunc*: CollisionPostSolveFunc
## This function is called each step when two shapes with types that match this collision handler are colliding.
## It's called after the collision solver runs so that you can read back information about the collision to trigger events in your game.
separateFunc*: CollisionSeparateFunc
## This function is called when two shapes with types that match this collision handler stop colliding.
userData*: DataPointer
## This is a user definable context pointer that is passed to all of the collision handler functions.
PostStepFunc* = proc (space: Space; key: pointer; data: pointer) {.cdecl.}
## Post Step callback function type.
SpacePointQueryFunc* = proc (shape: Shape; point: Vect; distance: Float; gradient: Vect; data: pointer) {.cdecl.}
## Nearest point query callback function type.
SpaceSegmentQueryFunc* = proc (shape: Shape; point: Vect; normal: Vect; alpha: Float; data: pointer) {.cdecl.}
## Segment query callback function type.
SpaceBBQueryFunc* = proc (shape: Shape; data: pointer) {.cdecl.}
## Rectangle Query callback function type.
SpaceShapeQueryFunc* = proc (shape: Shape; points: ptr ContactPointSet; data: pointer) {.cdecl.}
## Shape query callback function type.
SpaceBodyIteratorFunc* = proc (body: Body; data: pointer) {.cdecl.}
## Space/body iterator callback function type.
SpaceShapeIteratorFunc* = proc (shape: Shape; data: pointer) {.cdecl.}
## Space/body iterator callback function type.
SpaceConstraintIteratorFunc* = proc (constraint: Constraint; data: pointer) {.cdecl.}
## Space/constraint iterator callback function type.
SpaceDebugColor* {.bycopy.} = object
## Color type to use with the space debug drawing API.
r*: cfloat
g*: cfloat
b*: cfloat
a*: cfloat
SpaceDebugDrawCircleImpl* = proc (pos: Vect; angle: Float; radius: Float; outlineColor: SpaceDebugColor; fillColor: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a filled, stroked circle.
SpaceDebugDrawSegmentImpl* = proc (a: Vect; b: Vect; color: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a line segment.
SpaceDebugDrawFatSegmentImpl* = proc (a: Vect; b: Vect; radius: Float; outlineColor: SpaceDebugColor; fillColor: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a thick line segment.
SpaceDebugDrawPolygonImpl* = proc (count: cint; verts: ptr Vect; radius: Float; outlineColor: SpaceDebugColor; fillColor: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a convex polygon.
SpaceDebugDrawDotImpl* = proc (size: Float; pos: Vect; color: SpaceDebugColor; data: DataPointer) {.cdecl.}
## Callback type for a function that draws a dot.
SpaceDebugDrawColorForShapeImpl* = proc (shape: Shape; data: DataPointer): SpaceDebugColor {.cdecl.}
## Callback type for a function that returns a color for a given shape. This gives you an opportunity to color shapes based on how they are used in your engine.
SpaceDebugDrawFlags* {.size: sizeof(cint).} = enum
SPACE_DEBUG_DRAW_SHAPES = 0,
SPACE_DEBUG_DRAW_CONSTRAINTS = 1,
SPACE_DEBUG_DRAW_COLLISION_POINTS = 2
SpaceDebugDrawOptions* {.bycopy.} = object
## Struct used with cpSpaceDebugDraw() containing drawing callbacks and other drawing settings.
drawCircle*: SpaceDebugDrawCircleImpl
## Function that will be invoked to draw circles.
drawSegment*: SpaceDebugDrawSegmentImpl
## Function that will be invoked to draw line segments.
drawFatSegment*: SpaceDebugDrawFatSegmentImpl
## Function that will be invoked to draw thick line segments.
drawPolygon*: SpaceDebugDrawPolygonImpl
## Function that will be invoked to draw convex polygons.
drawDot*: SpaceDebugDrawDotImpl
## Function that will be invoked to draw dots.
flags*: set[SpaceDebugDrawFlags]
## Flags that request which things to draw (collision shapes, constraints, contact points).