SpringSettings.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /// Selects the way in which the spring is defined
  30. /// 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.
  31. ESpringMode mMode = ESpringMode::FrequencyAndDamping;
  32. union
  33. {
  34. /// Valid when mSpringMode = ESpringMode::FrequencyAndDamping.
  35. /// If mFrequency > 0 the constraint will be soft and mFrequency specifies the oscillation frequency in Hz.
  36. /// If mFrequency <= 0, mDamping is ignored and the distance constraint will have hard limits (as hard as the time step / the number of velocity / position solver steps allows).
  37. float mFrequency = 0.0f;
  38. /// Valid when mSpringMode = ESpringMode::StiffnessAndDamping.
  39. /// 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.
  40. /// If mStiffness <= 0, mDamping is ignored and the distance constraint will have hard limits (as hard as the time step / the number of velocity / position solver steps allows).
  41. float mStiffness;
  42. };
  43. /// When mSpringMode = ESpringMode::FrequencyAndDamping mDamping is the damping ratio (0 = no damping, 1 = critical damping).
  44. /// 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.
  45. /// 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.
  46. /// 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.
  47. float mDamping = 0.0f;
  48. };
  49. JPH_NAMESPACE_END