VehicleEngine.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/LinearCurve.h>
  7. #include <Jolt/Core/StreamIn.h>
  8. #include <Jolt/Core/StreamOut.h>
  9. #include <Jolt/Physics/StateRecorder.h>
  10. JPH_NAMESPACE_BEGIN
  11. #ifdef JPH_DEBUG_RENDERER
  12. class DebugRenderer;
  13. #endif // JPH_DEBUG_RENDERER
  14. /// Generic properties for a vehicle engine
  15. class VehicleEngineSettings
  16. {
  17. public:
  18. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(VehicleEngineSettings)
  19. /// Constructor
  20. VehicleEngineSettings();
  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. float mMaxTorque = 500.0f; ///< Max amount of torque (Nm) that the engine can deliver
  26. float mMinRPM = 1000.0f; ///< Min amount of revolutions per minute (rpm) the engine can produce without stalling
  27. float mMaxRPM = 6000.0f; ///< Max amount of revolutions per minute (rpm) the engine can generate
  28. LinearCurve mNormalizedTorque; ///< Curve that describes a ratio of the max torque the engine can produce vs the fraction of the max RPM of the engine
  29. float mInertia = 0.5f; ///< Moment of inertia (kg m^2) of the engine
  30. float mAngularDamping = 0.2f; ///< Angular damping factor of the wheel: dw/dt = -c * w
  31. };
  32. /// Runtime data for engine
  33. class VehicleEngine : public VehicleEngineSettings
  34. {
  35. public:
  36. /// Multiply an angular velocity (rad/s) with this value to get rounds per minute (RPM)
  37. static constexpr float cAngularVelocityToRPM = 60.0f / (2.0f * JPH_PI);
  38. /// Clamp the RPM between min and max RPM
  39. inline void ClampRPM() { mCurrentRPM = Clamp(mCurrentRPM, mMinRPM, mMaxRPM); }
  40. /// Current rotation speed of engine in rounds per minute
  41. float GetCurrentRPM() const { return mCurrentRPM; }
  42. /// Update rotation speed of engine in rounds per minute
  43. void SetCurrentRPM(float inRPM) { mCurrentRPM = inRPM; ClampRPM(); }
  44. /// Get current angular velocity of the engine in radians / second
  45. inline float GetAngularVelocity() const { return mCurrentRPM / cAngularVelocityToRPM; }
  46. /// Get the amount of torque (N m) that the engine can supply
  47. /// @param inAcceleration How much the gas pedal is pressed [0, 1]
  48. float GetTorque(float inAcceleration) const { return inAcceleration * mMaxTorque * mNormalizedTorque.GetValue(mCurrentRPM / mMaxRPM); }
  49. /// Apply a torque to the engine rotation speed
  50. /// @param inTorque Torque in N m
  51. /// @param inDeltaTime Delta time in seconds
  52. void ApplyTorque(float inTorque, float inDeltaTime);
  53. /// Update the engine RPM for damping
  54. /// @param inDeltaTime Delta time in seconds
  55. void ApplyDamping(float inDeltaTime);
  56. #ifdef JPH_DEBUG_RENDERER
  57. /// Debug draw a RPM meter
  58. void DrawRPM(DebugRenderer *inRenderer, RVec3Arg inPosition, Vec3Arg inForward, Vec3Arg inUp, float inSize, float inShiftDownRPM, float inShiftUpRPM) const;
  59. #endif // JPH_DEBUG_RENDERER
  60. /// Saving state for replay
  61. void SaveState(StateRecorder &inStream) const;
  62. void RestoreState(StateRecorder &inStream);
  63. private:
  64. float mCurrentRPM = mMinRPM; ///< Current rotation speed of engine in rounds per minute
  65. };
  66. JPH_NAMESPACE_END