Function damp_vec2

Source
pub fn damp_vec2(
    current: Vec2,
    target: Vec2,
    rate: f32,
    dt: f32,
) -> Result<Vec2, MotionError>
Expand description

Exponentially approach a 2D target without overshoot.

Remaining error is multiplied by exp(-rate * dt). rate is in inverse seconds; LN_2 / half_life_seconds gives a desired half-life. A zero target damps velocity toward rest. This bounds output between current and target; it does not impose an acceleration limit (use approach_vec2 for that).

All components must be finite, and rate/dt must be finite and nonnegative, otherwise a MotionError is returned. Valid zero time/rate or an unchanged target preserves the current value bit-for-bit. Wider intermediates preserve small changes and finite results for extreme inputs. See the module docs for rounding/partition limits. response keeps its existing permissive scalar behavior; this helper explicitly rejects invalid inputs.

use ferravex_math::Vec2;
use ferravex_play::motion::damp_vec2;
let velocity = damp_vec2(Vec2::new(8., -4.), Vec2::ZERO,
                         std::f32::consts::LN_2, 1.)?;
assert!(velocity.abs_diff_eq(Vec2::new(4., -2.), 1e-6));