Crates.io | damped-springs |
lib.rs | damped-springs |
version | 0.1.2 |
source | src |
created_at | 2024-12-08 21:12:12.249906 |
updated_at | 2024-12-09 00:10:39.957027 |
description | Implementation of damped springs for smooth and springy motion. |
homepage | |
repository | |
max_upload_size | |
id | 1476691 |
size | 19,441 |
Implementation of damped springs for smooth and springy motion.
Adapted from Ryan Juckett's Damped Springs. License included in source.
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.
Spring
is simply the current state of a spring that is being simulated.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.SpringTimeStep
also has pre-computed coefficients, but is now
specific to a particular time step interval.