Skip to main content

Module math

Module math 

Source
Expand description

Small scalar helpers for gameplay scripts: interpolation, smoothing and angles.

All functions are f32, allocation-free and safe for any finite input.

use kvas_engine::math::*;
assert_eq!(lerp(0.0, 10.0, 0.25), 2.5);
assert_eq!(remap(5.0, 0.0, 10.0, 100.0, 200.0), 150.0);
assert_eq!(approach(0.0, 10.0, 3.0), 3.0);
assert!((wrap_degrees(190.0) + 170.0).abs() < 1e-4);

Functions§

approach
Moves current towards target by at most max_delta (never overshoots).
approach_degrees
Rotates an angle in degrees towards target along the short way by at most max_delta.
damp
Frame-rate independent exponential smoothing. Larger lambda is snappier; call every frame with the frame’s dt: x = damp(x, target, 8.0, ctx.dt()).
delta_degrees
Shortest signed difference to - from in degrees (-180..180).
ease_smooth
Smooth S curve 3t² - 2t³ of a value clamped to 0..1.
ease_smoother
Even smoother S curve 6t⁵ - 15t⁴ + 10t³ with zero acceleration at both ends.
inverse_lerp
Where v lies between a and b (0 at a, 1 at b); 0 when a == b.
lerp
Linear interpolation: a at t = 0, b at t = 1 (not clamped).
remap
Maps v from the range in_a..in_b to out_a..out_b (not clamped).
remap_clamped
Like remap but the result never leaves out_a..out_b.
smooth_damp
Critically damped spring towards target (like a camera “smooth damp”): returns the new value and the new velocity. Keep the velocity between calls. smooth_time is roughly the seconds to arrive.
smoothstep
Hermite smoothstep between two edges (0 below e0, 1 above e1).
wrap_degrees
Wraps an angle in degrees into -180..180.