use super::{Approximation, Curve};
use num::Float;
#[derive(Copy, Clone, Debug)]
pub struct Decay<T>
where
T: Float,
{
pub from_value: T,
pub to_value: T,
pub time_constant: T,
}
impl<T> Decay<T>
where
T: Float,
{
pub fn ideal_target(from_value: T, power: T, velocity: T) -> T {
from_value + power * velocity
}
}
impl<T> Curve<T> for Decay<T>
where
T: Float,
{
fn approximate(&self, time: T) -> Approximation<T> {
let amplitude = self.to_value - self.from_value;
let delta = -amplitude * (-time / self.time_constant).exp();
Approximation {
time,
value: self.to_value + delta,
velocity: T::zero(),
}
}
fn target(&self) -> T {
self.to_value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ideal_target() {
assert_eq!(Decay::ideal_target(4.0, 0.8, 32.0), 4.0 + 0.8 * 32.0);
}
}