Skip to content

Remove usingnamespace, add new interface system, rework IDs, and misc fixes #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Read .zig-version
id: zigversion
uses: juliangruber/read-file-action@v1
with:
path: ./.zigversion
- name: Install Zig
uses: mlugg/setup-zig@v1
with:
version: ${{ steps.zigversion.outputs.content }}
uses: mlugg/setup-zig@v2
- name: Check format
continue-on-error: true
run: zig fmt --check .
Expand Down
1 change: 0 additions & 1 deletion .zigversion

This file was deleted.

53 changes: 30 additions & 23 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const std = @import("std");

fn addMacros(module: *std.Build.Module, options: anytype) void {
if (options.enable_cross_platform_determinism)
module.addCMacro("JPH_CROSS_PLATFORM_DETERMINISTIC", "");
if (options.enable_debug_renderer)
module.addCMacro("JPH_DEBUG_RENDERER", "");
if (options.use_double_precision)
module.addCMacro("JPH_DOUBLE_PRECISION", "");
if (options.enable_asserts)
module.addCMacro("JPH_ENABLE_ASSERTS", "");
}

pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
Expand Down Expand Up @@ -58,21 +69,18 @@ pub fn build(b: *std.Build) void {
});
zjolt.addIncludePath(b.path("libs/JoltC"));

const joltc = if (options.shared) blk: {
const lib = b.addSharedLibrary(.{
.name = "joltc",
const joltc = b.addLibrary(.{
.name = "joltc",
.linkage = if (options.shared) .dynamic else .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
});
if (target.result.os.tag == .windows) {
lib.root_module.addCMacro("JPC_API", "extern __declspec(dllexport)");
}
break :blk lib;
} else b.addStaticLibrary(.{
.name = "joltc",
.target = target,
.optimize = optimize,
}),
});

if (options.shared and target.result.os.tag == .windows)
joltc.root_module.addCMacro("JPC_API", "extern __declspec(dllexport)");

b.installArtifact(joltc);

joltc.addIncludePath(b.path("libs"));
Expand All @@ -87,15 +95,12 @@ pub fn build(b: *std.Build) void {
const src_dir = "libs/Jolt";
const c_flags = &.{
"-std=c++17",
if (options.enable_cross_platform_determinism) "-DJPH_CROSS_PLATFORM_DETERMINISTIC" else "",
if (options.enable_debug_renderer) "-DJPH_DEBUG_RENDERER" else "",
if (options.use_double_precision) "-DJPH_DOUBLE_PRECISION" else "",
if (options.enable_asserts) "-DJPH_ENABLE_ASSERTS" else "",
if (options.no_exceptions) "-fno-exceptions" else "",
"-fno-access-control",
"-fno-sanitize=undefined",
};

addMacros(joltc.root_module, options);
joltc.addCSourceFiles(.{
.files = &.{
"libs/JoltC/JoltPhysicsC.cpp",
Expand Down Expand Up @@ -248,9 +253,11 @@ pub fn build(b: *std.Build) void {

const tests = b.addTest(.{
.name = "zphysics-tests",
.root_source_file = b.path("src/zphysics.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/zphysics.zig"),
.target = target,
.optimize = optimize,
}),
});
b.installArtifact(tests);

Expand All @@ -259,17 +266,17 @@ pub fn build(b: *std.Build) void {
tests.want_lto = false;
}

addMacros(tests.root_module, options);
tests.addCSourceFile(.{
.file = b.path("libs/JoltC/JoltPhysicsC_Tests.c"),
.flags = &.{
if (options.enable_cross_platform_determinism) "-DJPH_CROSS_PLATFORM_DETERMINISTIC" else "",
if (options.enable_debug_renderer) "-DJPH_DEBUG_RENDERER" else "",
if (options.use_double_precision) "-DJPH_DOUBLE_PRECISION" else "",
if (options.enable_asserts) "-DJPH_ENABLE_ASSERTS" else "",
"-fno-sanitize=undefined",
},
});

if (b.option(bool, "verbose", "Print verbose test debug output to stderr") orelse false)
tests.root_module.addCMacro("PRINT_OUTPUT", "");

tests.root_module.addImport("zphysics_options", options_module);
tests.addIncludePath(b.path("libs/JoltC"));
tests.linkLibrary(joltc);
Expand Down
1 change: 1 addition & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.name = .zphysics,
.fingerprint = 0x1def6aac00c4909d,
.version = "0.2.0-dev",
.minimum_zig_version = "0.14.0",
.paths = .{
"build.zig",
"build.zig.zon",
Expand Down
30 changes: 22 additions & 8 deletions libs/JoltC/JoltPhysicsC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ JPH_SUPPRESS_WARNINGS

#define FN(name) static auto name

FN(toJph)(JPC_BodyID in) { return JPH::BodyID(in); }
FN(toJpc)(JPH::BodyID in) { return in.GetIndexAndSequenceNumber(); }
FN(toJph)(JPC_BodyID in) { return JPH::BodyID(in.id); }
FN(toJpc)(JPH::BodyID in) { return (JPC_BodyID){ in.GetIndexAndSequenceNumber() }; }

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

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

FN(toJpc)(const JPH::SubShapeIDCreator *in) { assert(in); return reinterpret_cast<const JPC_SubShapeIDCreator *>(in); }
FN(toJph)(const JPC_SubShapeIDCreator *in) { assert(in); return reinterpret_cast<const JPH::SubShapeIDCreator *>(in); }
Expand Down Expand Up @@ -738,9 +739,9 @@ class DebugRendererImpl final : public JPH::DebugRenderer
BatchImpl(const JPC_DebugRenderer_Primitive *c_primitive) : RenderPrimitive(c_primitive) {}
~BatchImpl()
{
if (sInstance && sInstance->c_renderer->vtbl->DestroyTriangleBatch)
if (sInstance)
{
sInstance->c_renderer->vtbl->DestroyTriangleBatch(sInstance->c_renderer, c_primitive);
sInstance->c_renderer->vtbl->DestroyTriangleBatch(sInstance->c_renderer, (void*)c_primitive);
}
}

Expand Down Expand Up @@ -789,6 +790,7 @@ class DebugRendererImpl final : public JPH::DebugRenderer
valid &= (c_renderer->vtbl->DrawTriangle != nullptr);
valid &= (c_renderer->vtbl->CreateTriangleBatch != nullptr);
valid &= (c_renderer->vtbl->CreateTriangleBatchIndexed != nullptr);
valid &= (c_renderer->vtbl->DestroyTriangleBatch != nullptr);
valid &= (c_renderer->vtbl->DrawGeometry != nullptr);
valid &= (c_renderer->vtbl->DrawText3D != nullptr);
return valid ? JPC_DEBUGRENDERER_SUCCESS : JPC_DEBUGRENDERER_INCOMPLETE_IMPL;
Expand Down Expand Up @@ -2509,12 +2511,24 @@ JPC_BodyInterface_ActivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_i
toJph(in_iface)->ActivateBody(toJph(in_body_id));
}

JPC_API void
JPC_BodyInterface_ActivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID *in_body_ids, int in_num_bodies)
{
toJph(in_iface)->ActivateBodies(toJph(in_body_ids), in_num_bodies);
}

JPC_API void
JPC_BodyInterface_DeactivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id)
{
toJph(in_iface)->DeactivateBody(toJph(in_body_id));
}

JPC_API void
JPC_BodyInterface_DeactivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID *in_body_ids, int in_num_bodies)
{
toJph(in_iface)->DeactivateBodies(toJph(in_body_ids), in_num_bodies);
}

JPC_API bool
JPC_BodyInterface_IsActive(const JPC_BodyInterface *in_iface, JPC_BodyID in_body_id)
{
Expand Down Expand Up @@ -2619,7 +2633,7 @@ JPC_BodyInterface_SetObjectLayer(JPC_BodyInterface *in_iface, JPC_BodyID in_body
JPC_API JPC_BodyID
JPC_Body_GetID(const JPC_Body *in_body)
{
return toJph(in_body)->GetID().GetIndexAndSequenceNumber();
return (JPC_BodyID){ toJph(in_body)->GetID().GetIndexAndSequenceNumber() };
}
//--------------------------------------------------------------------------------------------------
JPC_API bool
Expand Down Expand Up @@ -3198,19 +3212,19 @@ JPC_MotionProperties_SetMaxAngularVelocity(JPC_MotionProperties *in_properties,
JPC_API uint32_t
JPC_BodyID_GetIndex(JPC_BodyID in_body_id)
{
return JPH::BodyID(in_body_id).GetIndex();
return JPH::BodyID(in_body_id.id).GetIndex();
}
//--------------------------------------------------------------------------------------------------
JPC_API uint8_t
JPC_BodyID_GetSequenceNumber(JPC_BodyID in_body_id)
{
return JPH::BodyID(in_body_id).GetSequenceNumber();
return JPH::BodyID(in_body_id.id).GetSequenceNumber();
}
//--------------------------------------------------------------------------------------------------
JPC_API bool
JPC_BodyID_IsInvalid(JPC_BodyID in_body_id)
{
return JPH::BodyID(in_body_id).IsInvalid();
return JPH::BodyID(in_body_id.id).IsInvalid();
}
//--------------------------------------------------------------------------------------------------
//
Expand Down
30 changes: 22 additions & 8 deletions libs/JoltC/JoltPhysicsC.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,19 @@ typedef enum JPC_ShapeColor {
typedef uint16_t JPC_ObjectLayer;
typedef uint8_t JPC_BroadPhaseLayer;

typedef struct JPC_BodyID
{
uint32_t id;
} JPC_BodyID;

typedef struct JPC_SubShapeID
{
uint32_t id;
} JPC_SubShapeID;

#define JPC_ID_EQ(a, b) (a.id == b.id)

Comment on lines +305 to +306
Copy link
Preview

Copilot AI Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro JPC_ID_EQ assumes both parameters have an 'id' field but lacks type safety. Consider using inline functions with proper parameter types to ensure type safety and provide better error messages when used incorrectly.

Suggested change
#define JPC_ID_EQ(a, b) (a.id == b.id)
static inline bool JPC_ID_EQ(JPC_BodyID a, JPC_BodyID b) {
return a.id == b.id;
}
static inline bool JPC_SubShapeID_EQ(JPC_SubShapeID a, JPC_SubShapeID b) {
return a.id == b.id;
}

Copilot uses AI. Check for mistakes.

// 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;

Expand Down Expand Up @@ -985,8 +995,6 @@ typedef struct JPC_CharacterContactListenerVTable

typedef struct JPC_ContactListenerVTable
{
_JPC_VTABLE_HEADER;

// Optional, can be NULL.
JPC_ValidateResult
(*OnContactValidate)(void *in_self,
Expand Down Expand Up @@ -1050,7 +1058,7 @@ typedef struct JPC_DebugRendererVTable

// Optional
void
(*DestroyTriangleBatch)(void *in_self, const void *in_primitive);
(*DestroyTriangleBatch)(void *in_self, void *in_primitive);

// Required, *cannot* be NULL.
void
Expand Down Expand Up @@ -1384,9 +1392,9 @@ JPC_PhysicsSystem_GetActiveBodyIDs(const JPC_PhysicsSystem *in_physics_system,
/// Access a body, will return NULL if the body ID is no longer valid.
/// Use `JPC_PhysicsSystem_GetBodiesUnsafe()` to get an array of all body pointers.
#define JPC_TRY_GET_BODY(all_body_ptrs, body_id) \
JPC_IS_VALID_BODY_POINTER(all_body_ptrs[body_id & JPC_BODY_ID_INDEX_BITS]) && \
all_body_ptrs[body_id & JPC_BODY_ID_INDEX_BITS]->id == body_id ? \
all_body_ptrs[body_id & JPC_BODY_ID_INDEX_BITS] : NULL
JPC_IS_VALID_BODY_POINTER(all_body_ptrs[body_id.id & JPC_BODY_ID_INDEX_BITS]) && \
all_body_ptrs[body_id.id & JPC_BODY_ID_INDEX_BITS]->id.id == body_id.id ? \
all_body_ptrs[body_id.id & JPC_BODY_ID_INDEX_BITS] : NULL

/// Get direct access to all bodies. Not protected by a lock. Use with great care!
JPC_API JPC_Body **
Expand Down Expand Up @@ -2062,9 +2070,15 @@ JPC_BodyInterface_SetRotation(JPC_BodyInterface *in_iface,
JPC_API void
JPC_BodyInterface_ActivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);

JPC_API void
JPC_BodyInterface_ActivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID* in_body_ids, int in_num_bodies);

JPC_API void
JPC_BodyInterface_DeactivateBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);

JPC_API void
JPC_BodyInterface_DeactivateBodies(JPC_BodyInterface *in_iface, const JPC_BodyID* in_body_ids, int in_num_bodies);

JPC_API bool
JPC_BodyInterface_IsActive(const JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);

Expand Down
4 changes: 2 additions & 2 deletions libs/JoltC/JoltPhysicsC_Extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ JPC_PhysicsSystem_GetBodyIDs(const JPC_PhysicsSystem *in_physics_system,
for (const JPH::Body *b : physics_system->mBodyManager.mBodies)
if (JPH::BodyManager::sIsValidBodyPointer(b))
{
*out_body_ids = b->GetID().GetIndexAndSequenceNumber();
out_body_ids->id = b->GetID().GetIndexAndSequenceNumber();
out_body_ids += 1;
if (out_num_body_ids) *out_num_body_ids += 1;
in_max_body_ids -= 1;
Expand Down Expand Up @@ -91,7 +91,7 @@ JPC_PhysicsSystem_GetActiveBodyIDs(const JPC_PhysicsSystem *in_physics_system,
for (uint32_t i = 0; i < physics_system->mBodyManager.mNumActiveBodies[0]; ++i)
{
const JPH::BodyID body_id = physics_system->mBodyManager.mActiveBodies[0][i];
*out_body_ids = body_id.GetIndexAndSequenceNumber();
out_body_ids->id = body_id.GetIndexAndSequenceNumber();
out_body_ids += 1;
if (out_num_body_ids) *out_num_body_ids += 1;
in_max_body_ids -= 1;
Expand Down
Loading
Loading