-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathJoltPhysicsC.h
2406 lines (2036 loc) · 93.7 KB
/
JoltPhysicsC.h
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
// JoltPhysicsC v0.0.6 - C API for Jolt Physics C++ library
#pragma once
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdalign.h>
#include <float.h>
//--------------------------------------------------------------------------------------------------
//
// Const
//
//--------------------------------------------------------------------------------------------------
#ifndef JPC_API
#define JPC_API
#endif
// Always turn on asserts in Debug mode
#if defined(_DEBUG) || defined(JPH_ENABLE_ASSERTS)
#define JPC_ENABLE_ASSERTS 1
#else
#define JPC_ENABLE_ASSERTS 0
#endif
#if defined(JPH_DOUBLE_PRECISION)
#define JPC_DOUBLE_PRECISION 1
#else
#define JPC_DOUBLE_PRECISION 0
#endif
#if JPC_DOUBLE_PRECISION == 1
typedef double JPC_Real;
#define JPC_RVEC_ALIGN alignas(32)
#else
typedef float JPC_Real;
#define JPC_RVEC_ALIGN alignas(16)
#endif
#if defined(JPH_DEBUG_RENDERER)
#define JPC_DEBUG_RENDERER 1
#else
#define JPC_DEBUG_RENDERER 0
#endif
#define _JPC_REFTARGET_HEADER struct { const void * __vfptr_header[1]; uint32_t ref_count; };
#if defined(_MSC_VER)
#define _JPC_VTABLE_HEADER const void* __vtable_header[1]
// MSVC quirk: If the first member of a derived class has alignment > 8, then extra padding is inserted such that
// the first member of the base class has the same alignment.
#define _JPC_REFTARGET_HEADER_ALIGN_16 struct { const void * __vtable_ptr[2]; uint32_t ref_count; };
#else
#define _JPC_VTABLE_HEADER const void* __vtable_header[2]
#define _JPC_REFTARGET_HEADER_ALIGN_16 _JPC_REFTARGET_HEADER
#endif
#define JPC_PI 3.14159265358979323846f
#define JPC_COLLISION_GROUP_INVALID_GROUP 0xffffffff
#define JPC_COLLISION_GROUP_INVALID_SUB_GROUP 0xffffffff
#define JPC_BODY_ID_INVALID 0xffffffff
#define JPC_BODY_ID_INDEX_BITS 0x007fffff
#define JPC_BODY_ID_SEQUENCE_BITS 0xff000000
#define JPC_BODY_ID_SEQUENCE_SHIFT 24
#define JPC_SUB_SHAPE_ID_EMPTY 0xffffffff
#define JPC_FLT_EPSILON FLT_EPSILON
#ifdef __cplusplus
extern "C" {
#endif
// JPC_JobSystem_Create()
enum
{
JPC_MAX_PHYSICS_JOBS = 2048,
JPC_MAX_PHYSICS_BARRIERS = 8
};
typedef uint8_t JPC_PhysicsUpdateError;
enum
{
JPC_PHYSICS_UPDATE_NO_ERROR = 0,
JPC_PHYSICS_UPDATE_MANIFOLD_CACHE_FULL = 1 << 0,
JPC_PHYSICS_UPDATE_BODY_PAIR_CACHE_FULL = 1 << 1,
JPC_PHYSICS_UPDATE_CONTACT_CONSTRAINTS_FULL = 1 << 2,
};
typedef uint8_t JPC_ShapeType;
enum
{
JPC_SHAPE_TYPE_CONVEX = 0,
JPC_SHAPE_TYPE_COMPOUND = 1,
JPC_SHAPE_TYPE_DECORATED = 2,
JPC_SHAPE_TYPE_MESH = 3,
JPC_SHAPE_TYPE_HEIGHT_FIELD = 4,
JPC_SHAPE_TYPE_SOFT_BODY = 5,
JPC_SHAPE_TYPE_USER1 = 6,
JPC_SHAPE_TYPE_USER2 = 7,
JPC_SHAPE_TYPE_USER3 = 8,
JPC_SHAPE_TYPE_USER4 = 9
};
typedef uint8_t JPC_ShapeSubType;
enum
{
JPC_SHAPE_SUB_TYPE_SPHERE = 0,
JPC_SHAPE_SUB_TYPE_BOX = 1,
JPC_SHAPE_SUB_TYPE_TRIANGLE = 2,
JPC_SHAPE_SUB_TYPE_CAPSULE = 3,
JPC_SHAPE_SUB_TYPE_TAPERED_CAPSULE = 4,
JPC_SHAPE_SUB_TYPE_CYLINDER = 5,
JPC_SHAPE_SUB_TYPE_CONVEX_HULL = 6,
JPC_SHAPE_SUB_TYPE_STATIC_COMPOUND = 7,
JPC_SHAPE_SUB_TYPE_MUTABLE_COMPOUND = 8,
JPC_SHAPE_SUB_TYPE_ROTATED_TRANSLATED = 9,
JPC_SHAPE_SUB_TYPE_SCALED = 10,
JPC_SHAPE_SUB_TYPE_OFFSET_CENTER_OF_MASS = 11,
JPC_SHAPE_SUB_TYPE_MESH = 12,
JPC_SHAPE_SUB_TYPE_HEIGHT_FIELD = 13,
JPC_SHAPE_SUB_TYPE_SOFT_BODY = 14,
JPC_SHAPE_SUB_TYPE_USER1 = 15,
JPC_SHAPE_SUB_TYPE_USER2 = 16,
JPC_SHAPE_SUB_TYPE_USER3 = 17,
JPC_SHAPE_SUB_TYPE_USER4 = 18,
JPC_SHAPE_SUB_TYPE_USER5 = 19,
JPC_SHAPE_SUB_TYPE_USER6 = 20,
JPC_SHAPE_SUB_TYPE_USER7 = 21,
JPC_SHAPE_SUB_TYPE_USER8 = 22,
JPC_SHAPE_SUB_TYPE_USER_CONVEX1 = 23,
JPC_SHAPE_SUB_TYPE_USER_CONVEX2 = 24,
JPC_SHAPE_SUB_TYPE_USER_CONVEX3 = 25,
JPC_SHAPE_SUB_TYPE_USER_CONVEX4 = 26,
JPC_SHAPE_SUB_TYPE_USER_CONVEX5 = 27,
JPC_SHAPE_SUB_TYPE_USER_CONVEX6 = 28,
JPC_SHAPE_SUB_TYPE_USER_CONVEX7 = 29,
JPC_SHAPE_SUB_TYPE_USER_CONVEX8 = 30,
};
typedef enum JPC_ConstraintType
{
JPC_CONSTRAINT_TYPE_CONSTRAINT = 0,
JPC_CONSTRAINT_TYPE_TWO_BODY_CONSTRAINT = 1,
_JPC_CONSTRAINT_TYPE_FORCEU32 = 0x7fffffff
} JPC_ConstraintType;
typedef enum JPC_ConstraintSubType
{
JPC_CONSTRAINT_SUB_TYPE_FIXED = 0,
JPC_CONSTRAINT_SUB_TYPE_POINT = 1,
JPC_CONSTRAINT_SUB_TYPE_HINGE = 2,
JPC_CONSTRAINT_SUB_TYPE_SLIDER = 3,
JPC_CONSTRAINT_SUB_TYPE_DISTANCE = 4,
JPC_CONSTRAINT_SUB_TYPE_CONE = 5,
JPC_CONSTRAINT_SUB_TYPE_SWING_TWIST = 6,
JPC_CONSTRAINT_SUB_TYPE_SIX_DOF = 7,
JPC_CONSTRAINT_SUB_TYPE_PATH = 8,
JPC_CONSTRAINT_SUB_TYPE_VEHICLE = 9,
JPC_CONSTRAINT_SUB_TYPE_RACK_AND_PINION = 10,
JPC_CONSTRAINT_SUB_TYPE_GEAR = 11,
JPC_CONSTRAINT_SUB_TYPE_PULLEY = 12,
JPC_CONSTRAINT_SUB_TYPE_USER1 = 13,
JPC_CONSTRAINT_SUB_TYPE_USER2 = 14,
JPC_CONSTRAINT_SUB_TYPE_USER3 = 15,
JPC_CONSTRAINT_SUB_TYPE_USER4 = 16,
_JPC_CONSTRAINT_SUB_TYPE_FORCEU32 = 0x7fffffff
} JPC_ConstraintSubType;
typedef enum JPC_ConstraintSpace
{
JPC_CONSTRAINT_SPACE_LOCAL_TO_BODY_COM = 0,
JPC_CONSTRAINT_SPACE_WORLD_SPACE = 1,
_JPC_CONSTRAINT_SPACE_FORCEU32 = 0x7fffffff
} JPC_ConstraintSpace;
typedef uint8_t JPC_MotionType;
enum
{
JPC_MOTION_TYPE_STATIC = 0,
JPC_MOTION_TYPE_KINEMATIC = 1,
JPC_MOTION_TYPE_DYNAMIC = 2
};
typedef uint8_t JPC_MotionQuality;
enum
{
JPC_MOTION_QUALITY_DISCRETE = 0,
JPC_MOTION_QUALITY_LINEAR_CAST = 1
};
typedef uint8_t JPC_AllowedDOFs;
enum {
JPC_ALLOWED_DOFS_NONE = 0b000000,
JPC_ALLOWED_DOFS_ALL = 0b111111,
JPC_ALLOWED_DOFS_TRANSLATION_X = 0b000001,
JPC_ALLOWED_DOFS_TRANSLATION_Y = 0b000010,
JPC_ALLOWED_DOFS_TRANSLATION_Z = 0b000100,
JPC_ALLOWED_DOFS_ROTATION_X = 0b001000,
JPC_ALLOWED_DOFS_ROTATION_Y = 0b010000,
JPC_ALLOWED_DOFS_ROTATION_Z = 0b100000,
};
typedef uint8_t JPC_OverrideMassProperties;
enum
{
JPC_OVERRIDE_MASS_PROPS_CALC_MASS_INERTIA = 0,
JPC_OVERRIDE_MASS_PROPS_CALC_INERTIA = 1,
JPC_OVERRIDE_MASS_PROPS_MASS_INERTIA_PROVIDED = 2
};
typedef enum JPC_CharacterGroundState
{
JPC_CHARACTER_GROUND_STATE_ON_GROUND = 0,
JPC_CHARACTER_GROUND_STATE_ON_STEEP_GROUND = 1,
JPC_CHARACTER_GROUND_STATE_NOT_SUPPORTED = 2,
JPC_CHARACTER_GROUND_STATE_IN_AIR = 3,
_JPC_CHARACTER_GROUND_FORCEU32 = 0x7fffffff
} JPC_CharacterGroundState;
typedef enum JPC_Activation
{
JPC_ACTIVATION_ACTIVATE = 0,
JPC_ACTIVATION_DONT_ACTIVATE = 1,
_JPC_ACTIVATION_FORCEU32 = 0x7fffffff
} JPC_Activation;
typedef enum JPC_ValidateResult
{
JPC_VALIDATE_RESULT_ACCEPT_ALL_CONTACTS = 0,
JPC_VALIDATE_RESULT_ACCEPT_CONTACT = 1,
JPC_VALIDATE_RESULT_REJECT_CONTACT = 2,
JPC_VALIDATE_RESULT_REJECT_ALL_CONTACTS = 3,
_JPC_VALIDATE_RESULT_FORCEU32 = 0x7fffffff
} JPC_ValidateResult;
typedef uint8_t JPC_BackFaceMode;
enum
{
JPC_BACK_FACE_IGNORE = 0,
JPC_BACK_FACE_COLLIDE = 1
};
typedef uint8_t JPC_BodyType;
enum {
JPC_BODY_TYPE_RIGID_BODY = 0,
JPC_BODY_TYPE_SOFT_BODY = 1,
};
#if JPC_DEBUG_RENDERER == 1
typedef enum JPC_DebugRendererResult {
JPC_DEBUGRENDERER_SUCCESS,
JPC_DEBUGRENDERER_DUPLICATE_SINGLETON,
JPC_DEBUGRENDERER_MISSING_SINGLETON,
JPC_DEBUGRENDERER_INCOMPLETE_IMPL
} JPC_DebugRendererResult;
typedef enum JPC_CullMode {
JPC_CULL_BACK_FACE = 0,
JPC_CULL_FRONT_FACE = 1,
JPC_CULLING_OFF = 2,
_JPC_CULLING_FORCEU32 = 0x7fffffff
} JPC_CullMode;
typedef enum JPC_CastShadow {
JPC_CAST_SHADOW_ON = 0,
JPC_CAST_SHADOW_OFF = 1,
_JPC_CAST_SHADOW_FORCEU32 = 0x7fffffff
} JPC_CastShadow;
typedef enum JPC_DrawMode {
JPC_DRAW_MODE_SOLID = 0,
JPC_DRAW_MODE_WIREFRAME = 1,
_JPC_DRAW_MODE_FORCEU32 = 0x7fffffff
} JPC_DrawMode;
typedef enum JPC_ShapeColor {
JPC_INSTANCE_COLOR, // Random color per instance
JPC_SHAPE_TYPE_COLOR, // Convex = green, scaled = yellow, compound = orange, mesh = red
JPC_MOTION_TYPE_COLOR, // Static = grey, keyframed = green, dynamic = random color per instance
JPC_SLEEP_COLOR, // Static = grey, keyframed = green, dynamic = yellow, sleeping = red
JPC_ISLAND_COLOR, // Static = grey, active = random color per island, sleeping = light grey
JPC_MATERIAL_COLOR, // Color as defined by the PhysicsMaterial of the shape
} JPC_ShapeColor;
#endif //JPC_DEBUG_RENDERER
//--------------------------------------------------------------------------------------------------
//
// Types
//
//--------------------------------------------------------------------------------------------------
typedef uint16_t JPC_ObjectLayer;
typedef uint8_t JPC_BroadPhaseLayer;
// TODO: Consider using structures for IDs
typedef uint32_t JPC_BodyID;
typedef uint32_t JPC_SubShapeID;
typedef uint32_t JPC_CollisionGroupID;
typedef uint32_t JPC_CollisionSubGroupID;
// Must be 16 byte aligned
typedef void *(*JPC_AllocateFunction)(size_t in_size);
typedef void *(*JPC_ReallocateFunction)(void *in_block, size_t old_size, size_t new_size);
typedef void (*JPC_FreeFunction)(void *in_block);
typedef void *(*JPC_AlignedAllocateFunction)(size_t in_size, size_t in_alignment);
typedef void (*JPC_AlignedFreeFunction)(void *in_block);
typedef void (*JPC_TraceFunction)(const char *inFMT, ...);
typedef bool (*JPC_AssertFailedFunction)(
const char* in_expression,
const char* in_message,
const char* in_file,
uint32_t in_line);
//--------------------------------------------------------------------------------------------------
//
// Opaque Types
//
//--------------------------------------------------------------------------------------------------
typedef struct JPC_TempAllocator JPC_TempAllocator;
typedef struct JPC_JobSystem JPC_JobSystem;
typedef struct JPC_BodyInterface JPC_BodyInterface;
typedef struct JPC_BodyLockInterface JPC_BodyLockInterface;
typedef struct JPC_NarrowPhaseQuery JPC_NarrowPhaseQuery;
typedef struct JPC_ShapeSettings JPC_ShapeSettings;
typedef struct JPC_ConvexShapeSettings JPC_ConvexShapeSettings;
typedef struct JPC_BoxShapeSettings JPC_BoxShapeSettings;
typedef struct JPC_SphereShapeSettings JPC_SphereShapeSettings;
typedef struct JPC_TriangleShapeSettings JPC_TriangleShapeSettings;
typedef struct JPC_CapsuleShapeSettings JPC_CapsuleShapeSettings;
typedef struct JPC_TaperedCapsuleShapeSettings JPC_TaperedCapsuleShapeSettings;
typedef struct JPC_CylinderShapeSettings JPC_CylinderShapeSettings;
typedef struct JPC_ConvexHullShapeSettings JPC_ConvexHullShapeSettings;
typedef struct JPC_HeightFieldShapeSettings JPC_HeightFieldShapeSettings;
typedef struct JPC_MeshShapeSettings JPC_MeshShapeSettings;
typedef struct JPC_DecoratedShapeSettings JPC_DecoratedShapeSettings;
typedef struct JPC_CompoundShapeSettings JPC_CompoundShapeSettings;
typedef struct JPC_CharacterContactSettings JPC_CharacterContactSettings;
typedef struct JPC_ConstraintSettings JPC_ConstraintSettings;
typedef struct JPC_TwoBodyConstraintSettings JPC_TwoBodyConstraintSettings;
typedef struct JPC_FixedConstraintSettings JPC_FixedConstraintSettings;
typedef struct JPC_PhysicsSystem JPC_PhysicsSystem;
typedef struct JPC_SharedMutex JPC_SharedMutex;
typedef struct JPC_Shape JPC_Shape;
typedef struct JPC_BoxShape JPC_BoxShape;
typedef struct JPC_ConvexHullShape JPC_ConvexHullShape;
typedef struct JPC_DecoratedShape JPC_DecoratedShape;
typedef struct JPC_RotatedTranslatedShape JPC_RotatedTranslatedShape;
typedef struct JPC_ShapeToIDMap JPC_ShapeToIDMap;
typedef struct JPC_MaterialToIDMap JPC_MaterialToIDMap;
typedef struct JPC_IDToShapeMap JPC_IDToShapeMap;
typedef struct JPC_IDToMaterialMap JPC_IDToMaterialMap;
typedef struct JPC_Constraint JPC_Constraint;
typedef struct JPC_PhysicsMaterial JPC_PhysicsMaterial;
typedef struct JPC_GroupFilter JPC_GroupFilter;
typedef struct JPC_Character JPC_Character;
typedef struct JPC_CharacterVirtual JPC_CharacterVirtual;
#if JPC_DEBUG_RENDERER == 1
typedef struct JPC_BodyDrawFilter JPC_BodyDrawFilter;
typedef struct JPC_DebugRenderer_TriangleBatch JPC_DebugRenderer_TriangleBatch;
typedef struct JPC_DebugRenderer_Primitive JPC_DebugRenderer_Primitive;
#endif //JPC_DEBUG_RENDERER
//--------------------------------------------------------------------------------------------------
//
// Structures
//
//--------------------------------------------------------------------------------------------------
// NOTE: Needs to be kept in sync with JPH::MassProperties
typedef struct JPC_MassProperties
{
float mass;
alignas(16) float inertia[16];
} JPC_MassProperties;
// NOTE: Needs to be kept in sync with JPH::MotionProperties
typedef struct JPC_MotionProperties
{
alignas(16) float linear_velocity[4]; // 4th element is ignored
alignas(16) float angular_velocity[4]; // 4th element is ignored
alignas(16) float inv_inertia_diagonal[4]; // 4th element is ignored
alignas(16) float inertia_rotation[4];
float force[3];
float torque[3];
float inv_mass;
float linear_damping;
float angular_damping;
float max_linear_velocity;
float max_angular_velocity;
float gravity_factor;
uint32_t index_in_active_bodies;
uint32_t island_index;
JPC_MotionQuality motion_quality;
bool allow_sleeping;
JPC_AllowedDOFs allowed_DOFs;
uint8_t num_velocity_steps_override;
uint8_t num_position_steps_override;
#if JPC_DOUBLE_PRECISION == 1
alignas(8) uint8_t reserved[77];
#else
alignas(4) uint8_t reserved[53];
#endif
#if JPC_ENABLE_ASSERTS == 1
JPC_MotionType cached_motion_type;
#endif
} JPC_MotionProperties;
// NOTE: Needs to be kept in sync with JPH::CollisionGroup
typedef struct JPC_CollisionGroup
{
const JPC_GroupFilter * filter;
JPC_CollisionGroupID group_id;
JPC_CollisionSubGroupID sub_group_id;
} JPC_CollisionGroup;
// NOTE: Needs to be kept in sync with JPH::BodyCreationSettings
typedef struct JPC_BodyCreationSettings
{
JPC_RVEC_ALIGN JPC_Real position[4]; // 4th element is ignored
alignas(16) float rotation[4];
alignas(16) float linear_velocity[4]; // 4th element is ignored
alignas(16) float angular_velocity[4]; // 4th element is ignored
uint64_t user_data;
JPC_ObjectLayer object_layer;
JPC_CollisionGroup collision_group;
JPC_MotionType motion_type;
JPC_AllowedDOFs allowed_DOFs;
bool allow_dynamic_or_kinematic;
bool is_sensor;
bool collide_kinematic_vs_non_dynamic;
bool use_manifold_reduction;
bool apply_gyroscopic_force;
JPC_MotionQuality motion_quality;
bool enhanced_internal_edge_removal;
bool allow_sleeping;
float friction;
float restitution;
float linear_damping;
float angular_damping;
float max_linear_velocity;
float max_angular_velocity;
float gravity_factor;
uint32_t num_velocity_steps_override;
uint32_t num_position_steps_override;
JPC_OverrideMassProperties override_mass_properties;
float inertia_multiplier;
JPC_MassProperties mass_properties_override;
const void * reserved;
const JPC_Shape * shape;
} JPC_BodyCreationSettings;
// NOTE: Needs to be kept in sync with JPH::Body
typedef struct JPC_Body
{
JPC_RVEC_ALIGN JPC_Real position[4]; // 4th element is ignored
alignas(16) float rotation[4];
alignas(16) float bounds_min[4]; // 4th element is ignored
alignas(16) float bounds_max[4]; // 4th element is ignored
const JPC_Shape * shape;
JPC_MotionProperties * motion_properties; // will be NULL for static bodies
uint64_t user_data;
JPC_CollisionGroup collision_group;
float friction;
float restitution;
JPC_BodyID id;
JPC_ObjectLayer object_layer;
JPC_BodyType body_type;
JPC_BroadPhaseLayer broad_phase_layer;
JPC_MotionType motion_type;
uint8_t flags;
} JPC_Body;
// NOTE: Needs to be kept in sync
typedef struct JPC_CharacterBaseSettings
{
_JPC_REFTARGET_HEADER_ALIGN_16;
alignas(16) float up[4]; // 4th element is ignored
alignas(16) float supporting_volume[4];
float max_slope_angle;
bool enhanced_internal_edge_removal;
const JPC_Shape * shape;
} JPC_CharacterBaseSettings;
// NOTE: Needs to be kept in sync
typedef struct JPC_CharacterSettings
{
JPC_CharacterBaseSettings base;
JPC_ObjectLayer layer;
float mass;
float friction;
float gravity_factor;
} JPC_CharacterSettings;
// NOTE: Needs to be kept in sync
typedef struct JPC_CharacterVirtualSettings
{
JPC_CharacterBaseSettings base;
float mass;
float max_strength;
alignas(16) float shape_offset[4];
JPC_BackFaceMode back_face_mode;
float predictive_contact_distance;
uint32_t max_collision_iterations;
uint32_t max_constraint_iterations;
float min_time_remaining;
float collision_tolerance;
float character_padding;
uint32_t max_num_hits;
float hit_reduction_cos_max_angle;
float penetration_recovery_speed;
const JPC_Shape * inner_body_shape;
JPC_ObjectLayer inner_body_layer;
} JPC_CharacterVirtualSettings;
// NOTE: Needs to be kept in sync with JPH::SubShapeIDCreator
typedef struct JPC_SubShapeIDCreator
{
JPC_SubShapeID id;
uint32_t current_bit;
} JPC_SubShapeIDCreator;
// NOTE: Needs to be kept in sync with JPH::SubShapeIDPair
typedef struct JPC_SubShapeIDPair
{
struct {
JPC_BodyID body_id;
JPC_SubShapeID sub_shape_id;
} first;
struct {
JPC_BodyID body_id;
JPC_SubShapeID sub_shape_id;
} second;
} JPC_SubShapeIDPair;
// NOTE: Needs to be kept in sync with JPH::ContactManifold
typedef struct JPC_ContactManifold
{
JPC_RVEC_ALIGN JPC_Real base_offset[4]; // 4th element is ignored
alignas(16) float normal[4]; // 4th element is ignored; world space
float penetration_depth;
JPC_SubShapeID shape1_sub_shape_id;
JPC_SubShapeID shape2_sub_shape_id;
struct {
alignas(16) uint32_t num_points;
alignas(16) float points[64][4]; // 4th element is ignored; world space
} shape1_relative_contact;
struct {
alignas(16) uint32_t num_points;
alignas(16) float points[64][4]; // 4th element is ignored; world space
} shape2_relative_contact;
} JPC_ContactManifold;
// NOTE: Needs to be kept in sync with JPH::ContactSettings
typedef struct JPC_ContactSettings
{
float combined_friction;
float combined_restitution;
float inv_mass_scale_1;
float inv_inertia_scale_1;
float inv_mass_scale_2;
float inv_inertia_scale_2;
bool is_sensor;
alignas(16) float relative_linear_surface_velocity[4]; // 4th element is ignored
alignas(16) float relative_angular_surface_velocity[4]; // 4th element is ignored
} JPC_ContactSettings;
// NOTE: Needs to be kept in sync with JPH::CollideShapeResult
typedef struct JPC_CollideShapeResult
{
alignas(16) float shape1_contact_point[4]; // 4th element is ignored; world space
alignas(16) float shape2_contact_point[4]; // 4th element is ignored; world space
alignas(16) float penetration_axis[4]; // 4th element is ignored; world space
float penetration_depth;
JPC_SubShapeID shape1_sub_shape_id;
JPC_SubShapeID shape2_sub_shape_id;
JPC_BodyID body2_id;
struct {
alignas(16) uint32_t num_points;
alignas(16) float points[32][4]; // 4th element is ignored; world space
} shape1_face;
struct {
alignas(16) uint32_t num_points;
alignas(16) float points[32][4]; // 4th element is ignored; world space
} shape2_face;
} JPC_CollideShapeResult;
// NOTE: Needs to be kept in sync with JPH::TransformedShape
typedef struct JPC_TransformedShape
{
JPC_RVEC_ALIGN JPC_Real shape_position_com[4]; // 4th element is ignored
alignas(16) float shape_rotation[4];
const JPC_Shape * shape;
float shape_scale[3];
JPC_BodyID body_id;
JPC_SubShapeIDCreator sub_shape_id_creator;
} JPC_TransformedShape;
// NOTE: Needs to be kept in sync with JPH::BodyLockRead
typedef struct JPC_BodyLockRead
{
const JPC_BodyLockInterface *lock_interface;
JPC_SharedMutex * mutex;
const JPC_Body * body;
} JPC_BodyLockRead;
// NOTE: Needs to be kept in sync with JPH::BodyLockWrite
typedef struct JPC_BodyLockWrite
{
const JPC_BodyLockInterface *lock_interface;
JPC_SharedMutex * mutex;
JPC_Body * body;
} JPC_BodyLockWrite;
// NOTE: Needs to be kept in sync with JPH::RayCast
typedef struct JPC_RayCast
{
alignas(16) float origin[4]; // 4th element is ignored
alignas(16) float direction[4]; // length of the vector is important; 4th element is ignored
} JPC_RayCast;
// NOTE: Needs to be kept in sync with JPH::RRayCast
typedef struct JPC_RRayCast
{
JPC_RVEC_ALIGN JPC_Real origin[4]; // 4th element is ignored
alignas(16) float direction[4]; // length of the vector is important; 4th element is ignored
} JPC_RRayCast;
// NOTE: Needs to be kept in sync with JPH::RayCastResult
typedef struct JPC_RayCastResult
{
JPC_BodyID body_id; // JPC_BODY_ID_INVALID
float fraction; // 1.0 + JPC_FLT_EPSILON
JPC_SubShapeID sub_shape_id;
} JPC_RayCastResult;
// NOTE: Needs to be kept in sync with JPH::RayCastSettings
typedef struct JPC_RayCastSettings
{
JPC_BackFaceMode back_face_mode;
bool treat_convex_as_solid;
} JPC_RayCastSettings;
typedef struct JPC_AABox
{
alignas(16) float min[3];
alignas(16) float max[3];
} JPC_AABox;
typedef struct JPC_RMatrix
{
alignas(16) float column_0[4];
alignas(16) float column_1[4];
alignas(16) float column_2[4];
JPC_RVEC_ALIGN JPC_Real column_3[4];
} JPC_RMatrix;
typedef struct JPC_Shape_SupportingFace
{
alignas(16) uint32_t num_points;
alignas(16) float points[32][4]; // 4th element is ignored; world space
} JPC_Shape_SupportingFace;
// NOTE: Needs to be kept in sync with JPH::CharacterVirtual::ExtendedUpdateSettings
typedef struct JPC_CharacterVirtual_ExtendedUpdateSettings
{
alignas(16) float stick_to_floor_step_down[4]; // 4th element is ignored;
alignas(16) float walk_stairs_step_up[4]; // 4th element is ignored;
float walk_stairs_min_step_forward;
float walk_stairs_step_forward_test;
float walk_stairs_cos_angle_forward_contact;
alignas(16) float walk_stairs_step_down_extra[4]; // 4th element is ignored;
} JPC_CharacterVirtual_ExtendedUpdateSettings;
#if JPC_DEBUG_RENDERER == 1
// NOTE: Needs to be kept in sync with JPH::AABox
// NOTE: Needs to be kept in sync with JPH::Color
typedef union JPC_Color
{
uint32_t u32;
struct
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
} JPC_Color;
// NOTE: Needs to be kept in sync with JPH::DebugRenderer::Vertex
typedef struct JPC_DebugRenderer_Vertex
{
float position[3];
float normal[3];
float uv[2];
JPC_Color color;
} JPC_DebugRenderer_Vertex;
// NOTE: Needs to be kept in sync with JPH::DebugRenderer::Triangle
typedef struct JPC_DebugRenderer_Triangle
{
JPC_DebugRenderer_Vertex v[3];
} JPC_DebugRenderer_Triangle;
// NOTE: Needs to be kept in sync with JPH::DebugRenderer::LOD
typedef struct JPC_DebugRenderer_LOD
{
JPC_DebugRenderer_TriangleBatch *batch;
float distance;
} JPC_DebugRenderer_LOD;
// NOTE: NOT kept in sync - some translation required due to JPH::DebugRenderer::Geometry using std::vector.
typedef struct JPC_DebugRenderer_Geometry
{
JPC_DebugRenderer_LOD *LODs;
uint64_t num_LODs;
JPC_AABox *bounds;
} JPC_DebugRenderer_Geometry;
// NOTE: Needs to be kept in sync with JPH::BodyManager::DrawSettings
// For each boolean field, if it's true, that thing will be drawn.
typedef struct JPC_BodyManager_DrawSettings
{
bool get_support_func; // = false | Draw the GetSupport() function, used for convex collision detection
bool get_support_dir; // = false | If above true, also draw direction mapped to a specific support point
bool get_supporting_face; // = false | Draw the faces that were found colliding during collision detection
bool shape; // = true | Draw the shapes of all bodies
bool shape_wireframe; // = false | If 'shape' true, the shapes will be drawn in wireframe instead of solid.
JPC_ShapeColor shape_color; // = JPC_MOTION_TYPE_COLOR | Coloring scheme to use for shapes
bool bounding_box; // = false | Draw a bounding box per body
bool center_of_mass_transform; // = false | Draw the center of mass for each body
bool world_transform; // = false | Draw the world transform (which can be different than CoM) for each body
bool velocity; // = false | Draw the velocity vector for each body
bool mass_and_inertia; // = false | Draw the mass and inertia (as the box equivalent) for each body
bool sleep_stats; // = false | Draw stats regarding the sleeping algorithm of each body
} JPC_BodyManager_DrawSettings;
typedef bool (*JPC_BodyDrawFilterFunc)(const JPC_Body *);
#endif //JPC_DEBUG_RENDERER
//--------------------------------------------------------------------------------------------------
//
// Interfaces (virtual tables)
//
//--------------------------------------------------------------------------------------------------
typedef struct JPC_StreamOutVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
void
(*WriteBytes)(void *in_self, const void *in_data, size_t in_num_bytes);
// Required, *cannot* be NULL.
bool
(*IsFailed)(const void *in_self);
} JPC_StreamOutVTable;
typedef struct JPC_StreamInVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
void
(*ReadBytes)(void *in_self, void *out_data, size_t in_num_bytes);
// Required, *cannot* be NULL.
bool
(*IsEOF)(const void *in_self);
// Required, *cannot* be NULL.
bool
(*IsFailed)(const void *in_self);
} JPC_StreamInVTable;
typedef struct JPC_BroadPhaseLayerInterfaceVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
uint32_t
(*GetNumBroadPhaseLayers)(const void *in_self);
#ifdef _MSC_VER
// Required, *cannot* be NULL.
const JPC_BroadPhaseLayer *
(*GetBroadPhaseLayer)(const void *in_self, JPC_BroadPhaseLayer *out_layer, JPC_ObjectLayer in_layer);
#else
// Required, *cannot* be NULL.
JPC_BroadPhaseLayer
(*GetBroadPhaseLayer)(const void *in_self, JPC_ObjectLayer in_layer);
#endif
} JPC_BroadPhaseLayerInterfaceVTable;
typedef struct JPC_ObjectVsBroadPhaseLayerFilterVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
bool
(*ShouldCollide)(const void *in_self, JPC_ObjectLayer in_layer1, JPC_BroadPhaseLayer in_layer2);
} JPC_ObjectVsBroadPhaseLayerFilterVTable;
typedef struct JPC_BroadPhaseLayerFilterVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
bool
(*ShouldCollide)(const void *in_self, JPC_BroadPhaseLayer in_layer);
} JPC_BroadPhaseLayerFilterVTable;
typedef struct JPC_ObjectLayerPairFilterVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
bool
(*ShouldCollide)(const void *in_self, JPC_ObjectLayer in_layer1, JPC_ObjectLayer in_layer2);
} JPC_ObjectLayerPairFilterVTable;
typedef struct JPC_ObjectLayerFilterVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
bool
(*ShouldCollide)(const void *in_self, JPC_ObjectLayer in_layer);
} JPC_ObjectLayerFilterVTable;
typedef struct JPC_BodyActivationListenerVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
void
(*OnBodyActivated)(void *in_self, const JPC_BodyID *in_body_id, uint64_t in_user_data);
// Required, *cannot* be NULL.
void
(*OnBodyDeactivated)(void *in_self, const JPC_BodyID *in_body_id, uint64_t in_user_data);
} JPC_BodyActivationListenerVTable;
typedef struct JPC_BodyFilterVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
bool
(*ShouldCollide)(const void *in_self, const JPC_BodyID *in_body_id);
// Required, *cannot* be NULL.
bool
(*ShouldCollideLocked)(const void *in_self, const JPC_Body *in_body);
} JPC_BodyFilterVTable;
typedef struct JPC_ShapeFilterVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
bool
(*ShouldCollide)(const void *in_self, const JPC_Shape *in_shape, const JPC_SubShapeID *in_sub_shape_id);
// Required, *cannot* be NULL.
bool
(*PairShouldCollide)(const void *in_self,
const JPC_Shape *in_shape1,
const JPC_SubShapeID *in_sub_shape_id1,
const JPC_Shape *in_shape2,
const JPC_SubShapeID *in_sub_shape_id2);
// Set by the collision detection functions to the body ID of the "receiving" body before ShouldCollide is called.
uint32_t bodyId2;
} JPC_ShapeFilterVTable;
typedef struct JPC_PhysicsStepListenerVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
void
(*OnStep)(float in_delta_time, JPC_PhysicsSystem *in_physics_system);
} JPC_PhysicsStepListener;
// Made all callbacks required for this one for simplicity's sake, but can be modified to imitate ContactListener later.
typedef struct JPC_CharacterContactListenerVTable
{
_JPC_VTABLE_HEADER;
// Required, *cannot* be NULL.
void
(*OnAdjustBodyVelocity)(void *in_self,
const JPC_CharacterVirtual *in_character,
const JPC_Body *in_body2,
const float io_linear_velocity[3],
const float io_angular_velocity[3]);
// Required, *cannot* be NULL.
bool
(*OnContactValidate)(void *in_self,
const JPC_CharacterVirtual *in_character,
const JPC_Body *in_body2,
const JPC_SubShapeID *sub_shape_id);
// Required, *cannot* be NULL.
bool
(*OnCharacterContactValidate)(void *in_self,
const JPC_CharacterVirtual *in_character,
const JPC_CharacterVirtual *in_other_character,
const JPC_SubShapeID *sub_shape_id);
// Required, *cannot* be NULL.
void
(*OnContactAdded)(void *in_self,
const JPC_CharacterVirtual *in_character,
const JPC_Body *in_body2,
const JPC_SubShapeID *sub_shape_id,
const JPC_Real contact_position[3],
const float contact_normal[3],
JPC_CharacterContactSettings *io_settings);
// Required, *cannot* be NULL.
void
(*OnCharacterContactAdded)(void *in_self,
const JPC_CharacterVirtual *in_character,
const JPC_CharacterVirtual *in_other_character,
const JPC_SubShapeID *sub_shape_id,
const JPC_Real contact_position[3],
const float contact_normal[3],
JPC_CharacterContactSettings *io_settings);
// Required, *cannot* be NULL.
void
(*OnContactSolve)(void *in_self,
const JPC_CharacterVirtual *in_character,
const JPC_Body *in_body2,
const JPC_SubShapeID *sub_shape_id,
const JPC_Real contact_position[3],
const float contact_normal[3],
const float contact_velocity[3],
const JPC_PhysicsMaterial *contact_material,
const float character_velocity_in[3],
float character_velocity_out[3]);
// Required, *cannot* be NULL.
void
(*OnCharacterContactSolve)(void *in_self,
const JPC_CharacterVirtual *in_character,
const JPC_CharacterVirtual *in_other_character,
const JPC_SubShapeID *sub_shape_id,
const JPC_Real contact_position[3],
const float contact_normal[3],
const float contact_velocity[3],
const JPC_PhysicsMaterial *contact_material,
const float character_velocity_in[3],
float character_velocity_out[3]);
} JPC_CharacterContactListenerVTable;
typedef struct JPC_ContactListenerVTable
{
// Optional, can be NULL.
JPC_ValidateResult
(*OnContactValidate)(void *in_self,
const JPC_Body *in_body1,
const JPC_Body *in_body2,
const JPC_Real in_base_offset[3],
const JPC_CollideShapeResult *in_collision_result);
// Optional, can be NULL.
void
(*OnContactAdded)(void *in_self,
const JPC_Body *in_body1,
const JPC_Body *in_body2,
const JPC_ContactManifold *in_manifold,
JPC_ContactSettings *io_settings);
// Optional, can be NULL.
void
(*OnContactPersisted)(void *in_self,
const JPC_Body *in_body1,
const JPC_Body *in_body2,
const JPC_ContactManifold *in_manifold,
JPC_ContactSettings *io_settings);