MotorcycleController.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 MotorcycleControllerSettings : public WheeledVehicleControllerSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(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. /// How much to smooth the lean angle (0 = no smoothing, 1 = lean angle never changes)
  24. /// Note that this is frame rate dependent because the formula is: smoothing_factor * previous + (1 - smoothing_factor) * current
  25. float mLeanSmoothingFactor = 0.8f;
  26. };
  27. /// Runtime controller class
  28. class MotorcycleController : public WheeledVehicleController
  29. {
  30. public:
  31. JPH_OVERRIDE_NEW_DELETE
  32. /// Constructor
  33. MotorcycleController(const MotorcycleControllerSettings &inSettings, VehicleConstraint &inConstraint);
  34. /// Get the distance between the front and back wheels
  35. float GetWheelBase() const;
  36. protected:
  37. // See: VehicleController
  38. virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
  39. virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;
  40. virtual void SaveState(StateRecorder& inStream) const override;
  41. virtual void RestoreState(StateRecorder& inStream) override;
  42. #ifdef JPH_DEBUG_RENDERER
  43. virtual void Draw(DebugRenderer *inRenderer) const override;
  44. #endif // JPH_DEBUG_RENDERER
  45. // Configuration properties
  46. float mMaxLeanAngle;
  47. float mLeanSpringConstant;
  48. float mLeanSpringDamping;
  49. float mLeanSmoothingFactor;
  50. // Run-time calculated target lean vector
  51. Vec3 mTargetLean = Vec3::sZero();
  52. // Run-time total angular impulse applied to turn the cycle towards the target lean angle
  53. float mAppliedImpulse = 0.0f;
  54. };
  55. JPH_NAMESPACE_END