MotorSettings.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/Reference.h>
  5. #include <Jolt/ObjectStream/SerializableObject.h>
  6. JPH_NAMESPACE_BEGIN
  7. class StreamIn;
  8. class StreamOut;
  9. enum class EMotorState
  10. {
  11. Off, ///< Motor is off
  12. Velocity, ///< Motor will drive to target velocity
  13. Position ///< Motor will drive to target position
  14. };
  15. /// Class that contains the settings for a constraint motor.
  16. /// See the main page of the API documentation for more information on how to configure a motor.
  17. class MotorSettings
  18. {
  19. public:
  20. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(MotorSettings)
  21. /// Constructor
  22. MotorSettings() = default;
  23. MotorSettings(const MotorSettings &inRHS) = default;
  24. MotorSettings(float inFrequency, float inDamping) : mFrequency(inFrequency), mDamping(inDamping) { JPH_ASSERT(IsValid()); }
  25. MotorSettings(float inFrequency, float inDamping, float inForceLimit, float inTorqueLimit) : mFrequency(inFrequency), mDamping(inDamping), mMinForceLimit(-inForceLimit), mMaxForceLimit(inForceLimit), mMinTorqueLimit(-inTorqueLimit), mMaxTorqueLimit(inTorqueLimit) { JPH_ASSERT(IsValid()); }
  26. /// Set asymmetric force limits
  27. void SetForceLimits(float inMin, float inMax) { JPH_ASSERT(inMin <= inMax); mMinForceLimit = inMin; mMaxForceLimit = inMax; }
  28. /// Set asymmetric torque limits
  29. void SetTorqueLimits(float inMin, float inMax) { JPH_ASSERT(inMin <= inMax); mMinTorqueLimit = inMin; mMaxTorqueLimit = inMax; }
  30. /// Set symmetric force limits
  31. void SetForceLimit(float inLimit) { mMinForceLimit = -inLimit; mMaxForceLimit = inLimit; }
  32. /// Set symmetric torque limits
  33. void SetTorqueLimit(float inLimit) { mMinTorqueLimit = -inLimit; mMaxTorqueLimit = inLimit; }
  34. /// Check if settings are valid
  35. bool IsValid() const { return mFrequency >= 0.0f && mDamping >= 0.0f && mMinForceLimit <= mMaxForceLimit && mMinTorqueLimit <= mMaxTorqueLimit; }
  36. /// Saves the contents of the motor settings in binary form to inStream.
  37. void SaveBinaryState(StreamOut &inStream) const;
  38. /// Restores contents from the binary stream inStream.
  39. void RestoreBinaryState(StreamIn &inStream);
  40. // Settings
  41. float mFrequency = 2.0f; ///< Oscillation frequency when solving position target (Hz). Should be in the range (0, 0.5 * simulation frequency]. When simulating at 60 Hz, 20 is a good value for a strong motor. Only used for position motors.
  42. float mDamping = 1.0f; ///< Damping when solving position target (0 = minimal damping, 1 = critical damping). Only used for position motors.
  43. float mMinForceLimit = -FLT_MAX; ///< Minimum force to apply in case of a linear constraint (N). Usually this is -mMaxForceLimit unless you want a motor that can e.g. push but not pull. Not used when motor is an angular motor.
  44. float mMaxForceLimit = FLT_MAX; ///< Maximum force to apply in case of a linear constraint (N). Not used when motor is an angular motor.
  45. float mMinTorqueLimit = -FLT_MAX; ///< Minimum torque to apply in case of a angular constraint (N m). Usually this is -mMaxTorqueLimit unless you want a motor that can e.g. push but not pull. Not used when motor is a position motor.
  46. float mMaxTorqueLimit = FLT_MAX; ///< Maximum torque to apply in case of a angular constraint (N m). Not used when motor is a position motor.
  47. };
  48. JPH_NAMESPACE_END