#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SpringConfig {
pub stiffness: f32,
pub damping: f32,
pub mass: f32,
pub epsilon: f32,
}
impl Default for SpringConfig {
fn default() -> Self {
Self {
stiffness: 100.0,
damping: 10.0,
mass: 1.0,
epsilon: 0.001,
}
}
}
impl SpringConfig {
pub fn critically_damped(stiffness: f32) -> Self {
let stiffness = stiffness.max(0.0);
let mass = 1.0;
Self {
stiffness,
damping: 2.0 * animato_core::math::sqrt(stiffness * mass),
mass,
epsilon: 0.001,
}
}
pub fn overdamped(stiffness: f32, ratio: f32) -> Self {
let mut config = Self::critically_damped(stiffness);
config.damping *= ratio.max(1.0001);
config
}
pub fn underdamped(stiffness: f32, ratio: f32) -> Self {
let mut config = Self::critically_damped(stiffness);
config.damping *= ratio.clamp(0.0, 1.0);
config
}
pub fn gentle() -> Self {
Self {
stiffness: 60.0,
damping: 14.0,
mass: 1.0,
epsilon: 0.001,
}
}
pub fn wobbly() -> Self {
Self {
stiffness: 180.0,
damping: 12.0,
mass: 1.0,
epsilon: 0.001,
}
}
pub fn stiff() -> Self {
Self {
stiffness: 210.0,
damping: 20.0,
mass: 1.0,
epsilon: 0.001,
}
}
pub fn slow() -> Self {
Self {
stiffness: 37.0,
damping: 14.0,
mass: 1.0,
epsilon: 0.001,
}
}
pub fn snappy() -> Self {
Self {
stiffness: 300.0,
damping: 30.0,
mass: 1.0,
epsilon: 0.001,
}
}
}