MotorSettings.h 3.4 KB

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