menu_bookDocumentation
s&box MathX: Remap, LerpInverse, ExponentialDecay, SmoothDamp, SpringDamp, DeltaDegrees, and Unit Conversion
s&box MathX: Math Utility Extensions for Game Development
MathX is a static class providing math utilities not in System.Math or System.MathF. Most methods are also extension methods, so you can call them directly on values.Interpolation
CSHARP
// Linear interpolation (clamped by default)
float result = MathX.Lerp( 0f, 100f, 0.5f ); // 50
float result = from.LerpTo( to, fraction );
// Inverse lerp — get the fraction from a value
float t = value.LerpInverse( 0f, 100f ); // 0.5 if value == 50
// Remap from one range to another (clamped by default)
float mapped = value.Remap( 0f, 100f, 0f, 1f );
// Angle interpolation (takes shortest arc, handles 350→10 correctly)
float angle = MathX.LerpDegrees( 350f, 10f, 0.5f ); // 0 (not 180)Smoothing Functions
Three smoothing options with different trade-offs:
CSHARP
// Exponential decay — cheapest, no velocity tracking, good for UI
float smoothed = MathX.ExponentialDecay( current, target, halflife: 0.1f, Time.Delta );
// SmoothDamp — spring-based, tracks velocity for momentum (like Unity's SmoothDamp)
float velocity = 0f;
float smoothed = MathX.SmoothDamp( current, target, ref velocity, smoothTime: 0.1f, Time.Delta );
// SpringDamp — spring with configurable frequency and damping ratio
float velocity = 0f;
float smoothed = MathX.SpringDamp( current, target, ref velocity, Time.Delta, frequency: 2f, damping: 0.5f );Approach
CSHARP
// Move toward target by delta, stop at target (no overshoot)
float result = current.Approach( target, delta );Angle Utilities
CSHARP
// Normalize to [0, 360]
float normalized = angle.NormalizeDegrees();
// Difference between two angles, always [-180, +180]
float delta = MathX.DeltaDegrees( from, to );
// Unsigned modulo (always positive, unlike C# %)
float mod = value.UnsignedMod( 360f );Unit Conversion
CSHARP
float meters = inches.InchToMeter();
float inches = meters.MeterToInch();
float radians = degrees.DegreeToRadian();
float degrees = radians.RadianToDegree();Grid Snapping
CSHARP
float snapped = value.SnapToGrid( 16f ); // snap to 16-unit grid
int snapped = intValue.SnapToGrid( 8 );
Was this helpful?