TrackedVehicleController.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/VehicleTrack.h>
  9. JPH_NAMESPACE_BEGIN
  10. class PhysicsSystem;
  11. /// WheelSettings object specifically for TrackedVehicleController
  12. class WheelSettingsTV : public WheelSettings
  13. {
  14. public:
  15. JPH_DECLARE_SERIALIZABLE_VIRTUAL(WheelSettingsTV)
  16. // See: WheelSettings
  17. virtual void SaveBinaryState(StreamOut &inStream) const override;
  18. virtual void RestoreBinaryState(StreamIn &inStream) override;
  19. float mLongitudinalFriction = 4.0f; ///< Friction in forward direction of tire
  20. float mLateralFriction = 2.0f; ///< Friction in sideway direction of tire
  21. };
  22. /// Wheel object specifically for TrackedVehicleController
  23. class WheelTV : public Wheel
  24. {
  25. public:
  26. JPH_OVERRIDE_NEW_DELETE
  27. /// Constructor
  28. explicit WheelTV(const WheelSettingsTV &inWheel);
  29. /// Override GetSettings and cast to the correct class
  30. const WheelSettingsTV * GetSettings() const { return static_cast<const WheelSettingsTV *>(mSettings.GetPtr()); }
  31. /// Update the angular velocity of the wheel based on the angular velocity of the track
  32. void CalculateAngularVelocity(const VehicleConstraint &inConstraint);
  33. /// Update the wheel rotation based on the current angular velocity
  34. void Update(float inDeltaTime, const VehicleConstraint &inConstraint);
  35. int mTrackIndex = -1; ///< Index in mTracks to which this wheel is attached (calculated on initialization)
  36. float mCombinedLongitudinalFriction = 0.0f; ///< Combined friction coefficient in longitudinal direction (combines terrain and track)
  37. float mCombinedLateralFriction = 0.0f; ///< Combined friction coefficient in lateral direction (combines terrain and track)
  38. float mBrakeImpulse = 0.0f; ///< Amount of impulse that the brakes can apply to the floor (excluding friction), spread out from brake impulse applied on track
  39. };
  40. /// Settings of a vehicle with tank tracks
  41. ///
  42. /// Default settings are based around what I could find about the M1 Abrams tank.
  43. /// Note to avoid issues with very heavy objects vs very light objects the mass of the tank should be a lot lower (say 10x) than that of a real tank. That means that the engine/brake torque is also 10x less.
  44. class TrackedVehicleControllerSettings : public VehicleControllerSettings
  45. {
  46. public:
  47. JPH_DECLARE_SERIALIZABLE_VIRTUAL(TrackedVehicleControllerSettings)
  48. // Constructor
  49. TrackedVehicleControllerSettings();
  50. // See: VehicleControllerSettings
  51. virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override;
  52. virtual void SaveBinaryState(StreamOut &inStream) const override;
  53. virtual void RestoreBinaryState(StreamIn &inStream) override;
  54. VehicleEngineSettings mEngine; ///< The properties of the engine
  55. VehicleTransmissionSettings mTransmission; ///< The properties of the transmission (aka gear box)
  56. VehicleTrackSettings mTracks[(int)ETrackSide::Num]; ///< List of tracks and their properties
  57. };
  58. /// Runtime controller class for vehicle with tank tracks
  59. class TrackedVehicleController : public VehicleController
  60. {
  61. public:
  62. JPH_OVERRIDE_NEW_DELETE
  63. /// Constructor
  64. TrackedVehicleController(const TrackedVehicleControllerSettings &inSettings, VehicleConstraint &inConstraint);
  65. /// Set input from driver
  66. /// @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
  67. /// @param inLeftRatio Value between -1 and 1 indicating an extra multiplier to the rotation rate of the left track (used for steering)
  68. /// @param inRightRatio Value between -1 and 1 indicating an extra multiplier to the rotation rate of the right track (used for steering)
  69. /// @param inBrake Value between 0 and 1 indicating how strong the brake pedal is pressed
  70. void SetDriverInput(float inForward, float inLeftRatio, float inRightRatio, float inBrake) { JPH_ASSERT(inLeftRatio != 0.0f && inRightRatio != 0.0f); mForwardInput = inForward; mLeftRatio = inLeftRatio; mRightRatio = inRightRatio; mBrakeInput = inBrake; }
  71. /// Get current engine state
  72. const VehicleEngine & GetEngine() const { return mEngine; }
  73. /// Get current engine state (writable interface, allows you to make changes to the configuration which will take effect the next time step)
  74. VehicleEngine & GetEngine() { return mEngine; }
  75. /// Get current transmission state
  76. const VehicleTransmission & GetTransmission() const { return mTransmission; }
  77. /// Get current transmission state (writable interface, allows you to make changes to the configuration which will take effect the next time step)
  78. VehicleTransmission & GetTransmission() { return mTransmission; }
  79. /// Get the tracks this vehicle has
  80. const VehicleTracks & GetTracks() const { return mTracks; }
  81. /// Get the tracks this vehicle has (writable interface, allows you to make changes to the configuration which will take effect the next time step)
  82. VehicleTracks & GetTracks() { return mTracks; }
  83. #ifdef JPH_DEBUG_RENDERER
  84. /// Debug drawing of RPM meter
  85. void SetRPMMeter(Vec3Arg inPosition, float inSize) { mRPMMeterPosition = inPosition; mRPMMeterSize = inSize; }
  86. #endif // JPH_DEBUG_RENDERER
  87. protected:
  88. /// Synchronize angular velocities of left and right tracks according to their ratios
  89. void SyncLeftRightTracks();
  90. // See: VehicleController
  91. virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override { JPH_ASSERT(IsKindOf(&inWheel, JPH_RTTI(WheelSettingsTV))); return new WheelTV(static_cast<const WheelSettingsTV &>(inWheel)); }
  92. virtual bool AllowSleep() const override { return mForwardInput == 0.0f; }
  93. virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
  94. virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
  95. virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;
  96. virtual void SaveState(StateRecorder &inStream) const override;
  97. virtual void RestoreState(StateRecorder &inStream) override;
  98. #ifdef JPH_DEBUG_RENDERER
  99. virtual void Draw(DebugRenderer *inRenderer) const override;
  100. #endif // JPH_DEBUG_RENDERER
  101. // Control information
  102. 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
  103. float mLeftRatio = 1.0f; ///< Value between -1 and 1 indicating an extra multiplier to the rotation rate of the left track (used for steering)
  104. float mRightRatio = 1.0f; ///< Value between -1 and 1 indicating an extra multiplier to the rotation rate of the right track (used for steering)
  105. float mBrakeInput = 0.0f; ///< Value between 0 and 1 indicating how strong the brake pedal is pressed
  106. // Simluation information
  107. VehicleEngine mEngine; ///< Engine state of the vehicle
  108. VehicleTransmission mTransmission; ///< Transmission state of the vehicle
  109. VehicleTracks mTracks; ///< Tracks of the vehicle
  110. #ifdef JPH_DEBUG_RENDERER
  111. // Debug settings
  112. Vec3 mRPMMeterPosition { 0, 1, 0 }; ///< Position (in local space of the body) of the RPM meter when drawing the constraint
  113. float mRPMMeterSize = 0.5f; ///< Size of the RPM meter when drawing the constraint
  114. #endif // JPH_DEBUG_RENDERER
  115. };
  116. JPH_NAMESPACE_END