VehicleEngine.h 3.3 KB

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