0.2.0-dev Download ZIP
87 lines ·4.7 KB Raw
| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2023 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <Jolt/ObjectStream/SerializableObject.h> |
| 8 | |
| 9 | JPH_NAMESPACE_BEGIN |
| 10 | |
| 11 | class StreamIn; |
| 12 | class StreamOut; |
| 13 | |
| 14 | /// Enum used by constraints to specify how the spring is defined |
| 15 | enum class ESpringMode : uint8 |
| 16 | { |
| 17 | FrequencyAndDamping, ///< Frequency and damping are specified. |
| 18 | StiffnessAndDamping, ///< Stiffness and damping are specified. |
| 19 | MassNormalizedStiffnessAndDamping, ///< Stiffness and damping divided by mass / inertia are specified (also known as acceleration mode). This makes it easier to tune the spring and makes it mass independent. |
| 20 | }; |
| 21 | |
| 22 | /// Settings for a linear or angular spring |
| 23 | class JPH_EXPORT SpringSettings |
| 24 | { |
| 25 | JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SpringSettings) |
| 26 | |
| 27 | public: |
| 28 | /// Constructor |
| 29 | SpringSettings() = default; |
| 30 | SpringSettings(const SpringSettings &) = default; |
| 31 | SpringSettings & operator = (const SpringSettings &) = default; |
| 32 | SpringSettings(ESpringMode inMode, float inFrequencyOrStiffness, float inDamping) : mMode(inMode), mFrequency(inFrequencyOrStiffness), mDamping(inDamping) { } |
| 33 | |
| 34 | /// Saves the contents of the spring settings in binary form to inStream. |
| 35 | void SaveBinaryState(StreamOut &inStream) const; |
| 36 | |
| 37 | /// Restores contents from the binary stream inStream. |
| 38 | void RestoreBinaryState(StreamIn &inStream); |
| 39 | |
| 40 | /// Check if the spring has a valid frequency / stiffness, if not the spring will be hard |
| 41 | inline bool HasStiffness() const { return mFrequency > 0.0f; } |
| 42 | |
| 43 | /// Check if this spring has stiffness or damping (making it active), if not the constraint will be hard |
| 44 | inline bool HasStiffnessOrDamping() const { return mFrequency > 0.0f || (mMode != ESpringMode::FrequencyAndDamping && mDamping > 0.0f); } |
| 45 | |
| 46 | /// Selects the way in which the spring is defined. See the descriptions of the mFrequency, mStiffness and mDamping properties. |
| 47 | ESpringMode mMode = ESpringMode::FrequencyAndDamping; |
| 48 | |
| 49 | union |
| 50 | { |
| 51 | /// Valid when mMode = ESpringMode::FrequencyAndDamping. |
| 52 | /// If > 0 the constraint will be soft and this specifies the oscillation frequency in Hz. |
| 53 | /// If <= 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). |
| 54 | float mFrequency = 0.0f; |
| 55 | |
| 56 | /// When mMode = ESpringMode::StiffnessAndDamping: |
| 57 | /// 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. |
| 58 | /// Units are N / m for a linear spring and N m / rad for an angular spring. |
| 59 | /// |
| 60 | /// Note that stiffness values are large numbers. To calculate a ballpark value for the needed stiffness you can use: |
| 61 | /// force = stiffness * delta_spring_length = mass * gravity <=> stiffness = mass * gravity / delta_spring_length. |
| 62 | /// So if your object weighs 1500 kg and the spring compresses by 2 meters, you need a stiffness in the order of 1500 * 9.81 / 2 ~ 7500 N/m. |
| 63 | /// |
| 64 | /// When mMode = ESpringMode::MassNormalizedStiffnessAndDamping: |
| 65 | /// Specifies the stiffness (k) in the spring equation F = m_eff * (-k * x - c * v) for a linear or T = i_eff * (-k * theta - c * w) for an angular spring. |
| 66 | /// m_eff / i_eff is the effective mass / inertia of the constraint. |
| 67 | /// Units are 1 / s^2 for a linear spring and 1 / rad s^2 for an angular spring. |
| 68 | /// |
| 69 | /// Since the stiffness is multiplied by the effective mass / inertia of the constraint, you can use much smaller stiffness values and they will be mass independent. |
| 70 | float mStiffness; |
| 71 | }; |
| 72 | |
| 73 | /// When mMode = ESpringMode::FrequencyAndDamping this is the damping ratio (0 = no damping, 1 = critical damping). |
| 74 | /// |
| 75 | /// When mMode = ESpringMode::StiffnessAndDamping this 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. |
| 76 | /// Units are N s / m for a linear spring and N s m / rad for an angular spring. |
| 77 | /// |
| 78 | /// When mMode = ESpringMode::MassNormalizedStiffnessAndDamping this is the damping (c) in the spring equation F = m_eff * (-k * x - c * v) for a linear or T = i_eff * (-k * theta - c * w) for an angular spring. |
| 79 | /// m_eff / i_eff is the effective mass / inertia of the constraint. |
| 80 | /// Units are 1 / s for a linear spring and 1 / rad s for an angular spring. |
| 81 | /// |
| 82 | /// Note that if you set this to 0, you will not get an infinite oscillation. Because we integrate physics using an explicit Euler scheme, there is always energy loss. |
| 83 | /// 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 simulation explodes. |
| 84 | float mDamping = 0.0f; |
| 85 | }; |
| 86 | |
| 87 | JPH_NAMESPACE_END |