Function approach_vec2

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

Approach a 2D target by at most max_rate * dt in Euclidean distance.

Use velocities for current/target and acceleration (units/s²) for max_rate to accelerate, brake or steer without overshooting. Position inputs instead use a speed (units/s). A reachable target is returned exactly. All vector components must be finite; max_rate and dt (seconds) 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 keep results finite even when the difference or step exceeds f32::MAX. See the module docs for rounding/partition limits.

use ferravex_math::Vec2;
use ferravex_play::motion::approach_vec2;
let velocity = approach_vec2(Vec2::ZERO, Vec2::new(3., 4.), 2., 0.5)?;
assert!(velocity.abs_diff_eq(Vec2::new(0.6, 0.8), 1e-6));
assert_eq!(approach_vec2(velocity, Vec2::ZERO, 4., 1.)?, Vec2::ZERO);