use crate::geometry::{Rect, Vec2, vec2};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Tuning {
pub response: f32,
pub damping: f32,
}
impl Tuning {
pub const fn new(response: f32, damping: f32) -> Self {
Self { response, damping }
}
pub const SNAP: Tuning = Tuning::new(0.22, 1.0);
pub const SMOOTH: Tuning = Tuning::new(0.38, 0.86);
pub const PRESS: Tuning = Tuning::new(0.36, 0.62);
pub const BOUNCY: Tuning = Tuning::new(0.48, 0.68);
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Spring {
pub value: f32,
pub velocity: f32,
started: bool,
}
impl Spring {
pub fn new(value: f32) -> Self {
Self { value, velocity: 0.0, started: true }
}
pub fn step(&mut self, target: f32, tuning: Tuning, dt: f32) -> bool {
if !self.started {
*self = Spring::new(target);
return false;
}
let w = std::f32::consts::TAU / tuning.response.max(0.01);
let z = tuning.damping.clamp(0.05, 1.0);
let x0 = self.value - target;
let v0 = self.velocity;
let (x, v) = if z > 0.999 {
let c = v0 + w * x0;
let e = (-w * dt).exp();
((x0 + c * dt) * e, (v0 - w * c * dt) * e)
} else {
let wd = w * (1.0 - z * z).sqrt();
let e = (-z * w * dt).exp();
let (s, c) = (wd * dt).sin_cos();
let b = (v0 + z * w * x0) / wd;
(e * (x0 * c + b * s), e * (v0 * c - (z * w * b + wd * x0) * s))
};
self.value = target + x;
self.velocity = v;
let scale = 1.0 + target.abs() * 0.02;
if x.abs() < 0.0008 * scale && v.abs() < 0.01 * scale {
self.value = target;
self.velocity = 0.0;
return false;
}
true
}
pub fn set(&mut self, value: f32) {
*self = Spring::new(value);
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Spring2d {
pub x: Spring,
pub y: Spring,
}
impl Spring2d {
pub fn new(value: Vec2) -> Self {
Self { x: Spring::new(value.x), y: Spring::new(value.y) }
}
pub fn value(&self) -> Vec2 {
vec2(self.x.value, self.y.value)
}
pub fn velocity(&self) -> Vec2 {
vec2(self.x.velocity, self.y.velocity)
}
pub fn step(&mut self, target: Vec2, tuning: Tuning, dt: f32) -> bool {
self.x.step(target.x, tuning, dt) | self.y.step(target.y, tuning, dt)
}
pub fn set(&mut self, value: Vec2) {
*self = Self::new(value);
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct SpringRect {
pub center: Spring2d,
pub size: Spring2d,
}
impl SpringRect {
pub fn new(rect: Rect) -> Self {
Self { center: Spring2d::new(rect.center()), size: Spring2d::new(rect.size()) }
}
pub fn step(&mut self, target: Rect, center: Tuning, size: Tuning, dt: f32) -> bool {
self.center.step(target.center(), center, dt) | self.size.step(target.size(), size, dt)
}
pub fn rect(&self) -> Rect {
Rect::from_center_size(self.center.value(), self.size.value().max(Vec2::splat(1.0)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn first_step_snaps() {
let mut s = Spring::default();
assert!(!s.step(5.0, Tuning::PRESS, 0.016));
assert_eq!(s.value, 5.0);
}
#[test]
fn frame_rate_independent() {
let run = |dt: f32, n: usize| {
let mut s = Spring::new(0.0);
for _ in 0..n {
s.step(1.0, Tuning::PRESS, dt);
}
s.value
};
assert!((run(1.0 / 60.0, 12) - run(1.0 / 240.0, 48)).abs() < 1e-3);
}
#[test]
fn settles_and_overshoots_only_when_underdamped() {
let peak = |t: Tuning| {
let mut s = Spring::new(0.0);
let mut peak: f32 = 0.0;
for _ in 0..400 {
s.step(1.0, t, 1.0 / 120.0);
peak = peak.max(s.value);
}
assert_eq!(s.value, 1.0);
peak
};
assert!(peak(Tuning::PRESS) > 1.02);
assert!(peak(Tuning::SNAP) <= 1.0001);
}
#[test]
fn survives_huge_frames() {
let mut s = Spring::new(0.0);
s.step(100.0, Tuning::BOUNCY, 5.0);
assert!((s.value - 100.0).abs() < 1.0);
}
}