Skip to content

Add JoltPhysics Vehicle C bindings and Zig wrappers#22

Open
agrapine wants to merge 1 commit into
zig-gamedev:mainfrom
swipelab:feature/vehicle-bindings
Open

Add JoltPhysics Vehicle C bindings and Zig wrappers#22
agrapine wants to merge 1 commit into
zig-gamedev:mainfrom
swipelab:feature/vehicle-bindings

Conversation

@agrapine
Copy link
Copy Markdown

Summary

  • Add C bindings for JoltPhysics Vehicle system
  • Add Zig wrapper types following existing patterns
  • Enables wheeled vehicle physics with suspension, steering, drivetrain

New Types

  • VehicleConstraint / VehicleConstraintSettings
  • WheelSettings / WheelSettingsWV
  • WheeledVehicleController / WheeledVehicleControllerSettings
  • VehicleCollisionTester (ray and sphere variants)
  • Wheel (runtime wheel state)

Key Functions

  • Vehicle creation and configuration
  • Driver input (throttle, steering, brake, handbrake)
  • Wheel world transforms for rendering
  • Engine RPM and gear state queries
  • Step listener registration

- Add C bindings for VehicleConstraint, WheelSettings, WheeledVehicleController
- Add VehicleCollisionTester (ray and sphere-cast variants)
- Add Zig opaque type wrappers following existing patterns
- Add vehicle-specific step listener functions (handles multiple inheritance)
- Proper ref counting for settings objects (AddRef/Release)
@Srekel
Copy link
Copy Markdown
Member

Srekel commented Jan 29, 2026

Skimmed through it and looks fine, but I'm not sure if zig-gamedev has a stance on the use of auto. :)

@agrapine
Copy link
Copy Markdown
Author

Skimmed through it and looks fine, but I'm not sure if zig-gamedev has a stance on the use of auto. :)

are you referring to the c++ "auto" keyword? that would be quite strange

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds comprehensive support for the JoltPhysics vehicle system by introducing C bindings and corresponding Zig wrappers. The implementation enables wheeled vehicle physics with suspension, steering, and drivetrain simulation capabilities.

Changes:

  • Added C bindings for vehicle-related types including constraints, controllers, wheels, and collision testers
  • Implemented Zig wrapper types following the established patterns for memory management and API design
  • Added vehicle-specific step listener registration functions to handle multiple inheritance correctly

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
libs/JoltC/JoltPhysicsC.h Declares vehicle-related C types, enums, and API functions for the vehicle physics system
libs/JoltC/JoltPhysicsC.cpp Implements C bindings with type conversions, memory management, and JoltPhysics integration
src/zphysics.zig Provides idiomatic Zig wrappers for vehicle types with error handling and type safety

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

JPC_API JPC_Wheel *
JPC_VehicleConstraint_GetWheel(JPC_VehicleConstraint *in_constraint, uint32_t in_index)
{
return toJpc(toJph(in_constraint)->GetWheels()[in_index]);
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

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

Missing bounds checking before array access. The function directly accesses GetWheels()[in_index] without verifying that in_index is less than the number of wheels. This could lead to out-of-bounds access and undefined behavior if an invalid index is passed. Consider adding a bounds check similar to how other indexed access operations are typically validated, or document that the caller is responsible for ensuring the index is valid.

Suggested change
return toJpc(toJph(in_constraint)->GetWheels()[in_index]);
auto *constraint = toJph(in_constraint);
auto &wheels = constraint->GetWheels();
if (in_index >= static_cast<uint32_t>(wheels.size()))
return nullptr;
return toJpc(wheels[in_index]);

Copilot uses AI. Check for mistakes.
JPC_VehicleConstraint_GetController(JPC_VehicleConstraint *in_constraint)
{
JPH::VehicleController *controller = toJph(in_constraint)->GetController();
return toJpc(static_cast<JPH::WheeledVehicleController *>(controller));
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

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

Unsafe type cast without validation. The function casts the base VehicleController pointer to WheeledVehicleController without checking if the controller is actually of this type. While the current codebase may only use wheeled vehicles, this creates a potential type safety issue. If other vehicle controller types are added in the future, this could lead to undefined behavior. Consider either: 1) Adding runtime type checking before the cast, 2) Renaming the function to indicate it assumes a wheeled vehicle controller, or 3) Documenting this assumption clearly.

Suggested change
return toJpc(static_cast<JPH::WheeledVehicleController *>(controller));
if (controller == nullptr)
return nullptr;
JPH::WheeledVehicleController *wheeled_controller = dynamic_cast<JPH::WheeledVehicleController *>(controller);
assert(wheeled_controller != nullptr);
if (wheeled_controller == nullptr)
return nullptr;
return toJpc(wheeled_controller);

Copilot uses AI. Check for mistakes.
@zig-gamedev zig-gamedev deleted a comment from Copilot AI Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants