MotorcycleController.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Vehicle/WheeledVehicleController.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Settings of a two wheeled motorcycle (adds a spring to balance the motorcycle)
  8. /// Note: The motor cycle controller is still in development and may need a lot of tweaks/hacks to work properly!
  9. class JPH_EXPORT MotorcycleControllerSettings : public WheeledVehicleControllerSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, MotorcycleControllerSettings)
  13. // See: VehicleControllerSettings
  14. virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override;
  15. virtual void SaveBinaryState(StreamOut &inStream) const override;
  16. virtual void RestoreBinaryState(StreamIn &inStream) override;
  17. /// How far we're willing to make the bike lean over in turns (in radians)
  18. float mMaxLeanAngle = DegreesToRadians(45.0f);
  19. /// Spring constant for the lean spring
  20. float mLeanSpringConstant = 5000.0f;
  21. /// Spring damping constant for the lean spring
  22. float mLeanSpringDamping = 1000.0f;
  23. /// The lean spring applies an additional force equal to this coefficient * Integral(delta angle, 0, t), this effectively makes the lean spring a PID controller
  24. float mLeanSpringIntegrationCoefficient = 0.0f;
  25. /// How much to decay the angle integral when the wheels are not touching the floor: new_value = e^(-decay * t) * initial_value
  26. float mLeanSpringIntegrationCoefficientDecay = 4.0f;
  27. /// How much to smooth the lean angle (0 = no smoothing, 1 = lean angle never changes)
  28. /// Note that this is frame rate dependent because the formula is: smoothing_factor * previous + (1 - smoothing_factor) * current
  29. float mLeanSmoothingFactor = 0.8f;
  30. };
  31. /// Runtime controller class
  32. class JPH_EXPORT MotorcycleController : public WheeledVehicleController
  33. {
  34. public:
  35. JPH_OVERRIDE_NEW_DELETE
  36. /// Constructor
  37. MotorcycleController(const MotorcycleControllerSettings &inSettings, VehicleConstraint &inConstraint);
  38. /// Get the distance between the front and back wheels
  39. float GetWheelBase() const;
  40. /// Enable or disable the lean spring. This allows you to temporarily disable the lean spring to allow the motorcycle to fall over.
  41. void EnableLeanController(bool inEnable) { mEnableLeanController = inEnable; }
  42. /// Check if the lean spring is enabled.
  43. bool IsLeanControllerEnabled() const { return mEnableLeanController; }
  44. protected:
  45. // See: VehicleController
  46. virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
  47. virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;
  48. virtual void SaveState(StateRecorder& inStream) const override;
  49. virtual void RestoreState(StateRecorder& inStream) override;
  50. #ifdef JPH_DEBUG_RENDERER
  51. virtual void Draw(DebugRenderer *inRenderer) const override;
  52. #endif // JPH_DEBUG_RENDERER
  53. // Configuration properties
  54. bool mEnableLeanController = true;
  55. float mMaxLeanAngle;
  56. float mLeanSpringConstant;
  57. float mLeanSpringDamping;
  58. float mLeanSpringIntegrationCoefficient;
  59. float mLeanSpringIntegrationCoefficientDecay;
  60. float mLeanSmoothingFactor;
  61. // Run-time calculated target lean vector
  62. Vec3 mTargetLean = Vec3::sZero();
  63. // Integrated error for the lean spring
  64. float mLeanSpringIntegratedDeltaAngle = 0.0f;
  65. // Run-time total angular impulse applied to turn the cycle towards the target lean angle
  66. float mAppliedImpulse = 0.0f;
  67. };
  68. JPH_NAMESPACE_END