use crate::{convert::*, spring::*};
pub fn double_spring_damper_exact(
x: &mut f32,
v: &mut f32,
xi: &mut f32,
vi: &mut f32,
x_goal: f32,
halflife: f32,
dt: f32,
) {
simple_spring_damper_exact(xi, vi, x_goal, 0.5 * halflife, dt);
simple_spring_damper_exact(x, v, *xi, 0.5 * halflife, dt);
}
pub fn timed_spring_damper_exact(
x: &mut f32,
v: &mut f32,
xi: &mut f32,
x_goal: f32,
t_goal: f32,
halflife: f32,
dt: f32,
) {
if dt <= 0.0 {
return;
}
let min_time = if t_goal > dt { t_goal } else { dt };
let v_goal = (x_goal - *xi) / min_time;
let t_goal_future = halflife_to_lag(halflife);
let x_goal_future = if t_goal_future < t_goal {
*xi + v_goal * t_goal_future
} else {
x_goal
};
simple_spring_damper_exact(x, v, x_goal_future, halflife, dt);
*xi += v_goal * dt;
}
pub fn velocity_spring_damper_exact(
x: &mut f32,
v: &mut f32,
xi: &mut f32,
x_goal: f32,
v_goal: f32,
halflife: f32,
dt: f32,
eps: f32,
) {
if dt <= 0.0 {
return;
}
let _ = eps;
let x_diff = (if x_goal - *xi > 0.0 { 1.0 } else { -1.0 }) * v_goal;
let t_goal_future = halflife_to_lag(halflife);
let x_goal_future = if (x_goal - *xi).abs() > t_goal_future * v_goal {
*xi + x_diff * t_goal_future
} else {
x_goal
};
simple_spring_damper_exact(x, v, x_goal_future, halflife, dt);
*xi = if (x_goal - *xi).abs() > dt * v_goal {
*xi + x_diff * dt
} else {
x_goal
};
}
pub fn spring_energy(x: f32, v: f32, frequency: f32, x_rest: f32, v_rest: f32, scale: f32) -> f32 {
let s = frequency_to_stiffness(frequency);
(squaref(scale * (v - v_rest)) + s * squaref(scale * (x - x_rest))) / 2.0
}
pub fn resonant_frequency(goal_frequency: f32, halflife: f32) -> f32 {
let d = halflife_to_damping(halflife, DEFAULT_EPS);
stiffness_to_frequency(frequency_to_stiffness(goal_frequency) - (d * d) / 4.0)
}