Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/SpringSettings.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2023 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/ObjectStream/SerializableObject.h>78JPH_NAMESPACE_BEGIN910class StreamIn;11class StreamOut;1213/// Enum used by constraints to specify how the spring is defined14enum class ESpringMode : uint815{16FrequencyAndDamping, ///< Frequency and damping are specified17StiffnessAndDamping, ///< Stiffness and damping are specified18};1920/// Settings for a linear or angular spring21class JPH_EXPORT SpringSettings22{23JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SpringSettings)2425public:26/// Constructor27SpringSettings() = default;28SpringSettings(const SpringSettings &) = default;29SpringSettings & operator = (const SpringSettings &) = default;30SpringSettings(ESpringMode inMode, float inFrequencyOrStiffness, float inDamping) : mMode(inMode), mFrequency(inFrequencyOrStiffness), mDamping(inDamping) { }3132/// Saves the contents of the spring settings in binary form to inStream.33void SaveBinaryState(StreamOut &inStream) const;3435/// Restores contents from the binary stream inStream.36void RestoreBinaryState(StreamIn &inStream);3738/// Check if the spring has a valid frequency / stiffness, if not the spring will be hard39inline bool HasStiffness() const { return mFrequency > 0.0f; }4041/// Selects the way in which the spring is defined42/// 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.43ESpringMode mMode = ESpringMode::FrequencyAndDamping;4445union46{47/// Valid when mSpringMode = ESpringMode::FrequencyAndDamping.48/// If mFrequency > 0 the constraint will be soft and mFrequency specifies the oscillation frequency in Hz.49/// 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).50float mFrequency = 0.0f;5152/// Valid when mSpringMode = ESpringMode::StiffnessAndDamping.53/// 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.54/// 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).55///56/// Note that stiffness values are large numbers. To calculate a ballpark value for the needed stiffness you can use:57/// force = stiffness * delta_spring_length = mass * gravity <=> stiffness = mass * gravity / delta_spring_length.58/// 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.59float mStiffness;60};6162/// When mSpringMode = ESpringMode::FrequencyAndDamping mDamping is the damping ratio (0 = no damping, 1 = critical damping).63/// 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.64/// 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.65/// 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.66float mDamping = 0.0f;67};6869JPH_NAMESPACE_END707172