use crate::spring::*;
pub fn inertialize_transition(
off_x: &mut f32,
off_v: &mut f32,
src_x: f32,
src_v: f32,
dst_x: f32,
dst_v: f32,
) {
*off_x = (src_x + *off_x) - dst_x;
*off_v = (src_v + *off_v) - dst_v;
}
pub fn inertialize_update(
out_x: &mut f32,
out_v: &mut f32,
off_x: &mut f32,
off_v: &mut f32,
in_x: f32,
in_v: f32,
halflife: f32,
dt: f32,
) {
decay_spring_damper_exact(off_x, off_v, halflife, dt);
*out_x = in_x + *off_x;
*out_v = in_v + *off_v;
}
#[derive(Default, Debug, Clone, Copy)]
pub struct Inertializer {
pub off_x: f32,
pub off_v: f32,
}
impl Inertializer {
pub fn transition(&mut self, src_x: f32, src_v: f32, dst_x: f32, dst_v: f32) {
inertialize_transition(&mut self.off_x, &mut self.off_v, src_x, src_v, dst_x, dst_v);
}
pub fn update(&mut self, in_x: f32, in_v: f32, halflife: f32, dt: f32) -> (f32, f32) {
let (mut x, mut v) = (0.0, 0.0);
inertialize_update(
&mut x,
&mut v,
&mut self.off_x,
&mut self.off_v,
in_x,
in_v,
halflife,
dt,
);
(x, v)
}
}