diff --git a/libs/JoltC/JoltPhysicsC.cpp b/libs/JoltC/JoltPhysicsC.cpp index 73be6c5..0c1307d 100644 --- a/libs/JoltC/JoltPhysicsC.cpp +++ b/libs/JoltC/JoltPhysicsC.cpp @@ -38,6 +38,10 @@ #include #include #include +#include +#include +#include +#include #if JPC_DEBUG_RENDERER == 1 #include @@ -426,6 +430,52 @@ FN(toJph)(JPC_CharacterVirtualSettings *in) { assert(in); return reinterpret_cas FN(toJpc)(const JPH::CharacterVirtualSettings *in) { assert(in); return reinterpret_cast(in); } FN(toJpc)(JPH::CharacterVirtualSettings *in) { assert(in); return reinterpret_cast(in); } +// Vehicle types +FN(toJph)(const JPC_VehicleConstraintSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJph)(JPC_VehicleConstraintSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(const JPH::VehicleConstraintSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(JPH::VehicleConstraintSettings *in) { assert(in); return reinterpret_cast(in); } + +FN(toJph)(const JPC_VehicleConstraint *in) { assert(in); return reinterpret_cast(in); } +FN(toJph)(JPC_VehicleConstraint *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(const JPH::VehicleConstraint *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(JPH::VehicleConstraint *in) { assert(in); return reinterpret_cast(in); } + +FN(toJph)(const JPC_WheelSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJph)(JPC_WheelSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(const JPH::WheelSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(JPH::WheelSettings *in) { assert(in); return reinterpret_cast(in); } + +FN(toJph)(const JPC_WheelSettingsWV *in) { assert(in); return reinterpret_cast(in); } +FN(toJph)(JPC_WheelSettingsWV *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(const JPH::WheelSettingsWV *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(JPH::WheelSettingsWV *in) { assert(in); return reinterpret_cast(in); } + +FN(toJph)(const JPC_Wheel *in) { assert(in); return reinterpret_cast(in); } +FN(toJph)(JPC_Wheel *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(const JPH::Wheel *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(JPH::Wheel *in) { assert(in); return reinterpret_cast(in); } + +FN(toJph)(const JPC_WheeledVehicleControllerSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJph)(JPC_WheeledVehicleControllerSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(const JPH::WheeledVehicleControllerSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(JPH::WheeledVehicleControllerSettings *in) { assert(in); return reinterpret_cast(in); } + +FN(toJph)(const JPC_WheeledVehicleController *in) { assert(in); return reinterpret_cast(in); } +FN(toJph)(JPC_WheeledVehicleController *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(const JPH::WheeledVehicleController *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(JPH::WheeledVehicleController *in) { assert(in); return reinterpret_cast(in); } + +FN(toJph)(const JPC_VehicleCollisionTester *in) { assert(in); return reinterpret_cast(in); } +FN(toJph)(JPC_VehicleCollisionTester *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(const JPH::VehicleCollisionTester *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(JPH::VehicleCollisionTester *in) { assert(in); return reinterpret_cast(in); } + +FN(toJph)(const JPC_VehicleControllerSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJph)(JPC_VehicleControllerSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(const JPH::VehicleControllerSettings *in) { assert(in); return reinterpret_cast(in); } +FN(toJpc)(JPH::VehicleControllerSettings *in) { assert(in); return reinterpret_cast(in); } + FN(toJpc)(const JPH::AABox *in) { assert(in); return reinterpret_cast(in); } FN(toJph)(const JPC_AABox *in) { assert(in); return reinterpret_cast(in); } FN(toJpc)(JPH::AABox *in) { assert(in); return reinterpret_cast(in); } @@ -1151,6 +1201,24 @@ JPC_PhysicsSystem_RemoveStepListener(JPC_PhysicsSystem *in_physics_system, void } //-------------------------------------------------------------------------------------------------- JPC_API void +JPC_PhysicsSystem_AddVehicleStepListener(JPC_PhysicsSystem *in_physics_system, JPC_VehicleConstraint *in_vehicle) +{ + assert(in_vehicle != nullptr); + // VehicleConstraint has multiple inheritance (Constraint, PhysicsStepListener) + // C++ handles the pointer adjustment correctly when we have the proper type + JPH::VehicleConstraint *vehicle = toJph(in_vehicle); + toJph(in_physics_system)->AddStepListener(vehicle); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_PhysicsSystem_RemoveVehicleStepListener(JPC_PhysicsSystem *in_physics_system, JPC_VehicleConstraint *in_vehicle) +{ + assert(in_vehicle != nullptr); + JPH::VehicleConstraint *vehicle = toJph(in_vehicle); + toJph(in_physics_system)->RemoveStepListener(vehicle); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void JPC_PhysicsSystem_AddConstraint(JPC_PhysicsSystem *in_physics_system, JPC_Constraint *in_constraint) { assert(in_constraint != nullptr); @@ -3525,3 +3593,537 @@ JPC_CharacterVirtual_SetLinearVelocity(JPC_CharacterVirtual *in_character, const toJph(in_character)->SetLinearVelocity(loadVec3(in_linear_velocity)); } //-------------------------------------------------------------------------------------------------- +// +// JPC_WheelSettings +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_WheelSettings * +JPC_WheelSettings_Create() +{ + auto settings = new JPH::WheelSettings(); + settings->AddRef(); + return toJpc(settings); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_Release(JPC_WheelSettings *in_settings) +{ + toJph(in_settings)->Release(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetPosition(JPC_WheelSettings *in_settings, const float in_position[3]) +{ + toJph(in_settings)->mPosition = loadVec3(in_position); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetSuspensionDirection(JPC_WheelSettings *in_settings, const float in_direction[3]) +{ + toJph(in_settings)->mSuspensionDirection = loadVec3(in_direction); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetSteeringAxis(JPC_WheelSettings *in_settings, const float in_axis[3]) +{ + toJph(in_settings)->mSteeringAxis = loadVec3(in_axis); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetWheelUp(JPC_WheelSettings *in_settings, const float in_up[3]) +{ + toJph(in_settings)->mWheelUp = loadVec3(in_up); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetWheelForward(JPC_WheelSettings *in_settings, const float in_forward[3]) +{ + toJph(in_settings)->mWheelForward = loadVec3(in_forward); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetSuspensionMinLength(JPC_WheelSettings *in_settings, float in_length) +{ + toJph(in_settings)->mSuspensionMinLength = in_length; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetSuspensionMaxLength(JPC_WheelSettings *in_settings, float in_length) +{ + toJph(in_settings)->mSuspensionMaxLength = in_length; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetSuspensionPreloadLength(JPC_WheelSettings *in_settings, float in_length) +{ + toJph(in_settings)->mSuspensionPreloadLength = in_length; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetSuspensionSpring(JPC_WheelSettings *in_settings, float in_frequency, float in_damping) +{ + toJph(in_settings)->mSuspensionSpring.mFrequency = in_frequency; + toJph(in_settings)->mSuspensionSpring.mDamping = in_damping; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetRadius(JPC_WheelSettings *in_settings, float in_radius) +{ + toJph(in_settings)->mRadius = in_radius; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettings_SetWidth(JPC_WheelSettings *in_settings, float in_width) +{ + toJph(in_settings)->mWidth = in_width; +} +//-------------------------------------------------------------------------------------------------- +// +// JPC_WheelSettingsWV (-> JPC_WheelSettings) +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_WheelSettingsWV * +JPC_WheelSettingsWV_Create() +{ + auto settings = new JPH::WheelSettingsWV(); + settings->AddRef(); + return toJpc(settings); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettingsWV_Release(JPC_WheelSettingsWV *in_settings) +{ + toJph(in_settings)->Release(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_WheelSettings * +JPC_WheelSettingsWV_AsWheelSettings(JPC_WheelSettingsWV *in_settings) +{ + return toJpc(static_cast(toJph(in_settings))); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettingsWV_SetInertia(JPC_WheelSettingsWV *in_settings, float in_inertia) +{ + toJph(in_settings)->mInertia = in_inertia; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettingsWV_SetAngularDamping(JPC_WheelSettingsWV *in_settings, float in_damping) +{ + toJph(in_settings)->mAngularDamping = in_damping; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettingsWV_SetMaxSteerAngle(JPC_WheelSettingsWV *in_settings, float in_angle) +{ + toJph(in_settings)->mMaxSteerAngle = in_angle; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettingsWV_SetMaxBrakeTorque(JPC_WheelSettingsWV *in_settings, float in_torque) +{ + toJph(in_settings)->mMaxBrakeTorque = in_torque; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheelSettingsWV_SetMaxHandBrakeTorque(JPC_WheelSettingsWV *in_settings, float in_torque) +{ + toJph(in_settings)->mMaxHandBrakeTorque = in_torque; +} +//-------------------------------------------------------------------------------------------------- +// +// JPC_WheeledVehicleControllerSettings (-> JPC_VehicleControllerSettings) +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_WheeledVehicleControllerSettings * +JPC_WheeledVehicleControllerSettings_Create() +{ + auto settings = new JPH::WheeledVehicleControllerSettings(); + settings->AddRef(); + return toJpc(settings); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_Release(JPC_WheeledVehicleControllerSettings *in_settings) +{ + toJph(in_settings)->Release(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_VehicleControllerSettings * +JPC_WheeledVehicleControllerSettings_AsVehicleControllerSettings(JPC_WheeledVehicleControllerSettings *in_settings) +{ + return toJpc(static_cast(toJph(in_settings))); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_AddDifferential(JPC_WheeledVehicleControllerSettings *in_settings, + const JPC_VehicleDifferentialSettings *in_differential) +{ + JPH::VehicleDifferentialSettings diff; + diff.mLeftWheel = in_differential->left_wheel; + diff.mRightWheel = in_differential->right_wheel; + diff.mDifferentialRatio = in_differential->differential_ratio; + diff.mLeftRightSplit = in_differential->left_right_split; + diff.mLimitedSlipRatio = in_differential->limited_slip_ratio; + diff.mEngineTorqueRatio = in_differential->engine_torque_ratio; + toJph(in_settings)->mDifferentials.push_back(diff); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetEngineMaxTorque(JPC_WheeledVehicleControllerSettings *in_settings, float in_torque) +{ + toJph(in_settings)->mEngine.mMaxTorque = in_torque; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetEngineMinRPM(JPC_WheeledVehicleControllerSettings *in_settings, float in_rpm) +{ + toJph(in_settings)->mEngine.mMinRPM = in_rpm; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetEngineMaxRPM(JPC_WheeledVehicleControllerSettings *in_settings, float in_rpm) +{ + toJph(in_settings)->mEngine.mMaxRPM = in_rpm; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionMode(JPC_WheeledVehicleControllerSettings *in_settings, + JPC_ETransmissionMode in_mode) +{ + toJph(in_settings)->mTransmission.mMode = static_cast(in_mode); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionGearRatios(JPC_WheeledVehicleControllerSettings *in_settings, + const float *in_ratios, + uint32_t in_count) +{ + toJph(in_settings)->mTransmission.mGearRatios.clear(); + for (uint32_t i = 0; i < in_count; ++i) { + toJph(in_settings)->mTransmission.mGearRatios.push_back(in_ratios[i]); + } +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionReverseGearRatios(JPC_WheeledVehicleControllerSettings *in_settings, + const float *in_ratios, + uint32_t in_count) +{ + toJph(in_settings)->mTransmission.mReverseGearRatios.clear(); + for (uint32_t i = 0; i < in_count; ++i) { + toJph(in_settings)->mTransmission.mReverseGearRatios.push_back(in_ratios[i]); + } +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionSwitchTime(JPC_WheeledVehicleControllerSettings *in_settings, float in_time) +{ + toJph(in_settings)->mTransmission.mSwitchTime = in_time; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionClutchStrength(JPC_WheeledVehicleControllerSettings *in_settings, float in_strength) +{ + toJph(in_settings)->mTransmission.mClutchStrength = in_strength; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionShiftUpRPM(JPC_WheeledVehicleControllerSettings *in_settings, float in_rpm) +{ + toJph(in_settings)->mTransmission.mShiftUpRPM = in_rpm; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionShiftDownRPM(JPC_WheeledVehicleControllerSettings *in_settings, float in_rpm) +{ + toJph(in_settings)->mTransmission.mShiftDownRPM = in_rpm; +} +//-------------------------------------------------------------------------------------------------- +// +// JPC_VehicleCollisionTester +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_VehicleCollisionTester * +JPC_VehicleCollisionTesterRay_Create(JPC_ObjectLayer in_object_layer, const float in_up[3], float in_max_slope_angle) +{ + auto tester = new JPH::VehicleCollisionTesterRay(in_object_layer, loadVec3(in_up), in_max_slope_angle); + tester->AddRef(); + return toJpc(static_cast(tester)); +} +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_VehicleCollisionTester * +JPC_VehicleCollisionTesterCastSphere_Create(JPC_ObjectLayer in_object_layer, float in_radius, const float in_up[3], float in_max_slope_angle) +{ + auto tester = new JPH::VehicleCollisionTesterCastSphere(in_object_layer, in_radius, loadVec3(in_up), in_max_slope_angle); + tester->AddRef(); + return toJpc(static_cast(tester)); +} +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_VehicleCollisionTester * +JPC_VehicleCollisionTesterCastCylinder_Create(JPC_ObjectLayer in_object_layer, float in_convex_radius_fraction) +{ + auto tester = new JPH::VehicleCollisionTesterCastCylinder(in_object_layer, in_convex_radius_fraction); + tester->AddRef(); + return toJpc(static_cast(tester)); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleCollisionTester_AddRef(JPC_VehicleCollisionTester *in_tester) +{ + toJph(in_tester)->AddRef(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleCollisionTester_Release(JPC_VehicleCollisionTester *in_tester) +{ + toJph(in_tester)->Release(); +} +//-------------------------------------------------------------------------------------------------- +// +// JPC_VehicleConstraintSettings (-> JPC_ConstraintSettings) +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_VehicleConstraintSettings * +JPC_VehicleConstraintSettings_Create() +{ + auto settings = new JPH::VehicleConstraintSettings(); + settings->AddRef(); + return toJpc(settings); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraintSettings_Release(JPC_VehicleConstraintSettings *in_settings) +{ + toJph(in_settings)->Release(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_ConstraintSettings * +JPC_VehicleConstraintSettings_AsConstraintSettings(JPC_VehicleConstraintSettings *in_settings) +{ + return toJpc(static_cast(toJph(in_settings))); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraintSettings_SetUp(JPC_VehicleConstraintSettings *in_settings, const float in_up[3]) +{ + toJph(in_settings)->mUp = loadVec3(in_up); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraintSettings_SetForward(JPC_VehicleConstraintSettings *in_settings, const float in_forward[3]) +{ + toJph(in_settings)->mForward = loadVec3(in_forward); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraintSettings_SetMaxPitchRollAngle(JPC_VehicleConstraintSettings *in_settings, float in_angle) +{ + toJph(in_settings)->mMaxPitchRollAngle = in_angle; +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraintSettings_AddWheel(JPC_VehicleConstraintSettings *in_settings, JPC_WheelSettings *in_wheel) +{ + toJph(in_settings)->mWheels.push_back(toJph(in_wheel)); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraintSettings_AddAntiRollBar(JPC_VehicleConstraintSettings *in_settings, + const JPC_VehicleAntiRollBarSettings *in_anti_roll_bar) +{ + JPH::VehicleAntiRollBar arb; + arb.mLeftWheel = in_anti_roll_bar->left_wheel; + arb.mRightWheel = in_anti_roll_bar->right_wheel; + arb.mStiffness = in_anti_roll_bar->stiffness; + toJph(in_settings)->mAntiRollBars.push_back(arb); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraintSettings_SetController(JPC_VehicleConstraintSettings *in_settings, + JPC_VehicleControllerSettings *in_controller) +{ + toJph(in_settings)->mController = toJph(in_controller); +} +//-------------------------------------------------------------------------------------------------- +// +// JPC_VehicleConstraint (-> JPC_Constraint) +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_VehicleConstraint * +JPC_VehicleConstraint_Create(JPC_Body *in_body, const JPC_VehicleConstraintSettings *in_settings) +{ + auto constraint = new JPH::VehicleConstraint(*toJph(in_body), *toJph(in_settings)); + constraint->AddRef(); + return toJpc(constraint); +} +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_Constraint * +JPC_VehicleConstraint_AsConstraint(JPC_VehicleConstraint *in_constraint) +{ + return toJpc(static_cast(toJph(in_constraint))); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraint_SetVehicleCollisionTester(JPC_VehicleConstraint *in_constraint, + JPC_VehicleCollisionTester *in_tester) +{ + toJph(in_constraint)->SetVehicleCollisionTester(toJph(in_tester)); +} +//-------------------------------------------------------------------------------------------------- +JPC_API uint32_t +JPC_VehicleConstraint_GetNumWheels(const JPC_VehicleConstraint *in_constraint) +{ + return static_cast(toJph(in_constraint)->GetWheels().size()); +} +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_Wheel * +JPC_VehicleConstraint_GetWheel(JPC_VehicleConstraint *in_constraint, uint32_t in_index) +{ + return toJpc(toJph(in_constraint)->GetWheels()[in_index]); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraint_GetWheelLocalTransform(const JPC_VehicleConstraint *in_constraint, + uint32_t in_wheel_index, + const float in_wheel_right[3], + const float in_wheel_up[3], + float out_position[3], + float out_rotation[4]) +{ + JPH::RMat44 transform = toJph(in_constraint)->GetWheelLocalTransform( + in_wheel_index, + loadVec3(in_wheel_right), + loadVec3(in_wheel_up) + ); + storeVec3(out_position, transform.GetTranslation()); + storeVec4(out_rotation, transform.GetQuaternion().GetXYZW()); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_VehicleConstraint_GetWheelWorldTransform(const JPC_VehicleConstraint *in_constraint, + uint32_t in_wheel_index, + const float in_wheel_right[3], + const float in_wheel_up[3], + float out_position[3], + float out_rotation[4]) +{ + JPH::RMat44 transform = toJph(in_constraint)->GetWheelWorldTransform( + in_wheel_index, + loadVec3(in_wheel_right), + loadVec3(in_wheel_up) + ); + storeVec3(out_position, transform.GetTranslation()); + storeVec4(out_rotation, transform.GetQuaternion().GetXYZW()); +} +//-------------------------------------------------------------------------------------------------- +// +// JPC_WheeledVehicleController +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_WheeledVehicleController * +JPC_VehicleConstraint_GetController(JPC_VehicleConstraint *in_constraint) +{ + JPH::VehicleController *controller = toJph(in_constraint)->GetController(); + return toJpc(static_cast(controller)); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_WheeledVehicleController_SetDriverInput(JPC_WheeledVehicleController *in_controller, + float in_forward, + float in_right, + float in_brake, + float in_hand_brake) +{ + toJph(in_controller)->SetDriverInput(in_forward, in_right, in_brake, in_hand_brake); +} +//-------------------------------------------------------------------------------------------------- +JPC_API float +JPC_WheeledVehicleController_GetEngineRotationSpeed(const JPC_WheeledVehicleController *in_controller) +{ + return toJph(in_controller)->GetEngine().GetCurrentRPM(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API int32_t +JPC_WheeledVehicleController_GetTransmissionCurrentGear(const JPC_WheeledVehicleController *in_controller) +{ + return toJph(in_controller)->GetTransmission().GetCurrentGear(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API float +JPC_WheeledVehicleController_GetTransmissionClutchFriction(const JPC_WheeledVehicleController *in_controller) +{ + return toJph(in_controller)->GetTransmission().GetClutchFriction(); +} +//-------------------------------------------------------------------------------------------------- +// +// JPC_Wheel +// +//-------------------------------------------------------------------------------------------------- +JPC_API bool +JPC_Wheel_HasContact(const JPC_Wheel *in_wheel) +{ + return toJph(in_wheel)->HasContact(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_BodyID +JPC_Wheel_GetContactBodyID(const JPC_Wheel *in_wheel) +{ + return toJpc(toJph(in_wheel)->GetContactBodyID()); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_Wheel_GetContactPosition(const JPC_Wheel *in_wheel, float out_position[3]) +{ + storeRVec3(out_position, toJph(in_wheel)->GetContactPosition()); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_Wheel_GetContactNormal(const JPC_Wheel *in_wheel, float out_normal[3]) +{ + storeVec3(out_normal, toJph(in_wheel)->GetContactNormal()); +} +//-------------------------------------------------------------------------------------------------- +JPC_API float +JPC_Wheel_GetSuspensionLength(const JPC_Wheel *in_wheel) +{ + return toJph(in_wheel)->GetSuspensionLength(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API float +JPC_Wheel_GetAngularVelocity(const JPC_Wheel *in_wheel) +{ + return toJph(in_wheel)->GetAngularVelocity(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_Wheel_SetAngularVelocity(JPC_Wheel *in_wheel, float in_velocity) +{ + toJph(in_wheel)->SetAngularVelocity(in_velocity); +} +//-------------------------------------------------------------------------------------------------- +JPC_API float +JPC_Wheel_GetRotationAngle(const JPC_Wheel *in_wheel) +{ + return toJph(in_wheel)->GetRotationAngle(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_Wheel_SetRotationAngle(JPC_Wheel *in_wheel, float in_angle) +{ + toJph(in_wheel)->SetRotationAngle(in_angle); +} +//-------------------------------------------------------------------------------------------------- +JPC_API float +JPC_Wheel_GetSteerAngle(const JPC_Wheel *in_wheel) +{ + return toJph(in_wheel)->GetSteerAngle(); +} +//-------------------------------------------------------------------------------------------------- +JPC_API void +JPC_Wheel_SetSteerAngle(JPC_Wheel *in_wheel, float in_angle) +{ + toJph(in_wheel)->SetSteerAngle(in_angle); +} +//-------------------------------------------------------------------------------------------------- diff --git a/libs/JoltC/JoltPhysicsC.h b/libs/JoltC/JoltPhysicsC.h index d6716f7..f17070e 100644 --- a/libs/JoltC/JoltPhysicsC.h +++ b/libs/JoltC/JoltPhysicsC.h @@ -373,6 +373,19 @@ typedef struct JPC_GroupFilter JPC_GroupFilter; typedef struct JPC_Character JPC_Character; typedef struct JPC_CharacterVirtual JPC_CharacterVirtual; +// Vehicle types +typedef struct JPC_VehicleConstraintSettings JPC_VehicleConstraintSettings; +typedef struct JPC_VehicleConstraint JPC_VehicleConstraint; +typedef struct JPC_VehicleControllerSettings JPC_VehicleControllerSettings; +typedef struct JPC_VehicleController JPC_VehicleController; +typedef struct JPC_WheeledVehicleControllerSettings JPC_WheeledVehicleControllerSettings; +typedef struct JPC_WheeledVehicleController JPC_WheeledVehicleController; +typedef struct JPC_WheelSettings JPC_WheelSettings; +typedef struct JPC_WheelSettingsWV JPC_WheelSettingsWV; +typedef struct JPC_Wheel JPC_Wheel; +typedef struct JPC_WheelWV JPC_WheelWV; +typedef struct JPC_VehicleCollisionTester JPC_VehicleCollisionTester; + #if JPC_DEBUG_RENDERER == 1 typedef struct JPC_BodyDrawFilter JPC_BodyDrawFilter; typedef struct JPC_DebugRenderer_TriangleBatch JPC_DebugRenderer_TriangleBatch; @@ -1377,6 +1390,13 @@ JPC_PhysicsSystem_AddStepListener(JPC_PhysicsSystem *in_physics_system, void *in JPC_API void JPC_PhysicsSystem_RemoveStepListener(JPC_PhysicsSystem *in_physics_system, void *in_listener); +// Vehicle-specific step listener functions (handles multiple inheritance correctly) +JPC_API void +JPC_PhysicsSystem_AddVehicleStepListener(JPC_PhysicsSystem *in_physics_system, JPC_VehicleConstraint *in_vehicle); + +JPC_API void +JPC_PhysicsSystem_RemoveVehicleStepListener(JPC_PhysicsSystem *in_physics_system, JPC_VehicleConstraint *in_vehicle); + JPC_API void JPC_PhysicsSystem_AddConstraint(JPC_PhysicsSystem *in_physics_system, JPC_Constraint *in_constraint); @@ -2498,6 +2518,304 @@ JPC_CharacterVirtual_GetLinearVelocity(const JPC_CharacterVirtual *in_character, JPC_API void JPC_CharacterVirtual_SetLinearVelocity(JPC_CharacterVirtual *in_character, const float in_linear_velocity[3]); //-------------------------------------------------------------------------------------------------- +// +// Vehicle Structures +// +//-------------------------------------------------------------------------------------------------- +typedef struct JPC_VehicleAntiRollBarSettings { + int32_t left_wheel; + int32_t right_wheel; + float stiffness; +} JPC_VehicleAntiRollBarSettings; + +typedef struct JPC_VehicleDifferentialSettings { + int32_t left_wheel; + int32_t right_wheel; + float differential_ratio; + float left_right_split; + float limited_slip_ratio; + float engine_torque_ratio; +} JPC_VehicleDifferentialSettings; + +typedef enum JPC_ETransmissionMode { + JPC_TRANSMISSION_AUTO = 0, + JPC_TRANSMISSION_MANUAL = 1, + _JPC_TRANSMISSION_FORCEU32 = 0x7fffffff +} JPC_ETransmissionMode; +//-------------------------------------------------------------------------------------------------- +// +// JPC_WheelSettings +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_WheelSettings * +JPC_WheelSettings_Create(); + +JPC_API void +JPC_WheelSettings_Release(JPC_WheelSettings *in_settings); + +JPC_API void +JPC_WheelSettings_SetPosition(JPC_WheelSettings *in_settings, const float in_position[3]); + +JPC_API void +JPC_WheelSettings_SetSuspensionDirection(JPC_WheelSettings *in_settings, const float in_direction[3]); + +JPC_API void +JPC_WheelSettings_SetSteeringAxis(JPC_WheelSettings *in_settings, const float in_axis[3]); + +JPC_API void +JPC_WheelSettings_SetWheelUp(JPC_WheelSettings *in_settings, const float in_up[3]); + +JPC_API void +JPC_WheelSettings_SetWheelForward(JPC_WheelSettings *in_settings, const float in_forward[3]); + +JPC_API void +JPC_WheelSettings_SetSuspensionMinLength(JPC_WheelSettings *in_settings, float in_length); + +JPC_API void +JPC_WheelSettings_SetSuspensionMaxLength(JPC_WheelSettings *in_settings, float in_length); + +JPC_API void +JPC_WheelSettings_SetSuspensionPreloadLength(JPC_WheelSettings *in_settings, float in_length); + +JPC_API void +JPC_WheelSettings_SetSuspensionSpring(JPC_WheelSettings *in_settings, float in_frequency, float in_damping); + +JPC_API void +JPC_WheelSettings_SetRadius(JPC_WheelSettings *in_settings, float in_radius); + +JPC_API void +JPC_WheelSettings_SetWidth(JPC_WheelSettings *in_settings, float in_width); +//-------------------------------------------------------------------------------------------------- +// +// JPC_WheelSettingsWV (-> JPC_WheelSettings) +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_WheelSettingsWV * +JPC_WheelSettingsWV_Create(); + +JPC_API void +JPC_WheelSettingsWV_Release(JPC_WheelSettingsWV *in_settings); + +JPC_API JPC_WheelSettings * +JPC_WheelSettingsWV_AsWheelSettings(JPC_WheelSettingsWV *in_settings); + +JPC_API void +JPC_WheelSettingsWV_SetInertia(JPC_WheelSettingsWV *in_settings, float in_inertia); + +JPC_API void +JPC_WheelSettingsWV_SetAngularDamping(JPC_WheelSettingsWV *in_settings, float in_damping); + +JPC_API void +JPC_WheelSettingsWV_SetMaxSteerAngle(JPC_WheelSettingsWV *in_settings, float in_angle); + +JPC_API void +JPC_WheelSettingsWV_SetMaxBrakeTorque(JPC_WheelSettingsWV *in_settings, float in_torque); + +JPC_API void +JPC_WheelSettingsWV_SetMaxHandBrakeTorque(JPC_WheelSettingsWV *in_settings, float in_torque); +//-------------------------------------------------------------------------------------------------- +// +// JPC_WheeledVehicleControllerSettings (-> JPC_VehicleControllerSettings) +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_WheeledVehicleControllerSettings * +JPC_WheeledVehicleControllerSettings_Create(); + +JPC_API void +JPC_WheeledVehicleControllerSettings_Release(JPC_WheeledVehicleControllerSettings *in_settings); + +JPC_API JPC_VehicleControllerSettings * +JPC_WheeledVehicleControllerSettings_AsVehicleControllerSettings(JPC_WheeledVehicleControllerSettings *in_settings); + +JPC_API void +JPC_WheeledVehicleControllerSettings_AddDifferential(JPC_WheeledVehicleControllerSettings *in_settings, + const JPC_VehicleDifferentialSettings *in_differential); + +// Engine settings +JPC_API void +JPC_WheeledVehicleControllerSettings_SetEngineMaxTorque(JPC_WheeledVehicleControllerSettings *in_settings, float in_torque); + +JPC_API void +JPC_WheeledVehicleControllerSettings_SetEngineMinRPM(JPC_WheeledVehicleControllerSettings *in_settings, float in_rpm); + +JPC_API void +JPC_WheeledVehicleControllerSettings_SetEngineMaxRPM(JPC_WheeledVehicleControllerSettings *in_settings, float in_rpm); + +// Transmission settings +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionMode(JPC_WheeledVehicleControllerSettings *in_settings, + JPC_ETransmissionMode in_mode); + +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionGearRatios(JPC_WheeledVehicleControllerSettings *in_settings, + const float *in_ratios, + uint32_t in_count); + +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionReverseGearRatios(JPC_WheeledVehicleControllerSettings *in_settings, + const float *in_ratios, + uint32_t in_count); + +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionSwitchTime(JPC_WheeledVehicleControllerSettings *in_settings, float in_time); + +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionClutchStrength(JPC_WheeledVehicleControllerSettings *in_settings, float in_strength); + +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionShiftUpRPM(JPC_WheeledVehicleControllerSettings *in_settings, float in_rpm); + +JPC_API void +JPC_WheeledVehicleControllerSettings_SetTransmissionShiftDownRPM(JPC_WheeledVehicleControllerSettings *in_settings, float in_rpm); +//-------------------------------------------------------------------------------------------------- +// +// JPC_VehicleCollisionTester +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_VehicleCollisionTester * +JPC_VehicleCollisionTesterRay_Create(JPC_ObjectLayer in_object_layer, const float in_up[3], float in_max_slope_angle); + +JPC_API JPC_VehicleCollisionTester * +JPC_VehicleCollisionTesterCastSphere_Create(JPC_ObjectLayer in_object_layer, float in_radius, const float in_up[3], float in_max_slope_angle); + +JPC_API JPC_VehicleCollisionTester * +JPC_VehicleCollisionTesterCastCylinder_Create(JPC_ObjectLayer in_object_layer, float in_convex_radius_fraction); + +JPC_API void +JPC_VehicleCollisionTester_AddRef(JPC_VehicleCollisionTester *in_tester); + +JPC_API void +JPC_VehicleCollisionTester_Release(JPC_VehicleCollisionTester *in_tester); +//-------------------------------------------------------------------------------------------------- +// +// JPC_VehicleConstraintSettings (-> JPC_ConstraintSettings) +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_VehicleConstraintSettings * +JPC_VehicleConstraintSettings_Create(); + +JPC_API void +JPC_VehicleConstraintSettings_Release(JPC_VehicleConstraintSettings *in_settings); + +JPC_API JPC_ConstraintSettings * +JPC_VehicleConstraintSettings_AsConstraintSettings(JPC_VehicleConstraintSettings *in_settings); + +JPC_API void +JPC_VehicleConstraintSettings_SetUp(JPC_VehicleConstraintSettings *in_settings, const float in_up[3]); + +JPC_API void +JPC_VehicleConstraintSettings_SetForward(JPC_VehicleConstraintSettings *in_settings, const float in_forward[3]); + +JPC_API void +JPC_VehicleConstraintSettings_SetMaxPitchRollAngle(JPC_VehicleConstraintSettings *in_settings, float in_angle); + +JPC_API void +JPC_VehicleConstraintSettings_AddWheel(JPC_VehicleConstraintSettings *in_settings, JPC_WheelSettings *in_wheel); + +JPC_API void +JPC_VehicleConstraintSettings_AddAntiRollBar(JPC_VehicleConstraintSettings *in_settings, + const JPC_VehicleAntiRollBarSettings *in_anti_roll_bar); + +JPC_API void +JPC_VehicleConstraintSettings_SetController(JPC_VehicleConstraintSettings *in_settings, + JPC_VehicleControllerSettings *in_controller); +//-------------------------------------------------------------------------------------------------- +// +// JPC_VehicleConstraint (-> JPC_Constraint) +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_VehicleConstraint * +JPC_VehicleConstraint_Create(JPC_Body *in_body, const JPC_VehicleConstraintSettings *in_settings); + +JPC_API JPC_Constraint * +JPC_VehicleConstraint_AsConstraint(JPC_VehicleConstraint *in_constraint); + +JPC_API void +JPC_VehicleConstraint_SetVehicleCollisionTester(JPC_VehicleConstraint *in_constraint, + JPC_VehicleCollisionTester *in_tester); + +JPC_API uint32_t +JPC_VehicleConstraint_GetNumWheels(const JPC_VehicleConstraint *in_constraint); + +JPC_API JPC_Wheel * +JPC_VehicleConstraint_GetWheel(JPC_VehicleConstraint *in_constraint, uint32_t in_index); + +JPC_API void +JPC_VehicleConstraint_GetWheelLocalTransform(const JPC_VehicleConstraint *in_constraint, + uint32_t in_wheel_index, + const float in_wheel_right[3], + const float in_wheel_up[3], + float out_position[3], + float out_rotation[4]); + +JPC_API void +JPC_VehicleConstraint_GetWheelWorldTransform(const JPC_VehicleConstraint *in_constraint, + uint32_t in_wheel_index, + const float in_wheel_right[3], + const float in_wheel_up[3], + float out_position[3], + float out_rotation[4]); +//-------------------------------------------------------------------------------------------------- +// +// JPC_WheeledVehicleController +// +//-------------------------------------------------------------------------------------------------- +JPC_API JPC_WheeledVehicleController * +JPC_VehicleConstraint_GetController(JPC_VehicleConstraint *in_constraint); + +JPC_API void +JPC_WheeledVehicleController_SetDriverInput(JPC_WheeledVehicleController *in_controller, + float in_forward, + float in_right, + float in_brake, + float in_hand_brake); + +JPC_API float +JPC_WheeledVehicleController_GetEngineRotationSpeed(const JPC_WheeledVehicleController *in_controller); + +JPC_API int32_t +JPC_WheeledVehicleController_GetTransmissionCurrentGear(const JPC_WheeledVehicleController *in_controller); + +JPC_API float +JPC_WheeledVehicleController_GetTransmissionClutchFriction(const JPC_WheeledVehicleController *in_controller); +//-------------------------------------------------------------------------------------------------- +// +// JPC_Wheel +// +//-------------------------------------------------------------------------------------------------- +JPC_API bool +JPC_Wheel_HasContact(const JPC_Wheel *in_wheel); + +JPC_API JPC_BodyID +JPC_Wheel_GetContactBodyID(const JPC_Wheel *in_wheel); + +JPC_API void +JPC_Wheel_GetContactPosition(const JPC_Wheel *in_wheel, float out_position[3]); + +JPC_API void +JPC_Wheel_GetContactNormal(const JPC_Wheel *in_wheel, float out_normal[3]); + +JPC_API float +JPC_Wheel_GetSuspensionLength(const JPC_Wheel *in_wheel); + +JPC_API float +JPC_Wheel_GetAngularVelocity(const JPC_Wheel *in_wheel); + +JPC_API void +JPC_Wheel_SetAngularVelocity(JPC_Wheel *in_wheel, float in_velocity); + +JPC_API float +JPC_Wheel_GetRotationAngle(const JPC_Wheel *in_wheel); + +JPC_API void +JPC_Wheel_SetRotationAngle(JPC_Wheel *in_wheel, float in_angle); + +JPC_API float +JPC_Wheel_GetSteerAngle(const JPC_Wheel *in_wheel); + +JPC_API void +JPC_Wheel_SetSteerAngle(JPC_Wheel *in_wheel, float in_angle); +//-------------------------------------------------------------------------------------------------- #ifdef __cplusplus } #endif diff --git a/src/zphysics.zig b/src/zphysics.zig index 7491ada..cf24bff 100644 --- a/src/zphysics.zig +++ b/src/zphysics.zig @@ -1453,6 +1453,15 @@ pub const PhysicsSystem = opaque { c.JPC_PhysicsSystem_RemoveStepListener(@as(*c.JPC_PhysicsSystem, @ptrCast(physics_system)), listener); } + /// Add a VehicleConstraint as a step listener (handles multiple inheritance correctly) + pub fn addVehicleStepListener(physics_system: *PhysicsSystem, vehicle: *VehicleConstraint) void { + c.JPC_PhysicsSystem_AddVehicleStepListener(@as(*c.JPC_PhysicsSystem, @ptrCast(physics_system)), @ptrCast(vehicle)); + } + /// Remove a VehicleConstraint step listener + pub fn removeVehicleStepListener(physics_system: *PhysicsSystem, vehicle: *VehicleConstraint) void { + c.JPC_PhysicsSystem_RemoveVehicleStepListener(@as(*c.JPC_PhysicsSystem, @ptrCast(physics_system)), @ptrCast(vehicle)); + } + pub fn addConstraint(physics_system: *PhysicsSystem, constraint: ?*Constraint) void { c.JPC_PhysicsSystem_AddConstraint(@as(*c.JPC_PhysicsSystem, @ptrCast(physics_system)), @ptrCast(constraint)); } @@ -3762,6 +3771,388 @@ pub const Constraint = opaque { }; //-------------------------------------------------------------------------------------------------- // +// WheelSettings +// +//-------------------------------------------------------------------------------------------------- +pub const WheelSettings = opaque { + pub fn create() !*WheelSettings { + return @ptrCast(c.JPC_WheelSettings_Create() orelse return error.FailedToCreateWheelSettings); + } + + pub fn release(self: *WheelSettings) void { + c.JPC_WheelSettings_Release(@ptrCast(self)); + } + + pub fn setPosition(self: *WheelSettings, position: [3]f32) void { + c.JPC_WheelSettings_SetPosition(@ptrCast(self), &position); + } + + pub fn setSuspensionDirection(self: *WheelSettings, direction: [3]f32) void { + c.JPC_WheelSettings_SetSuspensionDirection(@ptrCast(self), &direction); + } + + pub fn setSteeringAxis(self: *WheelSettings, axis: [3]f32) void { + c.JPC_WheelSettings_SetSteeringAxis(@ptrCast(self), &axis); + } + + pub fn setWheelUp(self: *WheelSettings, up: [3]f32) void { + c.JPC_WheelSettings_SetWheelUp(@ptrCast(self), &up); + } + + pub fn setWheelForward(self: *WheelSettings, forward: [3]f32) void { + c.JPC_WheelSettings_SetWheelForward(@ptrCast(self), &forward); + } + + pub fn setSuspensionMinLength(self: *WheelSettings, length: f32) void { + c.JPC_WheelSettings_SetSuspensionMinLength(@ptrCast(self), length); + } + + pub fn setSuspensionMaxLength(self: *WheelSettings, length: f32) void { + c.JPC_WheelSettings_SetSuspensionMaxLength(@ptrCast(self), length); + } + + pub fn setSuspensionPreloadLength(self: *WheelSettings, length: f32) void { + c.JPC_WheelSettings_SetSuspensionPreloadLength(@ptrCast(self), length); + } + + pub fn setSuspensionSpring(self: *WheelSettings, frequency: f32, damping: f32) void { + c.JPC_WheelSettings_SetSuspensionSpring(@ptrCast(self), frequency, damping); + } + + pub fn setRadius(self: *WheelSettings, radius: f32) void { + c.JPC_WheelSettings_SetRadius(@ptrCast(self), radius); + } + + pub fn setWidth(self: *WheelSettings, width: f32) void { + c.JPC_WheelSettings_SetWidth(@ptrCast(self), width); + } +}; +//-------------------------------------------------------------------------------------------------- +// +// WheelSettingsWV (-> WheelSettings) +// +//-------------------------------------------------------------------------------------------------- +pub const WheelSettingsWV = opaque { + pub fn create() !*WheelSettingsWV { + return @ptrCast(c.JPC_WheelSettingsWV_Create() orelse return error.FailedToCreateWheelSettingsWV); + } + + pub fn release(self: *WheelSettingsWV) void { + c.JPC_WheelSettingsWV_Release(@ptrCast(self)); + } + + pub fn asWheelSettings(self: *WheelSettingsWV) *WheelSettings { + return @ptrCast(c.JPC_WheelSettingsWV_AsWheelSettings(@ptrCast(self))); + } + + pub fn setInertia(self: *WheelSettingsWV, inertia: f32) void { + c.JPC_WheelSettingsWV_SetInertia(@ptrCast(self), inertia); + } + + pub fn setAngularDamping(self: *WheelSettingsWV, damping: f32) void { + c.JPC_WheelSettingsWV_SetAngularDamping(@ptrCast(self), damping); + } + + pub fn setMaxSteerAngle(self: *WheelSettingsWV, angle: f32) void { + c.JPC_WheelSettingsWV_SetMaxSteerAngle(@ptrCast(self), angle); + } + + pub fn setMaxBrakeTorque(self: *WheelSettingsWV, torque: f32) void { + c.JPC_WheelSettingsWV_SetMaxBrakeTorque(@ptrCast(self), torque); + } + + pub fn setMaxHandBrakeTorque(self: *WheelSettingsWV, torque: f32) void { + c.JPC_WheelSettingsWV_SetMaxHandBrakeTorque(@ptrCast(self), torque); + } +}; +//-------------------------------------------------------------------------------------------------- +// +// Wheel +// +//-------------------------------------------------------------------------------------------------- +pub const Wheel = opaque { + pub fn hasContact(self: *const Wheel) bool { + return c.JPC_Wheel_HasContact(@ptrCast(self)); + } + + pub fn getContactBodyID(self: *const Wheel) BodyId { + return c.JPC_Wheel_GetContactBodyID(@ptrCast(self)); + } + + pub fn getContactPosition(self: *const Wheel) [3]f32 { + var position: [3]f32 = undefined; + c.JPC_Wheel_GetContactPosition(@ptrCast(self), &position); + return position; + } + + pub fn getContactNormal(self: *const Wheel) [3]f32 { + var normal: [3]f32 = undefined; + c.JPC_Wheel_GetContactNormal(@ptrCast(self), &normal); + return normal; + } + + pub fn getSuspensionLength(self: *const Wheel) f32 { + return c.JPC_Wheel_GetSuspensionLength(@ptrCast(self)); + } + + pub fn getAngularVelocity(self: *const Wheel) f32 { + return c.JPC_Wheel_GetAngularVelocity(@ptrCast(self)); + } + + pub fn setAngularVelocity(self: *Wheel, velocity: f32) void { + c.JPC_Wheel_SetAngularVelocity(@ptrCast(self), velocity); + } + + pub fn getRotationAngle(self: *const Wheel) f32 { + return c.JPC_Wheel_GetRotationAngle(@ptrCast(self)); + } + + pub fn setRotationAngle(self: *Wheel, angle: f32) void { + c.JPC_Wheel_SetRotationAngle(@ptrCast(self), angle); + } + + pub fn getSteerAngle(self: *const Wheel) f32 { + return c.JPC_Wheel_GetSteerAngle(@ptrCast(self)); + } + + pub fn setSteerAngle(self: *Wheel, angle: f32) void { + c.JPC_Wheel_SetSteerAngle(@ptrCast(self), angle); + } +}; +//-------------------------------------------------------------------------------------------------- +// +// VehicleControllerSettings +// +//-------------------------------------------------------------------------------------------------- +pub const VehicleControllerSettings = opaque {}; +//-------------------------------------------------------------------------------------------------- +// +// WheeledVehicleControllerSettings (-> VehicleControllerSettings) +// +//-------------------------------------------------------------------------------------------------- +pub const VehicleDifferentialSettings = extern struct { + left_wheel: i32 = -1, + right_wheel: i32 = -1, + differential_ratio: f32 = 3.42, + left_right_split: f32 = 0.5, + limited_slip_ratio: f32 = 1.4, + engine_torque_ratio: f32 = 1.0, +}; + +pub const TransmissionMode = enum(c_uint) { + auto = 0, + manual = 1, +}; + +pub const WheeledVehicleControllerSettings = opaque { + pub fn create() !*WheeledVehicleControllerSettings { + return @ptrCast(c.JPC_WheeledVehicleControllerSettings_Create() orelse return error.FailedToCreateWheeledVehicleControllerSettings); + } + + pub fn release(self: *WheeledVehicleControllerSettings) void { + c.JPC_WheeledVehicleControllerSettings_Release(@ptrCast(self)); + } + + pub fn asVehicleControllerSettings(self: *WheeledVehicleControllerSettings) *VehicleControllerSettings { + return @ptrCast(c.JPC_WheeledVehicleControllerSettings_AsVehicleControllerSettings(@ptrCast(self))); + } + + pub fn addDifferential(self: *WheeledVehicleControllerSettings, differential: VehicleDifferentialSettings) void { + c.JPC_WheeledVehicleControllerSettings_AddDifferential(@ptrCast(self), @ptrCast(&differential)); + } + + pub fn setEngineMaxTorque(self: *WheeledVehicleControllerSettings, torque: f32) void { + c.JPC_WheeledVehicleControllerSettings_SetEngineMaxTorque(@ptrCast(self), torque); + } + + pub fn setEngineMinRPM(self: *WheeledVehicleControllerSettings, rpm: f32) void { + c.JPC_WheeledVehicleControllerSettings_SetEngineMinRPM(@ptrCast(self), rpm); + } + + pub fn setEngineMaxRPM(self: *WheeledVehicleControllerSettings, rpm: f32) void { + c.JPC_WheeledVehicleControllerSettings_SetEngineMaxRPM(@ptrCast(self), rpm); + } + + pub fn setTransmissionMode(self: *WheeledVehicleControllerSettings, mode: TransmissionMode) void { + c.JPC_WheeledVehicleControllerSettings_SetTransmissionMode(@ptrCast(self), @intFromEnum(mode)); + } + + pub fn setTransmissionGearRatios(self: *WheeledVehicleControllerSettings, ratios: []const f32) void { + c.JPC_WheeledVehicleControllerSettings_SetTransmissionGearRatios(@ptrCast(self), ratios.ptr, @intCast(ratios.len)); + } + + pub fn setTransmissionReverseGearRatios(self: *WheeledVehicleControllerSettings, ratios: []const f32) void { + c.JPC_WheeledVehicleControllerSettings_SetTransmissionReverseGearRatios(@ptrCast(self), ratios.ptr, @intCast(ratios.len)); + } + + pub fn setTransmissionSwitchTime(self: *WheeledVehicleControllerSettings, time: f32) void { + c.JPC_WheeledVehicleControllerSettings_SetTransmissionSwitchTime(@ptrCast(self), time); + } + + pub fn setTransmissionClutchStrength(self: *WheeledVehicleControllerSettings, strength: f32) void { + c.JPC_WheeledVehicleControllerSettings_SetTransmissionClutchStrength(@ptrCast(self), strength); + } + + pub fn setTransmissionShiftUpRPM(self: *WheeledVehicleControllerSettings, rpm: f32) void { + c.JPC_WheeledVehicleControllerSettings_SetTransmissionShiftUpRPM(@ptrCast(self), rpm); + } + + pub fn setTransmissionShiftDownRPM(self: *WheeledVehicleControllerSettings, rpm: f32) void { + c.JPC_WheeledVehicleControllerSettings_SetTransmissionShiftDownRPM(@ptrCast(self), rpm); + } +}; +//-------------------------------------------------------------------------------------------------- +// +// WheeledVehicleController +// +//-------------------------------------------------------------------------------------------------- +pub const WheeledVehicleController = opaque { + pub fn setDriverInput(self: *WheeledVehicleController, forward: f32, right: f32, brake: f32, hand_brake: f32) void { + c.JPC_WheeledVehicleController_SetDriverInput(@ptrCast(self), forward, right, brake, hand_brake); + } + + pub fn getEngineRotationSpeed(self: *const WheeledVehicleController) f32 { + return c.JPC_WheeledVehicleController_GetEngineRotationSpeed(@ptrCast(self)); + } + + pub fn getTransmissionCurrentGear(self: *const WheeledVehicleController) i32 { + return c.JPC_WheeledVehicleController_GetTransmissionCurrentGear(@ptrCast(self)); + } + + pub fn getTransmissionClutchFriction(self: *const WheeledVehicleController) f32 { + return c.JPC_WheeledVehicleController_GetTransmissionClutchFriction(@ptrCast(self)); + } +}; +//-------------------------------------------------------------------------------------------------- +// +// VehicleCollisionTester +// +//-------------------------------------------------------------------------------------------------- +pub const VehicleCollisionTester = opaque { + pub fn addRef(self: *VehicleCollisionTester) void { + c.JPC_VehicleCollisionTester_AddRef(@ptrCast(self)); + } + + pub fn release(self: *VehicleCollisionTester) void { + c.JPC_VehicleCollisionTester_Release(@ptrCast(self)); + } +}; + +pub const VehicleCollisionTesterRay = opaque { + pub fn create(object_layer: ObjectLayer, up: [3]f32, max_slope_angle: f32) !*VehicleCollisionTester { + return @ptrCast(c.JPC_VehicleCollisionTesterRay_Create(object_layer, &up, max_slope_angle) orelse return error.FailedToCreateVehicleCollisionTester); + } +}; + +pub const VehicleCollisionTesterCastSphere = opaque { + pub fn create(object_layer: ObjectLayer, radius: f32, up: [3]f32, max_slope_angle: f32) !*VehicleCollisionTester { + return @ptrCast(c.JPC_VehicleCollisionTesterCastSphere_Create(object_layer, radius, &up, max_slope_angle) orelse return error.FailedToCreateVehicleCollisionTester); + } +}; + +pub const VehicleCollisionTesterCastCylinder = opaque { + pub fn create(object_layer: ObjectLayer, convex_radius_fraction: f32) !*VehicleCollisionTester { + return @ptrCast(c.JPC_VehicleCollisionTesterCastCylinder_Create(object_layer, convex_radius_fraction) orelse return error.FailedToCreateVehicleCollisionTester); + } +}; +//-------------------------------------------------------------------------------------------------- +// +// VehicleAntiRollBarSettings +// +//-------------------------------------------------------------------------------------------------- +pub const VehicleAntiRollBarSettings = extern struct { + left_wheel: i32, + right_wheel: i32, + stiffness: f32 = 1000.0, +}; +//-------------------------------------------------------------------------------------------------- +// +// VehicleConstraintSettings (-> ConstraintSettings) +// +//-------------------------------------------------------------------------------------------------- +pub const VehicleConstraintSettings = opaque { + pub fn create() !*VehicleConstraintSettings { + return @ptrCast(c.JPC_VehicleConstraintSettings_Create() orelse return error.FailedToCreateVehicleConstraintSettings); + } + + pub fn release(self: *VehicleConstraintSettings) void { + c.JPC_VehicleConstraintSettings_Release(@ptrCast(self)); + } + + pub fn asConstraintSettings(self: *VehicleConstraintSettings) *ConstraintSettings { + return @ptrCast(c.JPC_VehicleConstraintSettings_AsConstraintSettings(@ptrCast(self))); + } + + pub fn setUp(self: *VehicleConstraintSettings, up: [3]f32) void { + c.JPC_VehicleConstraintSettings_SetUp(@ptrCast(self), &up); + } + + pub fn setForward(self: *VehicleConstraintSettings, forward: [3]f32) void { + c.JPC_VehicleConstraintSettings_SetForward(@ptrCast(self), &forward); + } + + pub fn setMaxPitchRollAngle(self: *VehicleConstraintSettings, angle: f32) void { + c.JPC_VehicleConstraintSettings_SetMaxPitchRollAngle(@ptrCast(self), angle); + } + + pub fn addWheel(self: *VehicleConstraintSettings, wheel: *WheelSettings) void { + c.JPC_VehicleConstraintSettings_AddWheel(@ptrCast(self), @ptrCast(wheel)); + } + + pub fn addAntiRollBar(self: *VehicleConstraintSettings, anti_roll_bar: VehicleAntiRollBarSettings) void { + c.JPC_VehicleConstraintSettings_AddAntiRollBar(@ptrCast(self), @ptrCast(&anti_roll_bar)); + } + + pub fn setController(self: *VehicleConstraintSettings, controller: *VehicleControllerSettings) void { + c.JPC_VehicleConstraintSettings_SetController(@ptrCast(self), @ptrCast(controller)); + } +}; +//-------------------------------------------------------------------------------------------------- +// +// VehicleConstraint (-> Constraint) +// +//-------------------------------------------------------------------------------------------------- +pub const VehicleConstraint = opaque { + pub fn create(body: *Body, settings: *const VehicleConstraintSettings) !*VehicleConstraint { + return @ptrCast(c.JPC_VehicleConstraint_Create(@ptrCast(body), @ptrCast(settings)) orelse return error.FailedToCreateVehicleConstraint); + } + + pub fn asConstraint(self: *VehicleConstraint) *Constraint { + return @ptrCast(c.JPC_VehicleConstraint_AsConstraint(@ptrCast(self))); + } + + pub fn setVehicleCollisionTester(self: *VehicleConstraint, tester: *VehicleCollisionTester) void { + c.JPC_VehicleConstraint_SetVehicleCollisionTester(@ptrCast(self), @ptrCast(tester)); + } + + pub fn getNumWheels(self: *const VehicleConstraint) u32 { + return c.JPC_VehicleConstraint_GetNumWheels(@ptrCast(self)); + } + + pub fn getWheel(self: *VehicleConstraint, index: u32) *Wheel { + return @ptrCast(c.JPC_VehicleConstraint_GetWheel(@ptrCast(self), index)); + } + + pub fn getController(self: *VehicleConstraint) *WheeledVehicleController { + return @ptrCast(c.JPC_VehicleConstraint_GetController(@ptrCast(self))); + } + + pub fn getWheelLocalTransform(self: *const VehicleConstraint, wheel_index: u32, wheel_right: [3]f32, wheel_up: [3]f32) struct { position: [3]f32, rotation: [4]f32 } { + var position: [3]f32 = undefined; + var rotation: [4]f32 = undefined; + c.JPC_VehicleConstraint_GetWheelLocalTransform(@ptrCast(self), wheel_index, &wheel_right, &wheel_up, &position, &rotation); + return .{ .position = position, .rotation = rotation }; + } + + pub fn getWheelWorldTransform(self: *const VehicleConstraint, wheel_index: u32, wheel_right: [3]f32, wheel_up: [3]f32) struct { position: [3]f32, rotation: [4]f32 } { + var position: [3]f32 = undefined; + var rotation: [4]f32 = undefined; + c.JPC_VehicleConstraint_GetWheelWorldTransform(@ptrCast(self), wheel_index, &wheel_right, &wheel_up, &position, &rotation); + return .{ .position = position, .rotation = rotation }; + } +}; +//-------------------------------------------------------------------------------------------------- +// // Memory allocation // //--------------------------------------------------------------------------------------------------