| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2021 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <Jolt/Jolt.h> |
| 6 | |
| 7 | #include <Jolt/Physics/Vehicle/VehicleDifferential.h> |
| 8 | #include <Jolt/ObjectStream/TypeDeclarations.h> |
| 9 | |
| 10 | JPH_NAMESPACE_BEGIN |
| 11 | |
| 12 | JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(VehicleDifferentialSettings) |
| 13 | { |
| 14 | JPH_ADD_ATTRIBUTE(VehicleDifferentialSettings, mLeftWheel) |
| 15 | JPH_ADD_ATTRIBUTE(VehicleDifferentialSettings, mRightWheel) |
| 16 | JPH_ADD_ATTRIBUTE(VehicleDifferentialSettings, mDifferentialRatio) |
| 17 | JPH_ADD_ATTRIBUTE(VehicleDifferentialSettings, mLeftRightSplit) |
| 18 | JPH_ADD_ATTRIBUTE(VehicleDifferentialSettings, mLimitedSlipRatio) |
| 19 | JPH_ADD_ATTRIBUTE(VehicleDifferentialSettings, mEngineTorqueRatio) |
| 20 | } |
| 21 | |
| 22 | void VehicleDifferentialSettings::SaveBinaryState(StreamOut &inStream) const |
| 23 | { |
| 24 | inStream.Write(mLeftWheel); |
| 25 | inStream.Write(mRightWheel); |
| 26 | inStream.Write(mDifferentialRatio); |
| 27 | inStream.Write(mLeftRightSplit); |
| 28 | inStream.Write(mLimitedSlipRatio); |
| 29 | inStream.Write(mEngineTorqueRatio); |
| 30 | } |
| 31 | |
| 32 | void VehicleDifferentialSettings::RestoreBinaryState(StreamIn &inStream) |
| 33 | { |
| 34 | inStream.Read(mLeftWheel); |
| 35 | inStream.Read(mRightWheel); |
| 36 | inStream.Read(mDifferentialRatio); |
| 37 | inStream.Read(mLeftRightSplit); |
| 38 | inStream.Read(mLimitedSlipRatio); |
| 39 | inStream.Read(mEngineTorqueRatio); |
| 40 | } |
| 41 | |
| 42 | void VehicleDifferentialSettings::CalculateTorqueRatio(float inLeftAngularVelocity, float inRightAngularVelocity, float &outLeftTorqueFraction, float &outRightTorqueFraction) const |
| 43 | { |
| 44 | // Start with the default torque ratio |
| 45 | outLeftTorqueFraction = 1.0f - mLeftRightSplit; |
| 46 | outRightTorqueFraction = mLeftRightSplit; |
| 47 | |
| 48 | if (mLimitedSlipRatio < FLT_MAX) |
| 49 | { |
| 50 | JPH_ASSERT(mLimitedSlipRatio > 1.0f); |
| 51 | |
| 52 | // This is a limited slip differential, adjust torque ratios according to wheel speeds |
| 53 | float omega_l = max(1.0e-3f, abs(inLeftAngularVelocity)); // prevent div by zero by setting a minimum velocity and ignoring that the wheels may be rotating in different directions |
| 54 | float omega_r = max(1.0e-3f, abs(inRightAngularVelocity)); |
| 55 | float omega_min = min(omega_l, omega_r); |
| 56 | float omega_max = max(omega_l, omega_r); |
| 57 | |
| 58 | // Map into a value that is 0 when the wheels are turning at an equal rate and 1 when the wheels are turning at mLimitedSlipRotationRatio |
| 59 | float alpha = min((omega_max / omega_min - 1.0f) / (mLimitedSlipRatio - 1.0f), 1.0f); |
| 60 | JPH_ASSERT(alpha >= 0.0f); |
| 61 | float one_min_alpha = 1.0f - alpha; |
| 62 | |
| 63 | if (omega_l < omega_r) |
| 64 | { |
| 65 | // Redirect more power to the left wheel |
| 66 | outLeftTorqueFraction = outLeftTorqueFraction * one_min_alpha + alpha; |
| 67 | outRightTorqueFraction = outRightTorqueFraction * one_min_alpha; |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | // Redirect more power to the right wheel |
| 72 | outLeftTorqueFraction = outLeftTorqueFraction * one_min_alpha; |
| 73 | outRightTorqueFraction = outRightTorqueFraction * one_min_alpha + alpha; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Assert the values add up to 1 |
| 78 | JPH_ASSERT(abs(outLeftTorqueFraction + outRightTorqueFraction - 1.0f) < 1.0e-6f); |
| 79 | } |
| 80 | |
| 81 | JPH_NAMESPACE_END |