pub struct Spring {
pub stiffness: f64,
pub damping: f64,
pub mass: f64,
pub initial_velocity: f64,
pub rest_threshold: f64,
}Expand description
Spring configuration with physics-based parameters
§Example
use uzor_animation::Spring;
let spring = Spring::new()
.stiffness(180.0)
.damping(12.0)
.mass(1.0);
let (position, velocity) = spring.evaluate(0.1);Fields§
§stiffness: f64Spring stiffness (rigidity). Higher = more sudden movement. Default: 100.0
damping: f64Damping coefficient (friction). Dissipates energy, slows oscillation. Default: 10.0
mass: f64Mass of the animated object. Higher = more lethargic movement. Default: 1.0
initial_velocity: f64Initial velocity (speed at t=0). Default: 0.0
rest_threshold: f64Rest threshold - animation stops when |displacement| + |velocity| < threshold. Default: 0.001
Implementations§
Source§impl Spring
impl Spring
Sourcepub fn initial_velocity(self, v: f64) -> Self
pub fn initial_velocity(self, v: f64) -> Self
Set initial velocity
Sourcepub fn rest_threshold(self, t: f64) -> Self
pub fn rest_threshold(self, t: f64) -> Self
Set rest threshold
Sourcepub fn damping_ratio(&self) -> f64
pub fn damping_ratio(&self) -> f64
Calculate damping ratio: ζ = damping / (2 * sqrt(stiffness * mass))
Determines oscillation behavior:
- ζ < 1: Under-damped (bouncy, oscillates)
- ζ = 1: Critically damped (fastest settling, no overshoot)
- ζ > 1: Over-damped (slow, no oscillation)
Sourcepub fn angular_frequency(&self) -> f64
pub fn angular_frequency(&self) -> f64
Calculate angular frequency: ω₀ = sqrt(stiffness / mass)
Sourcepub fn evaluate(&self, t: f64) -> (f64, f64)
pub fn evaluate(&self, t: f64) -> (f64, f64)
Evaluate spring position and velocity at time t (seconds from start)
Returns (position, velocity) where:
- position: displacement from target (1.0 at start, 0.0 at rest)
- velocity: rate of change
Uses analytical solution for damped harmonic oscillator.
Sourcepub fn is_at_rest(&self, t: f64) -> bool
pub fn is_at_rest(&self, t: f64) -> bool
Check if spring is at rest at time t
Sourcepub fn estimated_duration(&self) -> f64
pub fn estimated_duration(&self) -> f64
Estimate duration until spring reaches rest
Uses exponential decay envelope to estimate settling time. For under-damped and critically damped springs, the envelope decays as e^(-ζω₀t). We solve for when envelope < threshold.
Sourcepub fn gentle() -> Self
pub fn gentle() -> Self
Gentle spring - iOS-style default
Smooth, natural feel with subtle bounce.
Sourcepub fn bouncy() -> Self
pub fn bouncy() -> Self
Bouncy spring - noticeable overshoot
Playful animation with visible oscillation.
Sourcepub fn as_easing(&self, samples: usize) -> Vec<f64>
pub fn as_easing(&self, samples: usize) -> Vec<f64>
Convert spring to a lookup table for use as an easing function
Returns vector of position values sampled over estimated duration. Each sample represents spring position at t = i * (duration / samples).
Can be used to create an easing curve from spring physics.