damped-springs

Crates.iodamped-springs
lib.rsdamped-springs
version0.1.2
sourcesrc
created_at2024-12-08 21:12:12.249906
updated_at2024-12-09 00:10:39.957027
descriptionImplementation of damped springs for smooth and springy motion.
homepage
repository
max_upload_size
id1476691
size19,441
Zander (voximity)

documentation

README

damped-springs

Implementation of damped springs for smooth and springy motion.

Adapted from Ryan Juckett's Damped Springs. License included in source.

Usage

See the examples.

First, start by configuring your spring:

// all spring types are generic over `f32` and `f64`!
let config = SpringConfig::new(5.0, 0.5);

Then, pre-compute some spring math:

let params = SpringParams::from(config);

Create a spring or two:

let spring_x = Spring::from_equilibrium(1.0);
let spring_y = Spring::from_equilibrium(2.0);

Then, update your springs either on a fixed time step or on a varying interval (if you're using a game engine):

// fixed time step of 0.1s
let time_step = SpringTimeStep::new(params, 0.1);
loop {
    spring_x.update(time_step);
    spring_y.update(time_step);
    println!("boing! ({}, {})", spring_x.position, spring_y.position);
}

// varying time step
loop {
    let time_step = SpringTimeStep::new(params, delta_time);
    spring_x.update(time_step);
    spring_y.update(time_step);
    println!("boing! ({}, {})", spring_x.position, spring_y.position);
}

Or, if you're only updating one spring at a varying time interval...

loop {
    spring.update_single(params, delta_time);
    // this creates the `SpringTimeStep` for you!
}

Working with a spring per dimension? See the collection example.

Why so many types?

  • A Spring is simply the current state of a spring that is being simulated.
  • A SpringConfig describes the user-level parameters of (a) spring(s), like its angular frequency and damping ratio.
  • SpringParams includes pre-computed coefficients to perform efficient spring updating later. It is pre-computed up to the point of delta time.
  • Finally, SpringTimeStep also has pre-computed coefficients, but is now specific to a particular time step interval.
Commit count: 0

cargo fmt