Skip to content

Commit 3b1f716

Browse files
committed
- Use structs for JPC_BodyID and JPC_SubShapeID
- Use non-exhaustive enums for BodyId and SubShapeId - Add activateBodies/deactivateBodies The ID change was motivated by needing to implement toJph for JPC_BodyID, which wasn't possible without making the IDs distinct types.
1 parent e61d7d3 commit 3b1f716

File tree

5 files changed

+184
-96
lines changed

5 files changed

+184
-96
lines changed

libs/JoltC/JoltPhysicsC.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ JPH_SUPPRESS_WARNINGS
6060

6161
#define FN(name) static auto name
6262

63-
FN(toJph)(JPC_BodyID in) { return JPH::BodyID(in); }
64-
FN(toJpc)(JPH::BodyID in) { return in.GetIndexAndSequenceNumber(); }
63+
FN(toJph)(JPC_BodyID in) { return JPH::BodyID(in.id); }
64+
FN(toJpc)(JPH::BodyID in) { return (JPC_BodyID){ in.GetIndexAndSequenceNumber() }; }
6565

6666
FN(toJpc)(const JPH::Body *in) { assert(in); return reinterpret_cast<const JPC_Body *>(in); }
6767
FN(toJph)(const JPC_Body *in) { assert(in); return reinterpret_cast<const JPH::Body *>(in); }
@@ -301,6 +301,7 @@ FN(toJpc)(const JPH::CollisionGroup *in) { assert(in); return reinterpret_cast<c
301301
FN(toJpc)(JPH::CollisionGroup *in) { assert(in); return reinterpret_cast<JPC_CollisionGroup *>(in); }
302302

303303
FN(toJph)(const JPC_SubShapeID *in) { assert(in); return reinterpret_cast<const JPH::SubShapeID *>(in); }
304+
FN(toJph)(const JPC_BodyID *in) { assert(in); return reinterpret_cast<const JPH::BodyID *>(in); }
304305

305306
FN(toJpc)(const JPH::SubShapeIDCreator *in) { assert(in); return reinterpret_cast<const JPC_SubShapeIDCreator *>(in); }
306307
FN(toJph)(const JPC_SubShapeIDCreator *in) { assert(in); return reinterpret_cast<const JPH::SubShapeIDCreator *>(in); }
@@ -2509,12 +2510,24 @@ JPC_BodyInterface_ActivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_i
25092510
toJph(in_iface)->ActivateBody(toJph(in_body_id));
25102511
}
25112512

2513+
JPC_API void
2514+
JPC_BodyInterface_ActivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID *in_body_ids, int in_num_bodies)
2515+
{
2516+
toJph(in_iface)->ActivateBodies(toJph(in_body_ids), in_num_bodies);
2517+
}
2518+
25122519
JPC_API void
25132520
JPC_BodyInterface_DeactivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id)
25142521
{
25152522
toJph(in_iface)->DeactivateBody(toJph(in_body_id));
25162523
}
25172524

2525+
JPC_API void
2526+
JPC_BodyInterface_DeactivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID *in_body_ids, int in_num_bodies)
2527+
{
2528+
toJph(in_iface)->DeactivateBodies(toJph(in_body_ids), in_num_bodies);
2529+
}
2530+
25182531
JPC_API bool
25192532
JPC_BodyInterface_IsActive(const JPC_BodyInterface *in_iface, JPC_BodyID in_body_id)
25202533
{
@@ -2619,7 +2632,7 @@ JPC_BodyInterface_SetObjectLayer(JPC_BodyInterface *in_iface, JPC_BodyID in_body
26192632
JPC_API JPC_BodyID
26202633
JPC_Body_GetID(const JPC_Body *in_body)
26212634
{
2622-
return toJph(in_body)->GetID().GetIndexAndSequenceNumber();
2635+
return (JPC_BodyID){ toJph(in_body)->GetID().GetIndexAndSequenceNumber() };
26232636
}
26242637
//--------------------------------------------------------------------------------------------------
26252638
JPC_API bool
@@ -3198,19 +3211,19 @@ JPC_MotionProperties_SetMaxAngularVelocity(JPC_MotionProperties *in_properties,
31983211
JPC_API uint32_t
31993212
JPC_BodyID_GetIndex(JPC_BodyID in_body_id)
32003213
{
3201-
return JPH::BodyID(in_body_id).GetIndex();
3214+
return JPH::BodyID(in_body_id.id).GetIndex();
32023215
}
32033216
//--------------------------------------------------------------------------------------------------
32043217
JPC_API uint8_t
32053218
JPC_BodyID_GetSequenceNumber(JPC_BodyID in_body_id)
32063219
{
3207-
return JPH::BodyID(in_body_id).GetSequenceNumber();
3220+
return JPH::BodyID(in_body_id.id).GetSequenceNumber();
32083221
}
32093222
//--------------------------------------------------------------------------------------------------
32103223
JPC_API bool
32113224
JPC_BodyID_IsInvalid(JPC_BodyID in_body_id)
32123225
{
3213-
return JPH::BodyID(in_body_id).IsInvalid();
3226+
return JPH::BodyID(in_body_id.id).IsInvalid();
32143227
}
32153228
//--------------------------------------------------------------------------------------------------
32163229
//

libs/JoltC/JoltPhysicsC.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,19 @@ typedef enum JPC_ShapeColor {
292292
typedef uint16_t JPC_ObjectLayer;
293293
typedef uint8_t JPC_BroadPhaseLayer;
294294

295+
typedef struct JPC_BodyID
296+
{
297+
uint32_t id;
298+
} JPC_BodyID;
299+
300+
typedef struct JPC_SubShapeID
301+
{
302+
uint32_t id;
303+
} JPC_SubShapeID;
304+
305+
#define JPC_ID_EQ(a, b) (a.id == b.id)
306+
295307
// TODO: Consider using structures for IDs
296-
typedef uint32_t JPC_BodyID;
297-
typedef uint32_t JPC_SubShapeID;
298308
typedef uint32_t JPC_CollisionGroupID;
299309
typedef uint32_t JPC_CollisionSubGroupID;
300310

@@ -1384,9 +1394,9 @@ JPC_PhysicsSystem_GetActiveBodyIDs(const JPC_PhysicsSystem *in_physics_system,
13841394
/// Access a body, will return NULL if the body ID is no longer valid.
13851395
/// Use `JPC_PhysicsSystem_GetBodiesUnsafe()` to get an array of all body pointers.
13861396
#define JPC_TRY_GET_BODY(all_body_ptrs, body_id) \
1387-
JPC_IS_VALID_BODY_POINTER(all_body_ptrs[body_id & JPC_BODY_ID_INDEX_BITS]) && \
1388-
all_body_ptrs[body_id & JPC_BODY_ID_INDEX_BITS]->id == body_id ? \
1389-
all_body_ptrs[body_id & JPC_BODY_ID_INDEX_BITS] : NULL
1397+
JPC_IS_VALID_BODY_POINTER(all_body_ptrs[body_id.id & JPC_BODY_ID_INDEX_BITS]) && \
1398+
all_body_ptrs[body_id.id & JPC_BODY_ID_INDEX_BITS]->id.id == body_id.id ? \
1399+
all_body_ptrs[body_id.id & JPC_BODY_ID_INDEX_BITS] : NULL
13901400

13911401
/// Get direct access to all bodies. Not protected by a lock. Use with great care!
13921402
JPC_API JPC_Body **
@@ -2062,9 +2072,15 @@ JPC_BodyInterface_SetRotation(JPC_BodyInterface *in_iface,
20622072
JPC_API void
20632073
JPC_BodyInterface_ActivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);
20642074

2075+
JPC_API void
2076+
JPC_BodyInterface_ActivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID* in_body_ids, int in_num_bodies);
2077+
20652078
JPC_API void
20662079
JPC_BodyInterface_DeactivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);
20672080

2081+
JPC_API void
2082+
JPC_BodyInterface_DeactivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID* in_body_ids, int in_num_bodies);
2083+
20682084
JPC_API bool
20692085
JPC_BodyInterface_IsActive(const JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);
20702086

libs/JoltC/JoltPhysicsC_Extensions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ JPC_PhysicsSystem_GetBodyIDs(const JPC_PhysicsSystem *in_physics_system,
6262
for (const JPH::Body *b : physics_system->mBodyManager.mBodies)
6363
if (JPH::BodyManager::sIsValidBodyPointer(b))
6464
{
65-
*out_body_ids = b->GetID().GetIndexAndSequenceNumber();
65+
out_body_ids->id = b->GetID().GetIndexAndSequenceNumber();
6666
out_body_ids += 1;
6767
if (out_num_body_ids) *out_num_body_ids += 1;
6868
in_max_body_ids -= 1;
@@ -91,7 +91,7 @@ JPC_PhysicsSystem_GetActiveBodyIDs(const JPC_PhysicsSystem *in_physics_system,
9191
for (uint32_t i = 0; i < physics_system->mBodyManager.mNumActiveBodies[0]; ++i)
9292
{
9393
const JPH::BodyID body_id = physics_system->mBodyManager.mActiveBodies[0][i];
94-
*out_body_ids = body_id.GetIndexAndSequenceNumber();
94+
out_body_ids->id = body_id.GetIndexAndSequenceNumber();
9595
out_body_ids += 1;
9696
if (out_num_body_ids) *out_num_body_ids += 1;
9797
in_max_body_ids -= 1;

libs/JoltC/JoltPhysicsC_Tests.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,8 @@ JoltCTest_Basic2(void)
541541
JPC_Body *floor = JPC_BodyInterface_CreateBody(body_interface, &floor_settings);
542542
if (floor == NULL) return 0;
543543
const JPC_BodyID floor_id = JPC_Body_GetID(floor);
544-
if (((floor_id & JPC_BODY_ID_SEQUENCE_BITS) >> JPC_BODY_ID_SEQUENCE_SHIFT) != 1) return 0;
545-
if ((floor_id & JPC_BODY_ID_INDEX_BITS) != 0) return 0;
544+
if (((floor_id.id & JPC_BODY_ID_SEQUENCE_BITS) >> JPC_BODY_ID_SEQUENCE_SHIFT) != 1) return 0;
545+
if ((floor_id.id & JPC_BODY_ID_INDEX_BITS) != 0) return 0;
546546
if (JPC_Body_IsStatic(floor) == false) return 0;
547547
if (JPC_Body_IsDynamic(floor) == true) return 0;
548548

@@ -551,8 +551,8 @@ JoltCTest_Basic2(void)
551551
JPC_Body *floor1 = JPC_BodyInterface_CreateBody(body_interface, &floor_settings);
552552
if (floor1 == NULL) return 0;
553553
const JPC_BodyID floor1_id = JPC_Body_GetID(floor1);
554-
if (((floor1_id & JPC_BODY_ID_SEQUENCE_BITS) >> JPC_BODY_ID_SEQUENCE_SHIFT) != 1) return 0;
555-
if ((floor1_id & JPC_BODY_ID_INDEX_BITS) != 1) return 0;
554+
if (((floor1_id.id & JPC_BODY_ID_SEQUENCE_BITS) >> JPC_BODY_ID_SEQUENCE_SHIFT) != 1) return 0;
555+
if ((floor1_id.id & JPC_BODY_ID_INDEX_BITS) != 1) return 0;
556556

557557
if (JPC_BodyInterface_IsAdded(body_interface, floor_id) != false) return 0;
558558
if (JPC_BodyInterface_IsAdded(body_interface, floor1_id) != false) return 0;
@@ -688,8 +688,8 @@ JoltCTest_HelloWorld(void)
688688
uint32_t num_body_ids = 0;
689689
JPC_PhysicsSystem_GetBodyIDs(physics_system, 2, &num_body_ids, &body_ids[0]);
690690
if (num_body_ids != 2) return 0;
691-
if (body_ids[0] != floor_id) return 0;
692-
if (body_ids[1] != sphere_id) return 0;
691+
if (!JPC_ID_EQ(body_ids[0], floor_id)) return 0;
692+
if (!JPC_ID_EQ(body_ids[1], sphere_id)) return 0;
693693
}
694694

695695
// Test JPC_PhysicsSystem_GetActiveBodyIDs()
@@ -698,7 +698,7 @@ JoltCTest_HelloWorld(void)
698698
uint32_t num_body_ids = 0;
699699
JPC_PhysicsSystem_GetActiveBodyIDs(physics_system, 2, &num_body_ids, &body_ids[0]);
700700
if (num_body_ids != 1) return 0;
701-
if (body_ids[0] != sphere_id) return 0;
701+
if (!JPC_ID_EQ(body_ids[0], sphere_id)) return 0;
702702
}
703703

704704
#ifdef PRINT_OUTPUT
@@ -798,7 +798,7 @@ JoltCTest_HelloWorld(void)
798798
JPC_BodyLockInterface_LockRead(lock_iface, sphere_id, &lock);
799799
if (lock.body)
800800
{
801-
JPC_Body *body = bodies[sphere_id & JPC_BODY_ID_INDEX_BITS];
801+
JPC_Body *body = bodies[sphere_id.id & JPC_BODY_ID_INDEX_BITS];
802802
if (!JPC_IS_VALID_BODY_POINTER(body)) return 0;
803803

804804
if (JPC_Body_IsDynamic(body) != true) return 0;
@@ -807,10 +807,10 @@ JoltCTest_HelloWorld(void)
807807
if (body_checked == NULL) return 0;
808808

809809
if (body_checked != body) return 0;
810-
if (body_checked->id != body->id) return 0;
810+
if (!JPC_ID_EQ(body_checked->id, body->id)) return 0;
811811

812812
if (body != lock.body) return 0;
813-
if (body->id != sphere_id) return 0;
813+
if (!JPC_ID_EQ(body->id, sphere_id)) return 0;
814814
}
815815
JPC_BodyLockInterface_UnlockRead(lock_iface, &lock);
816816
}

0 commit comments

Comments
 (0)