pub struct Spring { /* private fields */ }Expand description
A damped spring of unit mass, set by (duration, bounce). The fields are private so the
invariants hold: the duration is 0 (no spring, see Spring::NONE) or within
Spring::MIN_DURATION..=Spring::MAX_DURATION, and the bounce is within
-MAX_BOUNCE..=MAX_BOUNCE.
Implementations§
Source§impl Spring
impl Spring
Sourcepub const MIN_DURATION: f32 = 0.01
pub const MIN_DURATION: f32 = 0.01
The shortest duration of a spring that moves, in seconds.
Sourcepub const MAX_DURATION: f32 = 60.0
pub const MAX_DURATION: f32 = 60.0
The longest duration, in seconds.
Sourcepub const MAX_BOUNCE: f32 = 0.95
pub const MAX_BOUNCE: f32 = 0.95
The largest bounce either way.
Sourcepub const fn new(duration: f32, bounce: f32) -> Spring
pub const fn new(duration: f32, bounce: f32) -> Spring
A spring of duration seconds and bounce (−0.95..=0.95). Never panics: a NaN duration
gives the default 0.3 s, zero or negative gives Spring::NONE, other values are clamped
to MIN_DURATION..=MAX_DURATION; a NaN bounce is 0 and others are clamped.
Sourcepub const fn from_response(response: f32, damping_ratio: f32) -> Spring
pub const fn from_response(response: f32, damping_ratio: f32) -> Spring
A spring from a response (the duration) and a damping ratio ζ (1 = critical, below 1
bounces). Clamped like Spring::new; a NaN ratio is 1.
Sourcepub const fn duration(self) -> f32
pub const fn duration(self) -> f32
The duration in seconds (0 for Spring::NONE).
Sourcepub fn damping_ratio(self) -> f32
pub fn damping_ratio(self) -> f32
The damping ratio ζ.
Sourcepub fn stiffness(self) -> f32
pub fn stiffness(self) -> f32
The stiffness k = ω² for unit mass (0 for Spring::NONE).
Sourcepub fn damping(self) -> f32
pub fn damping(self) -> f32
The damping coefficient c = 2ζω for unit mass (0 for Spring::NONE).
Sourcepub fn sample(self, x0: f32, v0: f32, target: f32, t: f32) -> (f32, f32)
pub fn sample(self, x0: f32, v0: f32, target: f32, t: f32) -> (f32, f32)
Position and velocity t seconds after starting at x0 with velocity v0, pulled to
target. Closed form for all three damping regimes. Non-finite inputs are replaced
(position by the target, velocity by 0, a bad target by the start), t ≤ 0 returns the
start, and the result is always finite.
Sourcepub fn settle_time(self, from: f32, v0: f32, target: f32, eps: Eps) -> f32
pub fn settle_time(self, from: f32, v0: f32, target: f32, eps: Eps) -> f32
The time in seconds after which a motion from (from, v0) to target stays within
eps (position and velocity) for good. Never earlier than the true settle (it is the last
crossing of a bound of the motion), at most MAX_SETTLE, and 0 when already settled.
Sourcepub fn settle_time_lanes(
self,
lanes: impl IntoIterator<Item = (f32, f32, f32)>,
eps: Eps,
) -> f32
pub fn settle_time_lanes( self, lanes: impl IntoIterator<Item = (f32, f32, f32)>, eps: Eps, ) -> f32
The settle time of several lanes (from, v0, target) moving together: one bound over all
of them (each coefficient of the bound is the largest of any lane’s), so it is never
earlier than any lane’s settle and costs one root solve.