Struct Spring

Source
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 overshoot

Fields§

§value: f32§velocity: f32§target: f32

Implementations§

Source§

impl Spring

Source

pub fn new(value: f32, frequency: f32, damping: f32) -> Spring

Spring at rest at value. Invalid parameters fall back to 1 Hz / ratio 1.

Source

pub fn critical(value: f32, frequency: f32) -> Spring

Critically damped: settles as fast as possible without overshoot.

Source

pub fn underdamped(value: f32, frequency: f32, damping: f32) -> Spring

Bouncy spring with damping ratio in (0, 1) (e.g. 0.3–0.6).

Source

pub fn set_target(&mut self, target: f32)

Source

pub fn impulse(&mut self, velocity: f32)

Adds instantaneous velocity (a “kick” for pops and shakes).

Source

pub fn snap(&mut self, value: f32)

Places the spring at value with no velocity.

Source

pub fn is_settled(&self, epsilon: f32) -> bool

Within epsilon of the target with velocity below epsilon.

Source

pub fn advance(&mut self, dt: f32, motion: Motion) -> f32

Advances by dt and returns the value. Reduced motion snaps to target.

Trait Implementations§

Source§

impl Clone for Spring

Source§

fn clone(&self) -> Spring

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Spring

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Spring

Source§

fn eq(&self, other: &Spring) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Spring

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.