use crate::reactive::Signal;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SpringConfig {
pub stiffness: f32, pub damping: f32, pub mass: f32, pub rest_displacement_threshold: f32,
pub rest_velocity_threshold: f32,
}
impl Default for SpringConfig {
fn default() -> Self {
Self {
stiffness: 170.0,
damping: 26.0,
mass: 1.0,
rest_displacement_threshold: 0.001,
rest_velocity_threshold: 0.001,
}
}
}
impl SpringConfig {
pub const fn stiff() -> Self {
Self {
stiffness: 210.0,
damping: 20.0,
mass: 1.0,
rest_displacement_threshold: 0.001,
rest_velocity_threshold: 0.001,
}
}
pub const fn gentle() -> Self {
Self {
stiffness: 120.0,
damping: 14.0,
mass: 1.0,
rest_displacement_threshold: 0.001,
rest_velocity_threshold: 0.001,
}
}
pub const fn wobbly() -> Self {
Self {
stiffness: 180.0,
damping: 12.0,
mass: 1.0,
rest_displacement_threshold: 0.001,
rest_velocity_threshold: 0.001,
}
}
pub const fn bouncy() -> Self {
Self {
stiffness: 240.0,
damping: 10.0,
mass: 1.0,
rest_displacement_threshold: 0.001,
rest_velocity_threshold: 0.001,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
struct SpringState {
position: f32,
velocity: f32,
target: f32,
}
#[derive(Clone)]
pub struct SpringSignal {
config: SpringConfig,
state: Rc<RefCell<SpringState>>,
signal: Signal<f32>,
}
impl SpringSignal {
pub fn new(initial: f32) -> Self {
Self::with_config(initial, SpringConfig::default())
}
pub fn with_config(initial: f32, config: SpringConfig) -> Self {
Self {
config,
state: Rc::new(RefCell::new(SpringState {
position: initial,
velocity: 0.0,
target: initial,
})),
signal: Signal::new(initial),
}
}
#[inline]
pub fn get(&self) -> f32 {
self.signal.get()
}
#[inline]
pub fn untracked_get(&self) -> f32 {
self.signal.untracked_get()
}
#[inline]
pub fn target(&self) -> f32 {
self.state.borrow().target
}
#[inline]
pub fn velocity(&self) -> f32 {
self.state.borrow().velocity
}
pub fn set_target(&self, target: f32) {
let mut st = self.state.borrow_mut();
st.target = target;
}
pub fn snap_to(&self, value: f32) {
{
let mut st = self.state.borrow_mut();
st.position = value;
st.target = value;
st.velocity = 0.0;
}
self.signal.set(value);
}
pub fn is_at_rest(&self) -> bool {
let st = self.state.borrow();
(st.position - st.target).abs() < self.config.rest_displacement_threshold
&& st.velocity.abs() < self.config.rest_velocity_threshold
}
pub fn tick(&self, dt: f32) -> bool {
let mut st = self.state.borrow_mut();
if (st.position - st.target).abs() < self.config.rest_displacement_threshold
&& st.velocity.abs() < self.config.rest_velocity_threshold
{
if st.position != st.target || st.velocity != 0.0 {
st.position = st.target;
st.velocity = 0.0;
drop(st);
self.signal.set(self.target());
}
return false;
}
const SUB_STEPS: usize = 4;
let sub_dt = dt.clamp(0.0001, 0.1) / (SUB_STEPS as f32);
for _ in 0..SUB_STEPS {
let displacement = st.position - st.target;
let spring_force = -self.config.stiffness * displacement;
let damping_force = -self.config.damping * st.velocity;
let total_force = spring_force + damping_force;
let acceleration = total_force / self.config.mass.max(0.001);
st.velocity += acceleration * sub_dt;
st.position += st.velocity * sub_dt;
}
if (st.position - st.target).abs() < self.config.rest_displacement_threshold
&& st.velocity.abs() < self.config.rest_velocity_threshold
{
st.position = st.target;
st.velocity = 0.0;
let final_pos = st.position;
drop(st);
self.signal.set(final_pos);
return false;
}
let new_pos = st.position;
drop(st);
self.signal.set(new_pos);
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_spring_signal_rest_convergence() {
let spring = SpringSignal::new(0.0);
assert_eq!(spring.get(), 0.0);
assert!(spring.is_at_rest());
spring.set_target(100.0);
assert!(!spring.is_at_rest());
assert_eq!(spring.target(), 100.0);
let mut steps = 0;
while steps < 200 && spring.tick(0.016) {
steps += 1;
}
assert!(spring.is_at_rest(), "Spring must converge to rest");
assert!(
(spring.get() - 100.0).abs() < 1e-3,
"Position must reach target 100.0"
);
assert_eq!(spring.velocity(), 0.0);
}
#[test]
fn test_spring_signal_snap_to() {
let spring = SpringSignal::new(10.0);
spring.snap_to(500.0);
assert_eq!(spring.get(), 500.0);
assert_eq!(spring.target(), 500.0);
assert_eq!(spring.velocity(), 0.0);
assert!(spring.is_at_rest());
}
}