WheeledVehicleController.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Vehicle/VehicleConstraint.h>
  5. #include <Jolt/Physics/Vehicle/VehicleController.h>
  6. #include <Jolt/Physics/Vehicle/VehicleEngine.h>
  7. #include <Jolt/Physics/Vehicle/VehicleTransmission.h>
  8. #include <Jolt/Physics/Vehicle/VehicleDifferential.h>
  9. #include <Jolt/Core/LinearCurve.h>
  10. JPH_NAMESPACE_BEGIN
  11. class PhysicsSystem;
  12. /// WheelSettings object specifically for WheeledVehicleController
  13. class WheelSettingsWV : public WheelSettings
  14. {
  15. public:
  16. JPH_DECLARE_SERIALIZABLE_VIRTUAL(WheelSettingsWV)
  17. /// Constructor
  18. WheelSettingsWV();
  19. // See: WheelSettings
  20. virtual void SaveBinaryState(StreamOut &inStream) const override;
  21. virtual void RestoreBinaryState(StreamIn &inStream) override;
  22. float mInertia = 0.9f; ///< Moment of inertia (kg m^2), for a cylinder this would be 0.5 * M * R^2 which is 0.9 for a wheel with a mass of 20 kg and radius 0.3 m
  23. float mAngularDamping = 0.2f; ///< Angular damping factor of the wheel: dw/dt = -c * w
  24. float mMaxSteerAngle = DegreesToRadians(70.0f); ///< How much this wheel can steer (radians)
  25. LinearCurve mLongitudinalFriction; ///< Friction in forward direction of tire as a function of the slip ratio (fraction): (omega_wheel * r_wheel - v_longitudinal) / |v_longitudinal|
  26. LinearCurve mLateralFriction; ///< Friction in sideway direction of tire as a function of the slip angle (degrees): angle between relative contact velocity and vehicle direction
  27. float mMaxBrakeTorque = 1500.0f; ///< How much torque (Nm) the brakes can apply to this wheel
  28. float mMaxHandBrakeTorque = 4000.0f; ///< How much torque (Nm) the hand brake can apply to this wheel (usually only applied to the rear wheels)
  29. };
  30. /// Wheel object specifically for WheeledVehicleController
  31. class WheelWV : public Wheel
  32. {
  33. public:
  34. JPH_OVERRIDE_NEW_DELETE
  35. /// Constructor
  36. explicit WheelWV(const WheelSettingsWV &inWheel);
  37. /// Override GetSettings and cast to the correct class
  38. const WheelSettingsWV * GetSettings() const { return static_cast<const WheelSettingsWV *>(mSettings.GetPtr()); }
  39. /// Apply a torque (N m) to the wheel for a particular delta time
  40. void ApplyTorque(float inTorque, float inDeltaTime)
  41. {
  42. mAngularVelocity += inTorque * inDeltaTime / GetSettings()->mInertia;
  43. }
  44. /// Update the wheel rotation based on the current angular velocity
  45. void Update(float inDeltaTime, const VehicleConstraint &inConstraint);
  46. float mLongitudinalSlip = 0.0f; ///< Velocity difference between ground and wheel relative to ground velocity
  47. float mCombinedLongitudinalFriction = 0.0f; ///< Combined friction coefficient in longitudinal direction (combines terrain and tires)
  48. float mCombinedLateralFriction = 0.0f; ///< Combined friction coefficient in lateral direction (combines terrain and tires)
  49. float mBrakeImpulse = 0.0f; ///< Amount of impulse that the brakes can apply to the floor (excluding friction)
  50. };
  51. /// Settings of a vehicle with regular wheels
  52. ///
  53. /// The properties in this controller are largely based on "Car Physics for Games" by Marco Monster.
  54. /// See: https://www.asawicki.info/Mirror/Car%20Physics%20for%20Games/Car%20Physics%20for%20Games.html
  55. class WheeledVehicleControllerSettings : public VehicleControllerSettings
  56. {
  57. public:
  58. JPH_DECLARE_SERIALIZABLE_VIRTUAL(WheeledVehicleControllerSettings)
  59. // See: VehicleControllerSettings
  60. virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override;
  61. virtual void SaveBinaryState(StreamOut &inStream) const override;
  62. virtual void RestoreBinaryState(StreamIn &inStream) override;
  63. VehicleEngineSettings mEngine; ///< The properties of the engine
  64. VehicleTransmissionSettings mTransmission; ///< The properties of the transmission (aka gear box)
  65. Array<VehicleDifferentialSettings> mDifferentials; ///< List of differentials and their properties
  66. float mDifferentialLimitedSlipRatio = 1.4f; ///< Ratio max / min average wheel speed of each differential (measured at the clutch). When the ratio is exceeded all torque gets distributed to the differential with the minimal average velocity. This allows implementing a limited slip differential between differentials. Set to FLT_MAX for an open differential. Value should be > 1.
  67. };
  68. /// Runtime controller class
  69. class WheeledVehicleController : public VehicleController
  70. {
  71. public:
  72. JPH_OVERRIDE_NEW_DELETE
  73. /// Constructor
  74. WheeledVehicleController(const WheeledVehicleControllerSettings &inSettings, VehicleConstraint &inConstraint);
  75. /// Typedefs
  76. using Differentials = Array<VehicleDifferentialSettings>;
  77. /// Set input from driver
  78. /// @param inForward Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving direction and amount the gas pedal is pressed
  79. /// @param inRight Value between -1 and 1 indicating desired steering angle (1 = right)
  80. /// @param inBrake Value between 0 and 1 indicating how strong the brake pedal is pressed
  81. /// @param inHandBrake Value between 0 and 1 indicating how strong the hand brake is pulled
  82. void SetDriverInput(float inForward, float inRight, float inBrake, float inHandBrake) { mForwardInput = inForward; mRightInput = inRight; mBrakeInput = inBrake; mHandBrakeInput = inHandBrake; }
  83. /// Get current engine state
  84. const VehicleEngine & GetEngine() const { return mEngine; }
  85. /// Get current engine state (writable interface, allows you to make changes to the configuration which will take effect the next time step)
  86. VehicleEngine & GetEngine() { return mEngine; }
  87. /// Get current transmission state
  88. const VehicleTransmission & GetTransmission() const { return mTransmission; }
  89. /// Get current transmission state (writable interface, allows you to make changes to the configuration which will take effect the next time step)
  90. VehicleTransmission & GetTransmission() { return mTransmission; }
  91. /// Get the differentials this vehicle has
  92. const Differentials & GetDifferentials() const { return mDifferentials; }
  93. /// Get the differentials this vehicle has (writable interface, allows you to make changes to the configuration which will take effect the next time step)
  94. Differentials & GetDifferentials() { return mDifferentials; }
  95. /// Ratio max / min average wheel speed of each differential (measured at the clutch).
  96. float GetDifferentialLimitedSlipRatio() const { return mDifferentialLimitedSlipRatio; }
  97. void SetDifferentialLimitedSlipRatio(float inV) { mDifferentialLimitedSlipRatio = inV; }
  98. #ifdef JPH_DEBUG_RENDERER
  99. /// Debug drawing of RPM meter
  100. void SetRPMMeter(Vec3Arg inPosition, float inSize) { mRPMMeterPosition = inPosition; mRPMMeterSize = inSize; }
  101. #endif // JPH_DEBUG_RENDERER
  102. protected:
  103. // See: VehicleController
  104. virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override { JPH_ASSERT(IsKindOf(&inWheel, JPH_RTTI(WheelSettingsWV))); return new WheelWV(static_cast<const WheelSettingsWV &>(inWheel)); }
  105. virtual bool AllowSleep() const override { return mForwardInput == 0.0f; }
  106. virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
  107. virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
  108. virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;
  109. virtual void SaveState(StateRecorder &inStream) const override;
  110. virtual void RestoreState(StateRecorder &inStream) override;
  111. #ifdef JPH_DEBUG_RENDERER
  112. virtual void Draw(DebugRenderer *inRenderer) const override;
  113. #endif // JPH_DEBUG_RENDERER
  114. // Control information
  115. float mForwardInput = 0.0f; ///< Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving direction and amount the gas pedal is pressed
  116. float mRightInput = 0.0f; ///< Value between -1 and 1 indicating desired steering angle
  117. float mBrakeInput = 0.0f; ///< Value between 0 and 1 indicating how strong the brake pedal is pressed
  118. float mHandBrakeInput = 0.0f; ///< Value between 0 and 1 indicating how strong the hand brake is pulled
  119. // Simluation information
  120. VehicleEngine mEngine; ///< Engine state of the vehicle
  121. VehicleTransmission mTransmission; ///< Transmission state of the vehicle
  122. Differentials mDifferentials; ///< Differential states of the vehicle
  123. float mDifferentialLimitedSlipRatio; ///< Ratio max / min average wheel speed of each differential (measured at the clutch).
  124. #ifdef JPH_DEBUG_RENDERER
  125. // Debug settings
  126. Vec3 mRPMMeterPosition { 0, 1, 0 }; ///< Position (in local space of the body) of the RPM meter when drawing the constraint
  127. float mRPMMeterSize = 0.5f; ///< Size of the RPM meter when drawing the constraint
  128. #endif // JPH_DEBUG_RENDERER
  129. };
  130. JPH_NAMESPACE_END