pub struct Spring {
pub mass: f32,
pub stiffness: f32,
pub damping: f32,
}Expand description
A damped-harmonic-oscillator spring, solved in closed form.
Parameters follow the common “physical” convention: mass, stiffness (k)
and damping (c). The angular frequency is ω₀ = √(k/m) and the damping
ratio is ζ = c / (2√(k·m)).
Fields§
§mass: f32Oscillator mass (> 0).
stiffness: f32Spring stiffness k (> 0).
damping: f32Damping coefficient c (>= 0).
Implementations§
Source§impl Spring
impl Spring
Sourcepub fn new(mass: f32, stiffness: f32, damping: f32) -> Self
pub fn new(mass: f32, stiffness: f32, damping: f32) -> Self
Construct a spring from physical parameters.
Sourcepub fn from_frequency(omega0: f32, zeta: f32) -> Self
pub fn from_frequency(omega0: f32, zeta: f32) -> Self
Build a spring with the given natural frequency ω₀ (rad/s) and damping
ratio ζ (unit mass). ζ = 1 is critically damped.
Sourcepub fn natural_frequency(&self) -> f32
pub fn natural_frequency(&self) -> f32
The undamped natural angular frequency ω₀ = √(k/m).
Sourcepub fn damping_ratio(&self) -> f32
pub fn damping_ratio(&self) -> f32
The damping ratio ζ. < 1 under-, == 1 critically-, > 1 over-damped.
Sourcepub fn position(&self, from: f32, to: f32, v0: f32, t: f32) -> f32
pub fn position(&self, from: f32, to: f32, v0: f32, t: f32) -> f32
Position at time t (seconds) for a spring released from displacement
from - to with initial velocity v0, settling towards to.
Returns the absolute position (already offset by to). The solution is
the exact analytic response of m·x'' + c·x' + k·x = 0, so it is
numerically stable at any t and frame rate.