VehicleEngine.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/Vehicle/VehicleEngine.h>
  5. #include <ObjectStream/TypeDeclarations.h>
  6. #ifdef JPH_DEBUG_RENDERER
  7. #include <Renderer/DebugRenderer.h>
  8. #endif // JPH_DEBUG_RENDERER
  9. namespace JPH {
  10. JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(VehicleEngineSettings)
  11. {
  12. JPH_ADD_ATTRIBUTE(VehicleEngineSettings, mMaxTorque)
  13. JPH_ADD_ATTRIBUTE(VehicleEngineSettings, mMinRPM)
  14. JPH_ADD_ATTRIBUTE(VehicleEngineSettings, mMaxRPM)
  15. JPH_ADD_ATTRIBUTE(VehicleEngineSettings, mNormalizedTorque)
  16. }
  17. VehicleEngineSettings::VehicleEngineSettings()
  18. {
  19. mNormalizedTorque.Reserve(3);
  20. mNormalizedTorque.AddPoint(0.0f, 0.8f);
  21. mNormalizedTorque.AddPoint(0.66f, 1.0f);
  22. mNormalizedTorque.AddPoint(1.0f, 0.8f);
  23. }
  24. void VehicleEngineSettings::SaveBinaryState(StreamOut &inStream) const
  25. {
  26. inStream.Write(mMaxTorque);
  27. inStream.Write(mMinRPM);
  28. inStream.Write(mMaxRPM);
  29. mNormalizedTorque.SaveBinaryState(inStream);
  30. }
  31. void VehicleEngineSettings::RestoreBinaryState(StreamIn &inStream)
  32. {
  33. inStream.Read(mMaxTorque);
  34. inStream.Read(mMinRPM);
  35. inStream.Read(mMaxRPM);
  36. mNormalizedTorque.RestoreBinaryState(inStream);
  37. }
  38. void VehicleEngine::UpdateRPM(float inDeltaTime, float inAcceleration)
  39. {
  40. // Angular damping: dw/dt = -c * w
  41. // Solution: w(t) = w(0) * e^(-c * t) or w2 = w1 * e^(-c * dt)
  42. // Taylor expansion of e^(-c * dt) = 1 - c * dt + ...
  43. // Since dt is usually in the order of 1/60 and c is a low number too this approximation is good enough
  44. mCurrentRPM *= max(0.0f, 1.0f - mAngularDamping * inDeltaTime);
  45. // Accelerate engine using torque
  46. mCurrentRPM += cAngularVelocityToRPM * GetTorque(inAcceleration) * inDeltaTime / mInertia;
  47. // Clamp RPM
  48. mCurrentRPM = Clamp(mCurrentRPM, mMinRPM, mMaxRPM);
  49. }
  50. #ifdef JPH_DEBUG_RENDERER
  51. void VehicleEngine::DrawRPM(DebugRenderer *inRenderer, Vec3Arg inPosition, Vec3Arg inForward, Vec3Arg inUp, float inSize, float inShiftDownRPM, float inShiftUpRPM) const
  52. {
  53. // Function that converts RPM to an angle in radians
  54. auto rpm_to_angle = [this](float inRPM) { return (-0.75f + 1.5f * (inRPM - mMinRPM) / (mMaxRPM - mMinRPM)) * JPH_PI; };
  55. // Function to draw part of a pie
  56. auto draw_pie = [rpm_to_angle, inRenderer, inSize, inPosition, inForward, inUp](float inMinRPM, float inMaxRPM, Color inColor) {
  57. inRenderer->DrawPie(inPosition, inSize, inForward, inUp, rpm_to_angle(inMinRPM), rpm_to_angle(inMaxRPM), inColor, DebugRenderer::ECastShadow::Off);
  58. };
  59. // Draw segment until inShiftDownRPM
  60. if (mCurrentRPM < inShiftDownRPM)
  61. {
  62. draw_pie(mMinRPM, mCurrentRPM, Color::sRed);
  63. draw_pie(mCurrentRPM, inShiftDownRPM, Color::sDarkRed);
  64. }
  65. else
  66. {
  67. draw_pie(mMinRPM, inShiftDownRPM, Color::sRed);
  68. }
  69. // Draw segment between inShiftDownRPM and inShiftUpRPM
  70. if (mCurrentRPM > inShiftDownRPM && mCurrentRPM < inShiftUpRPM)
  71. {
  72. draw_pie(inShiftDownRPM, mCurrentRPM, Color::sOrange);
  73. draw_pie(mCurrentRPM, inShiftUpRPM, Color::sDarkOrange);
  74. }
  75. else
  76. {
  77. draw_pie(inShiftDownRPM, inShiftUpRPM, mCurrentRPM <= inShiftDownRPM? Color::sDarkOrange : Color::sOrange);
  78. }
  79. // Draw segment above inShiftUpRPM
  80. if (mCurrentRPM > inShiftUpRPM)
  81. {
  82. draw_pie(inShiftUpRPM, mCurrentRPM, Color::sGreen);
  83. draw_pie(mCurrentRPM, mMaxRPM, Color::sDarkGreen);
  84. }
  85. else
  86. {
  87. draw_pie(inShiftUpRPM, mMaxRPM, Color::sDarkGreen);
  88. }
  89. }
  90. #endif // JPH_DEBUG_RENDERER
  91. void VehicleEngine::SaveState(StateRecorder &inStream) const
  92. {
  93. inStream.Write(mCurrentRPM);
  94. }
  95. void VehicleEngine::RestoreState(StateRecorder &inStream)
  96. {
  97. inStream.Read(mCurrentRPM);
  98. }
  99. } // JPH