pub struct Spring {
pub config: SpringConfig,
/* private fields */
}Expand description
A 1D damped harmonic oscillator spring.
Stack-allocated and no_std-compatible. Use SpringN<T>
for multi-dimensional animation.
§Example
use animato_spring::{Spring, SpringConfig};
use animato_core::Update;
let mut s = Spring::new(SpringConfig::stiff());
s.set_target(100.0);
for _ in 0..300 {
s.update(1.0 / 60.0);
}
assert!((s.position() - 100.0).abs() < 0.01);Fields§
§config: SpringConfigThe spring configuration (stiffness, damping, mass, epsilon).
Implementations§
Source§impl Spring
impl Spring
Sourcepub fn new(config: SpringConfig) -> Self
pub fn new(config: SpringConfig) -> Self
Create a new spring at position 0.0 with target 0.0.
Sourcepub fn from_velocity(
initial: f32,
velocity: f32,
target: f32,
config: SpringConfig,
) -> Self
pub fn from_velocity( initial: f32, velocity: f32, target: f32, config: SpringConfig, ) -> Self
Create a spring at initial, moving with velocity, toward target.
This is useful for fling-to-snap gestures where the release velocity should carry into the settling animation.
Sourcepub fn set_target(&mut self, target: f32)
pub fn set_target(&mut self, target: f32)
Set the target position the spring moves toward.
Sourcepub fn is_settled(&self) -> bool
pub fn is_settled(&self) -> bool
true when the spring has come to rest within epsilon of the target.
Sourcepub fn energy(&self) -> f32
pub fn energy(&self) -> f32
Current kinetic plus potential energy.
This is useful for diagnostics and settle visualization. The units are simulation-relative rather than renderer-specific.
Sourcepub fn overshoot_count(&self) -> u32
pub fn overshoot_count(&self) -> u32
Number of times the spring has crossed its target since the last target change.