use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DampedResponse {
pub position: f32,
pub velocity: f32,
pub zeta: f32,
pub omega: f32,
}
impl DampedResponse {
#[must_use]
pub fn new(zeta: f32, omega: f32) -> Self {
Self {
position: 0.0,
velocity: 0.0,
zeta: zeta.max(0.01),
omega: omega.max(0.01),
}
}
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
pub fn impulse(&mut self, force: f32) {
self.velocity += force;
}
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
pub fn step(&mut self, dt: f32) {
let accel =
-2.0 * self.zeta * self.omega * self.velocity - self.omega * self.omega * self.position;
self.velocity += accel * dt;
self.position += self.velocity * dt;
}
#[must_use]
pub fn is_settled(&self, threshold: f32) -> bool {
self.position.abs() < threshold && self.velocity.abs() < threshold
}
}