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
currenttowardstargetby at mostmax_delta(never overshoots). - approach_
degrees - Rotates an angle in degrees towards
targetalong the short way by at mostmax_delta. - damp
- Frame-rate independent exponential smoothing. Larger
lambdais snappier; call every frame with the frame’sdt:x = damp(x, target, 8.0, ctx.dt()). - delta_
degrees - Shortest signed difference
to - fromin 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
vlies betweenaandb(0 ata, 1 atb); 0 whena == b. - lerp
- Linear interpolation:
aatt = 0,batt = 1(not clamped). - remap
- Maps
vfrom the rangein_a..in_btoout_a..out_b(not clamped). - remap_
clamped - Like
remapbut the result never leavesout_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_timeis roughly the seconds to arrive. - smoothstep
- Hermite smoothstep between two edges (0 below
e0, 1 abovee1). - wrap_
degrees - Wraps an angle in degrees into
-180..180.