pub struct Spring { /* private fields */ }Expand description
Spring physics animation that settles toward a target value.
Models a damped harmonic oscillator. Call Spring::set_target to change
the goal, then call Spring::tick once per frame to advance the
simulation. Read the current position with Spring::value.
Tune behavior with stiffness (how fast it accelerates toward the target)
and damping (how quickly oscillations decay). A damping value close to
1.0 is overdamped (no oscillation); lower values produce more bounce.
§Example
use slt::Spring;
let mut spring = Spring::new(0.0, 0.2, 0.85);
spring.set_target(100.0);
for _ in 0..200 {
spring.tick();
if spring.is_settled() { break; }
}
assert!((spring.value() - 100.0).abs() < 0.01);Implementations§
Source§impl Spring
impl Spring
Sourcepub fn new(initial: f64, stiffness: f64, damping: f64) -> Self
pub fn new(initial: f64, stiffness: f64, damping: f64) -> Self
Create a new spring at initial position with the given physics parameters.
stiffness: acceleration per unit of displacement (try0.1..0.5)damping: velocity multiplier per tick,< 1.0(try0.8..0.95)
Sourcepub fn on_settle(self, f: impl FnMut() + 'static) -> Self
pub fn on_settle(self, f: impl FnMut() + 'static) -> Self
Register a callback that runs once when the spring settles.
Sourcepub fn set_target(&mut self, target: f64)
pub fn set_target(&mut self, target: f64)
Set the target value the spring will move toward.
Sourcepub fn tick(&mut self)
pub fn tick(&mut self)
Advance the spring simulation by one tick.
Call this once per frame before reading Spring::value.
Sourcepub fn is_settled(&self) -> bool
pub fn is_settled(&self) -> bool
Returns true if the spring has effectively settled at its target.
Settled means both the distance to target and the velocity are below
0.01.