SpringSettings.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/ObjectStream/SerializableObject.h>
  6. JPH_NAMESPACE_BEGIN
  7. class StreamIn;
  8. class StreamOut;
  9. /// Enum used by constraints to specify how the spring is defined
  10. enum class ESpringMode : uint8
  11. {
  12. FrequencyAndDamping, ///< Frequency and damping are specified
  13. StiffnessAndDamping, ///< Stiffness and damping are specified
  14. };
  15. /// Settings for a linear or angular spring
  16. class JPH_EXPORT SpringSettings
  17. {
  18. public:
  19. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SpringSettings)
  20. /// Constructor
  21. SpringSettings() = default;
  22. SpringSettings(const SpringSettings &) = default;
  23. SpringSettings & operator = (const SpringSettings &) = default;
  24. SpringSettings(ESpringMode inMode, float inFrequencyOrStiffness, float inDamping) : mMode(inMode), mFrequency(inFrequencyOrStiffness), mDamping(inDamping) { }
  25. /// Saves the contents of the spring settings in binary form to inStream.
  26. void SaveBinaryState(StreamOut &inStream) const;
  27. /// Restores contents from the binary stream inStream.
  28. void RestoreBinaryState(StreamIn &inStream);
  29. /// Check if the spring has a valid frequency / stiffness, if not the spring will be hard
  30. inline bool HasStiffness() const { return mFrequency > 0.0f; }
  31. /// Selects the way in which the spring is defined
  32. /// If the mode is StiffnessAndDamping then mFrequency becomes the stiffness (k) and mDamping becomes the damping ratio (c) in the spring equation F = -k * x - c * v. Otherwise the properties are as documented.
  33. ESpringMode mMode = ESpringMode::FrequencyAndDamping;
  34. union
  35. {
  36. /// Valid when mSpringMode = ESpringMode::FrequencyAndDamping.
  37. /// If mFrequency > 0 the constraint will be soft and mFrequency specifies the oscillation frequency in Hz.
  38. /// If mFrequency <= 0, mDamping is ignored and the constraint will have hard limits (as hard as the time step / the number of velocity / position solver steps allows).
  39. float mFrequency = 0.0f;
  40. /// Valid when mSpringMode = ESpringMode::StiffnessAndDamping.
  41. /// If mStiffness > 0 the constraint will be soft and mStiffness specifies the stiffness (k) in the spring equation F = -k * x - c * v for a linear or T = -k * theta - c * w for an angular spring.
  42. /// If mStiffness <= 0, mDamping is ignored and the constraint will have hard limits (as hard as the time step / the number of velocity / position solver steps allows).
  43. float mStiffness;
  44. };
  45. /// When mSpringMode = ESpringMode::FrequencyAndDamping mDamping is the damping ratio (0 = no damping, 1 = critical damping).
  46. /// When mSpringMode = ESpringMode::StiffnessAndDamping mDamping is the damping (c) in the spring equation F = -k * x - c * v for a linear or T = -k * theta - c * w for an angular spring.
  47. /// Note that if you set mDamping = 0, you will not get an infinite oscillation. Because we integrate physics using an explicit Euler scheme, there is always energy loss.
  48. /// This is done to keep the simulation from exploding, because with a damping of 0 and even the slightest rounding error, the oscillation could become bigger and bigger until the simluation explodes.
  49. float mDamping = 0.0f;
  50. };
  51. JPH_NAMESPACE_END