Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/SpringSettings.h
9912 views
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
};
20
21
/// Settings for a linear or angular spring
22
class JPH_EXPORT SpringSettings
23
{
24
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SpringSettings)
25
26
public:
27
/// Constructor
28
SpringSettings() = default;
29
SpringSettings(const SpringSettings &) = default;
30
SpringSettings & operator = (const SpringSettings &) = default;
31
SpringSettings(ESpringMode inMode, float inFrequencyOrStiffness, float inDamping) : mMode(inMode), mFrequency(inFrequencyOrStiffness), mDamping(inDamping) { }
32
33
/// Saves the contents of the spring settings in binary form to inStream.
34
void SaveBinaryState(StreamOut &inStream) const;
35
36
/// Restores contents from the binary stream inStream.
37
void RestoreBinaryState(StreamIn &inStream);
38
39
/// Check if the spring has a valid frequency / stiffness, if not the spring will be hard
40
inline bool HasStiffness() const { return mFrequency > 0.0f; }
41
42
/// Selects the way in which the spring is defined
43
/// 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.
44
ESpringMode mMode = ESpringMode::FrequencyAndDamping;
45
46
union
47
{
48
/// Valid when mSpringMode = ESpringMode::FrequencyAndDamping.
49
/// If mFrequency > 0 the constraint will be soft and mFrequency specifies the oscillation frequency in Hz.
50
/// 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).
51
float mFrequency = 0.0f;
52
53
/// Valid when mSpringMode = ESpringMode::StiffnessAndDamping.
54
/// 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.
55
/// 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).
56
///
57
/// Note that stiffness values are large numbers. To calculate a ballpark value for the needed stiffness you can use:
58
/// force = stiffness * delta_spring_length = mass * gravity <=> stiffness = mass * gravity / delta_spring_length.
59
/// 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.
60
float mStiffness;
61
};
62
63
/// When mSpringMode = ESpringMode::FrequencyAndDamping mDamping is the damping ratio (0 = no damping, 1 = critical damping).
64
/// 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.
65
/// 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.
66
/// 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.
67
float mDamping = 0.0f;
68
};
69
70
JPH_NAMESPACE_END
71
72