Skip to main content

Spring

Struct Spring 

Source
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

Source

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 (try 0.1..0.5)
  • damping: velocity multiplier per tick, < 1.0 (try 0.8..0.95)
Source

pub fn on_settle(self, f: impl FnMut() + 'static) -> Self

Register a callback that runs once when the spring settles.

Source

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

Set the target value the spring will move toward.

Source

pub fn tick(&mut self)

Advance the spring simulation by one tick.

Call this once per frame before reading Spring::value.

Source

pub fn value(&self) -> f64

Return the current spring position.

Source

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.

Auto Trait Implementations§

§

impl Freeze for Spring

§

impl !RefUnwindSafe for Spring

§

impl !Send for Spring

§

impl !Sync for Spring

§

impl Unpin for Spring

§

impl UnsafeUnpin 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> 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, 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.