VehicleTransmission.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/ObjectStream/SerializableObject.h>
  6. #include <Jolt/Core/StreamIn.h>
  7. #include <Jolt/Core/StreamOut.h>
  8. #include <Jolt/Physics/StateRecorder.h>
  9. JPH_NAMESPACE_BEGIN
  10. /// How gears are shifted
  11. enum class ETransmissionMode : uint8
  12. {
  13. Auto, ///< Automatically shift gear up and down
  14. Manual, ///< Manual gear shift (call SetTransmissionInput)
  15. };
  16. /// Configuration for the transmission of a vehicle (gear box)
  17. class VehicleTransmissionSettings
  18. {
  19. public:
  20. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(VehicleTransmissionSettings)
  21. /// Saves the contents in binary form to inStream.
  22. void SaveBinaryState(StreamOut &inStream) const;
  23. /// Restores the contents in binary form to inStream.
  24. void RestoreBinaryState(StreamIn &inStream);
  25. ETransmissionMode mMode = ETransmissionMode::Auto; ///< How to switch gears
  26. Array<float> mGearRatios { 2.66f, 1.78f, 1.3f, 1.0f, 0.74f }; ///< Ratio in rotation rate between engine and gear box, first element is 1st gear, 2nd element 2nd gear etc.
  27. Array<float> mReverseGearRatios { -2.90f }; ///< Ratio in rotation rate between engine and gear box when driving in reverse
  28. float mSwitchTime = 0.5f; ///< How long it takes to switch gears (s), only used in auto mode
  29. float mClutchReleaseTime = 0.3f; ///< How long it takes to release the clutch (go to full friction), only used in auto mode
  30. float mSwitchLatency = 0.5f; ///< How long to wait after releasing the clutch before another switch is attempted (s), only used in auto mode
  31. float mShiftUpRPM = 4000.0f; ///< If RPM of engine is bigger then this we will shift a gear up, only used in auto mode
  32. float mShiftDownRPM = 2000.0f; ///< If RPM of engine is smaller then this we will shift a gear down, only used in auto mode
  33. float mClutchStrength = 10.0f; ///< Strength of the clutch when fully engaged. Total torque a clutch applies is Torque = ClutchStrength * (Velocity Engine - Avg Velocity Wheels) (units: k m^2 s^-1)
  34. };
  35. /// Runtime data for transmission
  36. class VehicleTransmission : public VehicleTransmissionSettings
  37. {
  38. public:
  39. /// Set input from driver regarding the transmission (only relevant when transmission is set to manual mode)
  40. /// @param inCurrentGear Current gear, -1 = reverse, 0 = neutral, 1 = 1st gear etc.
  41. /// @param inClutchFriction Value between 0 and 1 indicating how much friction the clutch gives (0 = no friction, 1 = full friction)
  42. void Set(int inCurrentGear, float inClutchFriction) { mCurrentGear = inCurrentGear; mClutchFriction = inClutchFriction; }
  43. /// Update the current gear and clutch friction if the transmission is in aut mode
  44. /// @param inDeltaTime Time step delta time in s
  45. /// @param inCurrentRPM Current RPM for engine
  46. /// @param inForwardInput Hint if the user wants to drive forward (> 0) or backwards (< 0)
  47. /// @param inCanShiftUp Indicates if we want to allow the transmission to shift up (e.g. pass false if wheels are slipping)
  48. void Update(float inDeltaTime, float inCurrentRPM, float inForwardInput, bool inCanShiftUp);
  49. /// Current gear, -1 = reverse, 0 = neutral, 1 = 1st gear etc.
  50. int GetCurrentGear() const { return mCurrentGear; }
  51. /// Value between 0 and 1 indicating how much friction the clutch gives (0 = no friction, 1 = full friction)
  52. float GetClutchFriction() const { return mClutchFriction; }
  53. /// If the auto box is currently switching gears
  54. bool IsSwitchingGear() const { return mGearSwitchTimeLeft > 0.0f; }
  55. /// Return the transmission ratio based on the current gear (ratio between engine and differential)
  56. float GetCurrentRatio() const;
  57. /// Saving state for replay
  58. void SaveState(StateRecorder &inStream) const;
  59. void RestoreState(StateRecorder &inStream);
  60. private:
  61. int mCurrentGear = 0; ///< Current gear, -1 = reverse, 0 = neutral, 1 = 1st gear etc.
  62. float mClutchFriction = 1.0f; ///< Value between 0 and 1 indicating how much friction the clutch gives (0 = no friction, 1 = full friction)
  63. float mGearSwitchTimeLeft = 0.0f; ///< When switching gears this will be > 0 and will cause the engine to not provide any torque to the wheels for a short time (used for automatic gear switching only)
  64. float mClutchReleaseTimeLeft = 0.0f; ///< After switching gears this will be > 0 and will cause the clutch friction to go from 0 to 1 (used for automatic gear switching only)
  65. float mGearSwitchLatencyTimeLeft = 0.0f; ///< After releasing the clutch this will be > 0 and will prevent another gear switch (used for automatic gear switching only)
  66. };
  67. JPH_NAMESPACE_END