Skip to content

MathHelper

Shmellyorc edited this page Aug 31, 2026 · 3 revisions

MathHelper provides a comprehensive collection of mathematical utility functions for interpolation, clamping, wrapping, conversion, and common game math operations.


Constants

Constant Value Description
PI 3.14159265f Pi constant
TwoPI 6.28318531f Two times Pi
HalfPI 1.57079633f Pi divided by two
QuarterPI 0.78539816f Pi divided by four
InvPI 0.31830989f One divided by Pi
DegToRad 0.01745329f Degrees to radians conversion factor
RadToDeg 57.2957795f Radians to degrees conversion factor
Epsilon 0.001f Epsilon value for floating-point comparisons

Angle Conversion

ToRadians

Converts degrees to radians.

float radians = MathHelper.ToRadians(90f);  // 1.5708 (PI/2)

ToDegrees

Converts radians to degrees.

float degrees = MathHelper.ToDegrees(MathHelper.PI);  // 180

Interpolation

Lerp

Linearly interpolates between two values.

float value = MathHelper.Lerp(0f, 10f, 0.5f);  // 5f

SmoothStep

Performs smooth Hermite interpolation between two values.

float value = MathHelper.SmoothStep(0f, 10f, 0.5f);  // 5f (with smooth easing)

InverseLerp

Calculates the inverse interpolation factor between two values.

float t = MathHelper.InverseLerp(0f, 10f, 5f);  // 0.5f

Remap

Remaps a value from one range to another.

float value = MathHelper.Remap(5f, 0f, 10f, 0f, 100f);  // 50f

MoveTowards

Moves a value towards a target by a maximum delta.

float value = MathHelper.MoveTowards(5f, 10f, 2f);  // 7f

Clamping

Clamp

Clamps a value between a minimum and maximum.

float value = MathHelper.Clamp(15f, 0f, 10f);  // 10f

Saturate

Clamps a value between 0 and 1.

float value = MathHelper.Saturate(1.5f);  // 1f

Center

Calculates the center offset between two values.

float center = MathHelper.Center(100f, 50f, false);  // 25f
float centerClamped = MathHelper.Center(100f, 50f, true);  // 25f (rounded)

Wrapping

Wrap (Float)

Wraps a floating-point value within a specified range.

float value = MathHelper.Wrap(5f, 0f, 3f);  // 2f
float value2 = MathHelper.Wrap(-1f, 0f, 3f);  // 2f

Wrap (Int)

Wraps an integer value within a specified range.

int value = MathHelper.Wrap(5, 0, 3);  // 2
int value2 = MathHelper.Wrap(-1, 0, 3);  // 2

PingPong

Ping-pongs a value between 0 and a specified length.

float value = MathHelper.PingPong(5f, 3f);  // 1f (bounces between 0 and 3)

Direction and Angle

DirectionToAngle

Converts a direction vector to an angle in radians.

float angle = MathHelper.DirectionToAngle(new Vect2(1f, 0f));  // 0f
float angle2 = MathHelper.DirectionToAngle(new Vect2(0f, 1f));  // PI/2

AngleToDirection

Converts an angle in radians to a direction vector.

Vect2 direction = MathHelper.AngleToDirection(0f);  // (1, 0)
Vect2 direction2 = MathHelper.AngleToDirection(MathHelper.HalfPI);  // (0, 1)

Rounding

RoundToInt

Rounds a floating-point value to the nearest integer.

int value = MathHelper.RoundToInt(3.7f);  // 4

FloorToInt

Floors a floating-point value to the nearest integer.

int value = MathHelper.FloorToInt(3.7f);  // 3

CeilToInt

Ceils a floating-point value to the nearest integer.

int value = MathHelper.CeilToInt(3.2f);  // 4

Snap

Snaps a value to the nearest multiple of a grid size.

float value = MathHelper.Snap(12.3f, 5f);  // 10f
float value2 = MathHelper.Snap(17.8f, 5f);  // 20f

Equality Comparisons

AlmostZero

Determines whether a floating-point value is approximately zero.

bool isZero = MathHelper.AlmostZero(0.0005f, 0.001f);  // true
bool isZero2 = MathHelper.AlmostZero(0.002f, 0.001f);  // false

AlmostEquals

Determines whether two floating-point values are approximately equal.

bool equal = MathHelper.AlmostEquals(0.999f, 1.000f, 0.001f);  // true
bool equal2 = MathHelper.AlmostEquals(1.002f, 1.000f, 0.001f);  // false

Summary

Category Methods
Constants PI, TwoPI, HalfPI, QuarterPI, InvPI, DegToRad, RadToDeg, Epsilon
Angle Conversion ToRadians, ToDegrees
Interpolation Lerp, SmoothStep, InverseLerp, Remap, MoveTowards
Clamping Clamp, Saturate, Center
Wrapping Wrap, PingPong
Direction DirectionToAngle, AngleToDirection
Rounding RoundToInt, FloorToInt, CeilToInt, Snap
Equality AlmostZero, AlmostEquals

Back to Home

Clone this wiki locally