-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathzbullet.zig
More file actions
1673 lines (1379 loc) · 54.7 KB
/
zbullet.zig
File metadata and controls
1673 lines (1379 loc) · 54.7 KB
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
// Zig bindings for Bullet Physics SDK
const std = @import("std");
const Mutex = std.Thread.Mutex;
const expect = std.testing.expect;
pub const World = *align(@sizeOf(usize)) WorldImpl;
pub const Shape = *align(@sizeOf(usize)) ShapeImpl;
pub const BoxShape = *align(@sizeOf(usize)) BoxShapeImpl;
pub const SphereShape = *align(@sizeOf(usize)) SphereShapeImpl;
pub const CapsuleShape = *align(@sizeOf(usize)) CapsuleShapeImpl;
pub const CylinderShape = *align(@sizeOf(usize)) CylinderShapeImpl;
pub const ConvexHullShape = *align(@sizeOf(usize)) ConvexHullShapeImpl;
pub const CompoundShape = *align(@sizeOf(usize)) CompoundShapeImpl;
pub const TriangleMeshShape = *align(@sizeOf(usize)) TriangleMeshShapeImpl;
pub const Body = *align(@sizeOf(usize)) BodyImpl;
pub const Constraint = *align(@sizeOf(usize)) ConstraintImpl;
pub const Point2PointConstraint = *align(@sizeOf(usize)) Point2PointConstraintImpl;
pub const AllocFn = *const fn (size: usize, alignment: i32) callconv(.c) ?*anyopaque;
pub const FreeFn = *const fn (ptr: ?*anyopaque) callconv(.c) void;
extern fn cbtAlignedAllocSetCustomAligned(
alloc: ?AllocFn,
free: ?FreeFn,
) void;
const SizeAndAlignment = packed struct(u64) {
size: u48,
alignment: u16,
};
var mem_allocator: ?std.mem.Allocator = null;
var mem_allocations: ?std.AutoHashMap(usize, SizeAndAlignment) = null;
var mem_mutex: std.Thread.Mutex = .{};
export fn zbulletAlloc(size: usize, alignment: i32) callconv(.c) ?*anyopaque {
mem_mutex.lock();
defer mem_mutex.unlock();
const ptr = mem_allocator.?.rawAlloc(
size,
std.mem.Alignment.fromByteUnits(@intCast(alignment)),
@returnAddress(),
);
if (ptr == null) @panic("zbullet: out of memory");
mem_allocations.?.put(
@intFromPtr(ptr),
.{ .size = @as(u32, @intCast(size)), .alignment = @as(u16, @intCast(alignment)) },
) catch @panic("zbullet: out of memory");
return ptr;
}
export fn zbulletFree(maybe_ptr: ?*anyopaque) callconv(.c) void {
if (maybe_ptr) |ptr| {
mem_mutex.lock();
defer mem_mutex.unlock();
const info = mem_allocations.?.fetchRemove(@intFromPtr(ptr)).?.value;
const mem = @as([*]u8, @ptrCast(ptr))[0..info.size];
mem_allocator.?.rawFree(
mem,
std.mem.Alignment.fromByteUnits(@intCast(info.alignment)),
@returnAddress(),
);
}
}
extern fn cbtTaskSchedInit() void;
extern fn cbtTaskSchedDeinit() void;
pub fn init(alloc: std.mem.Allocator) void {
std.debug.assert(mem_allocator == null and mem_allocations == null);
mem_allocator = alloc;
mem_allocations = std.AutoHashMap(usize, SizeAndAlignment).init(mem_allocator.?);
mem_allocations.?.ensureTotalCapacity(256) catch @panic("zbullet: out of memory");
cbtAlignedAllocSetCustomAligned(zbulletAlloc, zbulletFree);
cbtTaskSchedInit();
_ = ConstraintImpl.getFixedBody(); // This will allocate 'fixed body' singleton on the heap.
}
pub fn deinit() void {
ConstraintImpl.destroyFixedBody();
cbtTaskSchedDeinit();
cbtAlignedAllocSetCustomAligned(null, null);
mem_allocations.?.deinit();
mem_allocations = null;
mem_allocator = null;
}
pub const CollisionFilter = packed struct {
default: bool = false,
static: bool = false,
kinematic: bool = false,
debris: bool = false,
sensor_trigger: bool = false,
character: bool = false,
_pad0: u10 = 0,
_pad1: u16 = 0,
pub const all = @as(CollisionFilter, @bitCast(~@as(u32, 0)));
comptime {
std.debug.assert(@sizeOf(@This()) == @sizeOf(u32) and @bitSizeOf(@This()) == @bitSizeOf(u32));
}
};
pub const RayCastFlags = packed struct {
trimesh_skip_backfaces: bool = false,
trimesh_keep_unflipped_normals: bool = false,
use_subsimplex_convex_test: bool = false, // used by default, faster but less accurate
use_gjk_convex_test: bool = false,
_pad0: u12 = 0,
_pad1: u16 = 0,
comptime {
std.debug.assert(@sizeOf(@This()) == @sizeOf(u32) and @bitSizeOf(@This()) == @bitSizeOf(u32));
}
};
pub const RayCastResult = extern struct {
hit_normal_world: [3]f32,
hit_point_world: [3]f32,
hit_fraction: f32,
body: ?Body,
};
pub fn initWorld() World {
return WorldImpl.init();
}
const WorldImpl = opaque {
fn init() World {
std.debug.assert(mem_allocator != null and mem_allocations != null);
return cbtWorldCreate();
}
extern fn cbtWorldCreate() World;
pub fn deinit(world: World) void {
std.debug.assert(world.getNumBodies() == 0);
std.debug.assert(world.getNumConstraints() == 0);
cbtWorldDestroy(world);
}
extern fn cbtWorldDestroy(world: World) void;
pub const setGravity = cbtWorldSetGravity;
extern fn cbtWorldSetGravity(world: World, gravity: *const [3]f32) void;
pub const getGravity = cbtWorldGetGravity;
extern fn cbtWorldGetGravity(world: World, gravity: *[3]f32) void;
pub fn stepSimulation(world: World, time_step: f32, args: struct {
max_sub_steps: u32 = 1,
fixed_time_step: f32 = 1.0 / 60.0,
}) u32 {
return cbtWorldStepSimulation(
world,
time_step,
args.max_sub_steps,
args.fixed_time_step,
);
}
extern fn cbtWorldStepSimulation(
world: World,
time_step: f32,
max_sub_steps: u32,
fixed_time_step: f32,
) u32;
pub const addBody = cbtWorldAddBody;
extern fn cbtWorldAddBody(world: World, body: Body) void;
pub const removeBody = cbtWorldRemoveBody;
extern fn cbtWorldRemoveBody(world: World, body: Body) void;
pub const getBody = cbtWorldGetBody;
extern fn cbtWorldGetBody(world: World, index: i32) Body;
pub const getNumBodies = cbtWorldGetNumBodies;
extern fn cbtWorldGetNumBodies(world: World) i32;
pub const addConstraint = cbtWorldAddConstraint;
extern fn cbtWorldAddConstraint(
world: World,
con: Constraint,
disable_collision_between_linked_bodies: bool,
) void;
pub const removeConstraint = cbtWorldRemoveConstraint;
extern fn cbtWorldRemoveConstraint(world: World, con: Constraint) void;
pub const getConstraint = cbtWorldGetConstraint;
extern fn cbtWorldGetConstraint(world: World, index: i32) Constraint;
pub const getNumConstraints = cbtWorldGetNumConstraints;
extern fn cbtWorldGetNumConstraints(world: World) i32;
pub const debugSetDrawer = cbtWorldDebugSetDrawer;
extern fn cbtWorldDebugSetDrawer(world: World, debug: *const DebugDraw) void;
pub fn debugSetMode(world: World, mode: DebugMode) void {
cbtWorldDebugSetMode(world, @as(c_int, @bitCast(mode)));
}
extern fn cbtWorldDebugSetMode(world: World, mode: c_int) void;
pub fn debugGetMode(world: World) DebugMode {
return @as(DebugMode, @bitCast(cbtWorldDebugGetMode(world)));
}
extern fn cbtWorldDebugGetMode(world: World) c_int;
pub const debugDrawAll = cbtWorldDebugDrawAll;
extern fn cbtWorldDebugDrawAll(world: World) void;
pub const debugDrawLine1 = cbtWorldDebugDrawLine1;
extern fn cbtWorldDebugDrawLine1(
world: World,
p0: *const [3]f32,
p1: *const [3]f32,
color: *const [3]f32,
) void;
pub const debugDrawLine2 = cbtWorldDebugDrawLine2;
extern fn cbtWorldDebugDrawLine2(
world: World,
p0: *const [3]f32,
p1: *const [3]f32,
color0: *const [3]f32,
color1: *const [3]f32,
) void;
pub const debugDrawSphere = cbtWorldDebugDrawSphere;
extern fn cbtWorldDebugDrawSphere(
world: World,
position: *const [3]f32,
radius: f32,
color: *const [3]f32,
) void;
pub fn rayTestClosest(
world: World,
ray_from_world: *const [3]f32,
ray_to_world: *const [3]f32,
group: CollisionFilter,
mask: CollisionFilter,
flags: RayCastFlags,
raycast_result: ?*RayCastResult,
) bool {
return cbtWorldRayTestClosest(
world,
ray_from_world,
ray_to_world,
@as(c_int, @bitCast(group)),
@as(c_int, @bitCast(mask)),
@as(c_int, @bitCast(flags)),
raycast_result,
);
}
extern fn cbtWorldRayTestClosest(
world: World,
ray_from_world: *const [3]f32,
ray_to_world: *const [3]f32,
group: c_int,
mask: c_int,
flags: c_int,
raycast_result: ?*RayCastResult,
) bool;
};
pub const Axis = enum(c_int) {
x = 0,
y = 1,
z = 2,
};
pub const ShapeType = enum(c_int) {
box = 0,
sphere = 8,
capsule = 10,
cylinder = 13,
compound = 31,
trimesh = 21,
convex_hull = 4,
};
const ShapeImpl = opaque {
pub const alloc = cbtShapeAllocate;
extern fn cbtShapeAllocate(stype: ShapeType) Shape;
pub const dealloc = cbtShapeDeallocate;
extern fn cbtShapeDeallocate(shape: Shape) void;
pub fn deinit(shape: Shape) void {
shape.destroy();
shape.dealloc();
}
pub fn destroy(shape: Shape) void {
switch (shape.getType()) {
.box, .sphere, .capsule, .cylinder, .compound, .convex_hull => cbtShapeDestroy(shape),
.trimesh => cbtShapeTriMeshDestroy(shape),
}
}
extern fn cbtShapeDestroy(shape: Shape) void;
extern fn cbtShapeTriMeshDestroy(shape: Shape) void;
pub const isCreated = cbtShapeIsCreated;
extern fn cbtShapeIsCreated(shape: Shape) bool;
pub const getType = cbtShapeGetType;
extern fn cbtShapeGetType(shape: Shape) ShapeType;
pub const setMargin = cbtShapeSetMargin;
extern fn cbtShapeSetMargin(shape: Shape, margin: f32) void;
pub const getMargin = cbtShapeGetMargin;
extern fn cbtShapeGetMargin(shape: Shape) f32;
pub const isPolyhedral = cbtShapeIsPolyhedral;
extern fn cbtShapeIsPolyhedral(shape: Shape) bool;
pub const isConvex2d = cbtShapeIsConvex2d;
extern fn cbtShapeIsConvex2d(shape: Shape) bool;
pub const isConvex = cbtShapeIsConvex;
extern fn cbtShapeIsConvex(shape: Shape) bool;
pub const isNonMoving = cbtShapeIsNonMoving;
extern fn cbtShapeIsNonMoving(shape: Shape) bool;
pub const isConcave = cbtShapeIsConcave;
extern fn cbtShapeIsConcave(shape: Shape) bool;
pub const isCompound = cbtShapeIsCompound;
extern fn cbtShapeIsCompound(shape: Shape) bool;
pub const calculateLocalInertia = cbtShapeCalculateLocalInertia;
extern fn cbtShapeCalculateLocalInertia(
shape: Shape,
mass: f32,
inertia: *[3]f32,
) void;
pub const setUserPointer = cbtShapeSetUserPointer;
extern fn cbtShapeSetUserPointer(shape: Shape, ptr: ?*anyopaque) void;
pub const getUserPointer = cbtShapeGetUserPointer;
extern fn cbtShapeGetUserPointer(shape: Shape) ?*anyopaque;
pub const setUserIndex = cbtShapeSetUserIndex;
extern fn cbtShapeSetUserIndex(shape: Shape, slot: u32, index: i32) void;
pub const getUserIndex = cbtShapeGetUserIndex;
extern fn cbtShapeGetUserIndex(shape: Shape, slot: u32) i32;
pub fn as(shape: Shape, comptime stype: ShapeType) switch (stype) {
.box => BoxShape,
.sphere => SphereShape,
.cylinder => CylinderShape,
.capsule => CapsuleShape,
.compound => CompoundShape,
.trimesh => TriangleMeshShape,
.convex_hull => ConvexHullShape,
} {
std.debug.assert(shape.getType() == stype);
return switch (stype) {
.box => @as(BoxShape, @ptrCast(shape)),
.sphere => @as(SphereShape, @ptrCast(shape)),
.cylinder => @as(CylinderShape, @ptrCast(shape)),
.capsule => @as(CapsuleShape, @ptrCast(shape)),
.compound => @as(CompoundShape, @ptrCast(shape)),
.trimesh => @as(TriangleMeshShape, @ptrCast(shape)),
.convex_hull => @as(ConvexHullShape, @ptrCast(shape)),
};
}
};
fn ShapeInterface(comptime T: type) type {
return struct {
pub fn asShape(shape: T) Shape {
return @as(Shape, @ptrCast(shape));
}
pub fn dealloc(shape: T) void {
shape.asShape().dealloc();
}
pub fn destroy(shape: T) void {
shape.asShape().destroy();
}
pub fn deinit(shape: T) void {
shape.asShape().deinit();
}
pub fn isCreated(shape: T) bool {
return shape.asShape().isCreated();
}
pub fn getType(shape: T) ShapeType {
return shape.asShape().getType();
}
pub fn setMargin(shape: T, margin: f32) void {
shape.asShape().setMargin(margin);
}
pub fn getMargin(shape: T) f32 {
return shape.asShape().getMargin();
}
pub fn isPolyhedral(shape: T) bool {
return shape.asShape().isPolyhedral();
}
pub fn isConvex2d(shape: T) bool {
return shape.asShape().isConvex2d();
}
pub fn isConvex(shape: T) bool {
return shape.asShape().isConvex();
}
pub fn isNonMoving(shape: T) bool {
return shape.asShape().isNonMoving();
}
pub fn isConcave(shape: T) bool {
return shape.asShape().isConcave();
}
pub fn isCompound(shape: T) bool {
return shape.asShape().isCompound();
}
pub fn calculateLocalInertia(shape: T, mass: f32, inertia: *[3]f32) void {
shape.asShape().calculateLocalInertia(mass, inertia);
}
pub fn setUserPointer(shape: T, ptr: ?*anyopaque) void {
shape.asShape().setUserPointer(ptr);
}
pub fn getUserPointer(shape: T) ?*anyopaque {
return shape.asShape().getUserPointer();
}
pub fn setUserIndex(shape: T, slot: u32, index: i32) void {
shape.asShape().setUserIndex(slot, index);
}
pub fn getUserIndex(shape: T, slot: u32) i32 {
return shape.asShape().getUserIndex(slot);
}
};
}
fn ConstraintInterface(comptime T: type) type {
return struct {
pub fn asConstraint(con: T) Constraint {
return @as(Constraint, @ptrCast(con));
}
pub fn dealloc(con: T) void {
con.asConstraint().dealloc();
}
pub fn destroy(con: T) void {
con.asConstraint().destroy();
}
pub fn getType(con: T) ConstraintType {
return con.asConstraint().getType();
}
pub fn isCreated(con: T) bool {
return con.asConstraint().isCreated();
}
pub fn setEnabled(con: T, enabled: bool) void {
con.asConstraint().setEnabled(enabled);
}
pub fn isEnabled(con: T) bool {
return con.asConstraint().isEnabled();
}
pub fn getBodyA(con: T) Body {
return con.asConstraint().getBodyA();
}
pub fn getBodyB(con: T) Body {
return con.asConstraint().getBodyB();
}
pub fn setDebugDrawSize(con: T, size: f32) void {
con.asConstraint().setDebugDrawSize(size);
}
};
}
pub fn initBoxShape(half_extents: *const [3]f32) BoxShape {
const box = BoxShapeImpl.alloc();
box.create(half_extents);
return box;
}
const BoxShapeImpl = opaque {
const Interface = ShapeInterface(BoxShape);
pub const asShape = Interface.asShape;
pub const dealloc = Interface.dealloc;
pub const destroy = Interface.destroy;
pub const deinit = Interface.deinit;
pub const isCreated = Interface.isCreated;
pub const getType = Interface.getType;
pub const setMargin = Interface.setMargin;
pub const getMargin = Interface.getMargin;
pub const isPolyhedral = Interface.isPolyhedral;
pub const isConvex2d = Interface.isConvex2d;
pub const isConvex = Interface.isConvex;
pub const isNonMoving = Interface.isNonMoving;
pub const isConcave = Interface.isConcave;
pub const isCompound = Interface.isCompound;
pub const calculateLocalInertia = Interface.calculateLocalInertia;
pub const setUserPointer = Interface.setUserPointer;
pub const getUserPointer = Interface.getUserPointer;
pub const setUserIndex = Interface.setUserIndex;
pub const getUserIndex = Interface.getUserIndex;
fn alloc() BoxShape {
return @as(BoxShape, @ptrCast(ShapeImpl.alloc(.box)));
}
pub const create = cbtShapeBoxCreate;
extern fn cbtShapeBoxCreate(box: BoxShape, half_extents: *const [3]f32) void;
pub const getHalfExtentsWithoutMargin = cbtShapeBoxGetHalfExtentsWithoutMargin;
extern fn cbtShapeBoxGetHalfExtentsWithoutMargin(box: BoxShape, half_extents: *[3]f32) void;
pub const getHalfExtentsWithMargin = cbtShapeBoxGetHalfExtentsWithMargin;
extern fn cbtShapeBoxGetHalfExtentsWithMargin(box: BoxShape, half_extents: *[3]f32) void;
};
pub fn initSphereShape(radius: f32) SphereShape {
const sphere = SphereShapeImpl.alloc();
sphere.create(radius);
return sphere;
}
const SphereShapeImpl = opaque {
const Interface = ShapeInterface(SphereShape);
pub const asShape = Interface.asShape;
pub const dealloc = Interface.dealloc;
pub const destroy = Interface.destroy;
pub const deinit = Interface.deinit;
pub const isCreated = Interface.isCreated;
pub const getType = Interface.getType;
pub const setMargin = Interface.setMargin;
pub const getMargin = Interface.getMargin;
pub const isPolyhedral = Interface.isPolyhedral;
pub const isConvex2d = Interface.isConvex2d;
pub const isConvex = Interface.isConvex;
pub const isNonMoving = Interface.isNonMoving;
pub const isConcave = Interface.isConcave;
pub const isCompound = Interface.isCompound;
pub const calculateLocalInertia = Interface.calculateLocalInertia;
pub const setUserPointer = Interface.setUserPointer;
pub const getUserPointer = Interface.getUserPointer;
pub const setUserIndex = Interface.setUserIndex;
pub const getUserIndex = Interface.getUserIndex;
fn alloc() SphereShape {
return @as(SphereShape, @ptrCast(ShapeImpl.alloc(.sphere)));
}
pub const create = cbtShapeSphereCreate;
extern fn cbtShapeSphereCreate(sphere: SphereShape, radius: f32) void;
pub const getRadius = cbtShapeSphereGetRadius;
extern fn cbtShapeSphereGetRadius(sphere: SphereShape) f32;
pub const setUnscaledRadius = cbtShapeSphereSetUnscaledRadius;
extern fn cbtShapeSphereSetUnscaledRadius(sphere: SphereShape, radius: f32) void;
};
pub fn initCapsuleShape(radius: f32, height: f32, upaxis: Axis) CapsuleShape {
const capsule = CapsuleShapeImpl.alloc();
capsule.create(radius, height, upaxis);
return capsule;
}
const CapsuleShapeImpl = opaque {
const Interface = ShapeInterface(CapsuleShape);
pub const asShape = Interface.asShape;
pub const dealloc = Interface.dealloc;
pub const destroy = Interface.destroy;
pub const deinit = Interface.deinit;
pub const isCreated = Interface.isCreated;
pub const getType = Interface.getType;
pub const setMargin = Interface.setMargin;
pub const getMargin = Interface.getMargin;
pub const isPolyhedral = Interface.isPolyhedral;
pub const isConvex2d = Interface.isConvex2d;
pub const isConvex = Interface.isConvex;
pub const isNonMoving = Interface.isNonMoving;
pub const isConcave = Interface.isConcave;
pub const isCompound = Interface.isCompound;
pub const calculateLocalInertia = Interface.calculateLocalInertia;
pub const setUserPointer = Interface.setUserPointer;
pub const getUserPointer = Interface.getUserPointer;
pub const setUserIndex = Interface.setUserIndex;
pub const getUserIndex = Interface.getUserIndex;
fn alloc() CapsuleShape {
return @as(CapsuleShape, @ptrCast(ShapeImpl.alloc(.capsule)));
}
pub const create = cbtShapeCapsuleCreate;
extern fn cbtShapeCapsuleCreate(
capsule: CapsuleShape,
radius: f32,
height: f32,
upaxis: Axis,
) void;
pub const getUpAxis = cbtShapeCapsuleGetUpAxis;
extern fn cbtShapeCapsuleGetUpAxis(capsule: CapsuleShape) Axis;
pub const getHalfHeight = cbtShapeCapsuleGetHalfHeight;
extern fn cbtShapeCapsuleGetHalfHeight(capsule: CapsuleShape) f32;
pub const getRadius = cbtShapeCapsuleGetRadius;
extern fn cbtShapeCapsuleGetRadius(capsule: CapsuleShape) f32;
};
pub fn initCylinderShape(
half_extents: *const [3]f32,
upaxis: Axis,
) CylinderShape {
const cylinder = CylinderShapeImpl.alloc();
cylinder.create(half_extents, upaxis);
return cylinder;
}
const CylinderShapeImpl = opaque {
const Interface = ShapeInterface(CylinderShape);
pub const asShape = Interface.asShape;
pub const dealloc = Interface.dealloc;
pub const destroy = Interface.destroy;
pub const deinit = Interface.deinit;
pub const isCreated = Interface.isCreated;
pub const getType = Interface.getType;
pub const setMargin = Interface.setMargin;
pub const getMargin = Interface.getMargin;
pub const isPolyhedral = Interface.isPolyhedral;
pub const isConvex2d = Interface.isConvex2d;
pub const isConvex = Interface.isConvex;
pub const isNonMoving = Interface.isNonMoving;
pub const isConcave = Interface.isConcave;
pub const isCompound = Interface.isCompound;
pub const calculateLocalInertia = Interface.calculateLocalInertia;
pub const setUserPointer = Interface.setUserPointer;
pub const getUserPointer = Interface.getUserPointer;
pub const setUserIndex = Interface.setUserIndex;
pub const getUserIndex = Interface.getUserIndex;
fn alloc() CylinderShape {
return @as(CylinderShape, @ptrCast(ShapeImpl.alloc(.cylinder)));
}
pub const create = cbtShapeCylinderCreate;
extern fn cbtShapeCylinderCreate(
cylinder: CylinderShape,
half_extents: *const [3]f32,
upaxis: Axis,
) void;
pub const getHalfExtentsWithoutMargin = cbtShapeCylinderGetHalfExtentsWithoutMargin;
extern fn cbtShapeCylinderGetHalfExtentsWithoutMargin(
cylinder: CylinderShape,
half_extents: *[3]f32,
) void;
pub const getHalfExtentsWithMargin = cbtShapeCylinderGetHalfExtentsWithMargin;
extern fn cbtShapeCylinderGetHalfExtentsWithMargin(
cylinder: CylinderShape,
half_extents: *[3]f32,
) void;
pub const getUpAxis = cbtShapeCylinderGetUpAxis;
extern fn cbtShapeCylinderGetUpAxis(capsule: CylinderShape) Axis;
};
const ConvexHullShapeImpl = opaque {
const Interface = ShapeInterface(ConvexHullShape);
pub const asShape = Interface.asShape;
pub const dealloc = Interface.dealloc;
pub const destroy = Interface.destroy;
pub const deinit = Interface.deinit;
pub const isCreated = Interface.isCreated;
pub const getType = Interface.getType;
pub const setMargin = Interface.setMargin;
pub const getMargin = Interface.getMargin;
pub const isPolyhedral = Interface.isPolyhedral;
pub const isConvex2d = Interface.isConvex2d;
pub const isConvex = Interface.isConvex;
pub const isNonMoving = Interface.isNonMoving;
pub const isConcave = Interface.isConcave;
pub const isCompound = Interface.isCompound;
pub const calculateLocalInertia = Interface.calculateLocalInertia;
pub const setUserPointer = Interface.setUserPointer;
pub const getUserPointer = Interface.getUserPointer;
pub const setUserIndex = Interface.setUserIndex;
pub const getUserIndex = Interface.getUserIndex;
fn alloc() ConvexHullShape {
return @as(ConvexHullShape, @ptrCast(ShapeImpl.alloc(.convex_hull)));
}
pub const create = cbtShapeConvexHullCreate;
extern fn cbtShapeConvexHullCreate(
convex_hull: ConvexHullShape,
points: ?*f32,
num_points: i32,
stride: i32,
) void;
pub const recalcLocalAabb = cbtShapeConvexHullRecalcLocalAabb;
extern fn cbtShapeConvexHullRecalcLocalAabb(convex_hull: ConvexHullShape) void;
pub const addPoint = cbtShapeConvexHullAddPoint;
extern fn cbtShapeConvexHullAddPoint(convex_hull: ConvexHullShape, point: *const [3]f32, recalculate_local_aabb: bool) void;
};
pub fn initConvexHullShape(
args: struct {
points: ?*f32 = null,
num_points: i32 = 0,
stride: i32 = @sizeOf([3]f32),
},
) ConvexHullShape {
const cshape = ConvexHullShapeImpl.alloc();
cshape.create(args.points, args.num_points, args.stride);
return cshape;
}
pub fn initCompoundShape(
args: struct {
enable_dynamic_aabb_tree: bool = true,
initial_child_capacity: u32 = 0,
},
) CompoundShape {
const cshape = CompoundShapeImpl.alloc();
cshape.create(args.enable_dynamic_aabb_tree, args.initial_child_capacity);
return cshape;
}
const CompoundShapeImpl = opaque {
const Interface = ShapeInterface(CompoundShape);
pub const asShape = Interface.asShape;
pub const dealloc = Interface.dealloc;
pub const destroy = Interface.destroy;
pub const deinit = Interface.deinit;
pub const isCreated = Interface.isCreated;
pub const getType = Interface.getType;
pub const setMargin = Interface.setMargin;
pub const getMargin = Interface.getMargin;
pub const isPolyhedral = Interface.isPolyhedral;
pub const isConvex2d = Interface.isConvex2d;
pub const isConvex = Interface.isConvex;
pub const isNonMoving = Interface.isNonMoving;
pub const isConcave = Interface.isConcave;
pub const isCompound = Interface.isCompound;
pub const calculateLocalInertia = Interface.calculateLocalInertia;
pub const setUserPointer = Interface.setUserPointer;
pub const getUserPointer = Interface.getUserPointer;
pub const setUserIndex = Interface.setUserIndex;
pub const getUserIndex = Interface.getUserIndex;
fn alloc() CompoundShape {
return @as(CompoundShape, @ptrCast(ShapeImpl.alloc(.compound)));
}
pub const create = cbtShapeCompoundCreate;
extern fn cbtShapeCompoundCreate(
cshape: CompoundShape,
enable_dynamic_aabb_tree: bool,
initial_child_capacity: u32,
) void;
pub const addChild = cbtShapeCompoundAddChild;
extern fn cbtShapeCompoundAddChild(
cshape: CompoundShape,
local_transform: *const [12]f32,
child_shape: Shape,
) void;
pub const removeChild = cbtShapeCompoundRemoveChild;
extern fn cbtShapeCompoundRemoveChild(cshape: CompoundShape, child_shape: Shape) void;
pub const removeChildByIndex = cbtShapeCompoundRemoveChildByIndex;
extern fn cbtShapeCompoundRemoveChildByIndex(cshape: CompoundShape, index: i32) void;
pub const getNumChilds = cbtShapeCompoundGetNumChilds;
extern fn cbtShapeCompoundGetNumChilds(cshape: CompoundShape) i32;
pub const getChild = cbtShapeCompoundGetChild;
extern fn cbtShapeCompoundGetChild(cshape: CompoundShape, index: i32) Shape;
pub const getChildTransform = cbtShapeCompoundGetChildTransform;
extern fn cbtShapeCompoundGetChildTransform(
cshape: CompoundShape,
index: i32,
local_transform: *[12]f32,
) void;
};
pub fn initTriangleMeshShape() TriangleMeshShape {
const trimesh = TriangleMeshShapeImpl.alloc();
trimesh.createBegin();
return trimesh;
}
const TriangleMeshShapeImpl = opaque {
const Interface = ShapeInterface(TriangleMeshShape);
pub const asShape = Interface.asShape;
pub const dealloc = Interface.dealloc;
pub const destroy = Interface.destroy;
pub const deinit = Interface.deinit;
pub const isCreated = Interface.isCreated;
pub const getType = Interface.getType;
pub const setMargin = Interface.setMargin;
pub const getMargin = Interface.getMargin;
pub const isPolyhedral = Interface.isPolyhedral;
pub const isConvex2d = Interface.isConvex2d;
pub const isConvex = Interface.isConvex;
pub const isNonMoving = Interface.isNonMoving;
pub const isConcave = Interface.isConcave;
pub const isCompound = Interface.isCompound;
pub const calculateLocalInertia = Interface.calculateLocalInertia;
pub const setUserPointer = Interface.setUserPointer;
pub const getUserPointer = Interface.getUserPointer;
pub const setUserIndex = Interface.setUserIndex;
pub const getUserIndex = Interface.getUserIndex;
pub fn finish(trimesh: TriangleMeshShape) void {
trimesh.createEnd();
}
fn alloc() TriangleMeshShape {
return @as(TriangleMeshShape, @ptrCast(ShapeImpl.alloc(.trimesh)));
}
pub const addIndexVertexArray = cbtShapeTriMeshAddIndexVertexArray;
extern fn cbtShapeTriMeshAddIndexVertexArray(
trimesh: TriangleMeshShape,
num_triangles: u32,
triangles_base: *const anyopaque,
triangle_stride: u32,
num_vertices: u32,
vertices_base: *const anyopaque,
vertex_stride: u32,
) void;
pub const createBegin = cbtShapeTriMeshCreateBegin;
extern fn cbtShapeTriMeshCreateBegin(trimesh: TriangleMeshShape) void;
pub const createEnd = cbtShapeTriMeshCreateEnd;
extern fn cbtShapeTriMeshCreateEnd(trimesh: TriangleMeshShape) void;
};
pub const BodyActivationState = enum(c_int) {
active = 1,
sleeping = 2,
wants_deactivation = 3,
deactivation_disabled = 4,
simulation_disabled = 5,
};
pub const CollisionFlags = packed struct(c_int) {
static_object: bool = false,
kinematic_object: bool = false,
no_contact_response: bool = false,
custom_material_callback: bool = false,
character_object: bool = false,
disable_visualize_object: bool = false,
disable_spu_collision_processing: bool = false,
has_contact_stiffness_damping: bool = false,
has_custom_debug_rendering_color: bool = false,
has_friction_anchor: bool = false,
has_collision_sound_trigger: bool = false,
_padding: u21 = 0,
};
pub fn initBody(
mass: f32,
transform: *const [12]f32,
shape: Shape,
) Body {
const body = BodyImpl.alloc();
body.create(mass, transform, shape);
return body;
}
const BodyImpl = opaque {
pub fn deinit(body: Body) void {
body.destroy();
body.dealloc();
}
pub const alloc = cbtBodyAllocate;
extern fn cbtBodyAllocate() Body;
pub const dealloc = cbtBodyDeallocate;
extern fn cbtBodyDeallocate(body: Body) void;
pub const create = cbtBodyCreate;
extern fn cbtBodyCreate(
body: Body,
mass: f32,
transform: *const [12]f32,
shape: Shape,
) void;
pub const destroy = cbtBodyDestroy;
extern fn cbtBodyDestroy(body: Body) void;
pub const isCreated = cbtBodyIsCreated;
extern fn cbtBodyIsCreated(body: Body) bool;
pub const setShape = cbtBodySetShape;
extern fn cbtBodySetShape(body: Body, shape: Shape) void;
pub const getShape = cbtBodyGetShape;
extern fn cbtBodyGetShape(body: Body) Shape;
pub const getMass = cbtBodyGetMass;
extern fn cbtBodyGetMass(body: Body) f32;
pub const setRestitution = cbtBodySetRestitution;
extern fn cbtBodySetRestitution(body: Body, restitution: f32) void;
pub const getRestitution = cbtBodyGetRestitution;
extern fn cbtBodyGetRestitution(body: Body) f32;
pub const getTotalForce = cbtBodyGetTotalForce;
extern fn cbtBodyGetTotalForce(body: Body, force: *[3]f32) void;
pub const getTotalTorque = cbtBodyGetTotalTorque;
extern fn cbtBodyGetTotalTorque(body: Body, torque: *[3]f32) void;
pub const setFriction = cbtBodySetFriction;
extern fn cbtBodySetFriction(body: Body, friction: f32) void;
pub const setRollingFriction = cbtBodySetRollingFriction;
extern fn cbtBodySetRollingFriction(body: Body, friction: f32) void;
pub const setSpinningFriction = cbtBodySetSpinningFriction;
extern fn cbtBodySetSpinningFriction(body: Body, friction: f32) void;
pub const setAnisotropicFriction = cbtBodySetAnisotropicFriction;
extern fn cbtBodySetAnisotropicFriction(body: Body, friction: f32) void;
pub const getGraphicsWorldTransform = cbtBodyGetGraphicsWorldTransform;
extern fn cbtBodyGetGraphicsWorldTransform(
body: Body,
transform: *[12]f32,
) void;
pub const getCenterOfMassTransform = cbtBodyGetCenterOfMassTransform;
extern fn cbtBodyGetCenterOfMassTransform(
body: Body,
transform: *[12]f32,
) void;
pub const getInvCenterOfMassTransform = cbtBodyGetInvCenterOfMassTransform;
extern fn cbtBodyGetInvCenterOfMassTransform(
body: Body,
transform: *[12]f32,
) void;
pub const applyCentralForce = cbtBodyApplyCentralForce;
extern fn cbtBodyApplyCentralForce(body: Body, force: *const [3]f32) void;
pub const applyCentralImpulse = cbtBodyApplyCentralImpulse;
extern fn cbtBodyApplyCentralImpulse(body: Body, impulse: *const [3]f32) void;
pub const applyForce = cbtBodyApplyForce;
extern fn cbtBodyApplyForce(body: Body, force: *const [3]f32, rel_pos: *const [3]f32) void;
pub const applyImpulse = cbtBodyApplyImpulse;
extern fn cbtBodyApplyImpulse(body: Body, impulse: *const [3]f32, rel_pos: *const [3]f32) void;
pub const applyBodyTorque = cbtBodyApplyTorque;
extern fn cbtBodyApplyTorque(body: Body, torque: *const [3]f32) void;
pub const applyBodyTorqueImpulse = cbtBodyApplyTorqueImpulse;
extern fn cbtBodyApplyTorqueImpulse(body: Body, impulse: *const [3]f32) void;
pub const setUserIndex = cbtBodySetUserIndex;
extern fn cbtBodySetUserIndex(body: Body, slot: u32, index: i32) void;
pub const getUserIndex = cbtBodyGetUserIndex;
extern fn cbtBodyGetUserIndex(body: Body, slot: u32) i32;
pub const getCcdSweptSphereRadius = cbtBodyGetCcdSweptSphereRadius;
extern fn cbtBodyGetCcdSweptSphereRadius(body: Body) f32;
pub const setCcdSweptSphereRadius = cbtBodySetCcdSweptSphereRadius;
extern fn cbtBodySetCcdSweptSphereRadius(body: Body, radius: f32) void;
pub const getCcdMotionThreshold = cbtBodyGetCcdMotionThreshold;
extern fn cbtBodyGetCcdMotionThreshold(body: Body) f32;
pub const setCcdMotionThreshold = cbtBodySetCcdMotionThreshold;
extern fn cbtBodySetCcdMotionThreshold(body: Body, threshold: f32) void;
pub fn setCollisionFlags(body: Body, flags: CollisionFlags) void {
cbtBodySetCollisionFlags(body, @as(c_int, @bitCast(flags)));
}
extern fn cbtBodySetCollisionFlags(body: Body, flags: c_int) void;