pub struct Spring {
pub value: f32,
pub velocity: f32,
pub target: f32,
/* private fields */
}Expand description
A damped harmonic spring toward target, integrated analytically so any
dt is stable and splitting a step gives the same result (up to rounding).
frequency is the undamped angular speed in Hz; damping is the damping
ratio: 1 is critically damped (fastest without overshoot), below 1
overshoots and wobbles, above 1 is sluggish.
use ferravex_play::tween::{Motion, Spring};
let mut hover = Spring::critical(0., 8.);
hover.set_target(1.);
let mut peak: f32 = 0.;
for _ in 0..120 { peak = peak.max(hover.advance(1. / 60., Motion::Full)); }
assert!(peak <= 1. && hover.is_settled(1e-3));
let mut pop = Spring::underdamped(0., 5., 0.3);
pop.set_target(1.);
let mut peak: f32 = 0.;
for _ in 0..120 { peak = peak.max(pop.advance(1. / 60., Motion::Full)); }
assert!(peak > 1.2); // springy overshootFields§
§value: f32§velocity: f32§target: f32Implementations§
Source§impl Spring
impl Spring
Sourcepub fn new(value: f32, frequency: f32, damping: f32) -> Spring
pub fn new(value: f32, frequency: f32, damping: f32) -> Spring
Spring at rest at value. Invalid parameters fall back to 1 Hz / ratio 1.
Sourcepub fn critical(value: f32, frequency: f32) -> Spring
pub fn critical(value: f32, frequency: f32) -> Spring
Critically damped: settles as fast as possible without overshoot.
Sourcepub fn underdamped(value: f32, frequency: f32, damping: f32) -> Spring
pub fn underdamped(value: f32, frequency: f32, damping: f32) -> Spring
Bouncy spring with damping ratio in (0, 1) (e.g. 0.3–0.6).
pub fn set_target(&mut self, target: f32)
Sourcepub fn impulse(&mut self, velocity: f32)
pub fn impulse(&mut self, velocity: f32)
Adds instantaneous velocity (a “kick” for pops and shakes).
Sourcepub fn is_settled(&self, epsilon: f32) -> bool
pub fn is_settled(&self, epsilon: f32) -> bool
Within epsilon of the target with velocity below epsilon.
Trait Implementations§
impl Copy for Spring
impl StructuralPartialEq for Spring
Auto Trait Implementations§
impl Freeze for Spring
impl RefUnwindSafe for Spring
impl Send for Spring
impl Sync for Spring
impl Unpin for Spring
impl UnwindSafe for Spring
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more