-
Notifications
You must be signed in to change notification settings - Fork 1
MathHelper
MathHelper provides a comprehensive collection of mathematical utility functions for interpolation, clamping, wrapping, conversion, and common game math operations.
| 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 |
Converts degrees to radians.
float radians = MathHelper.ToRadians(90f); // 1.5708 (PI/2)Converts radians to degrees.
float degrees = MathHelper.ToDegrees(MathHelper.PI); // 180Linearly interpolates between two values.
float value = MathHelper.Lerp(0f, 10f, 0.5f); // 5fPerforms smooth Hermite interpolation between two values.
float value = MathHelper.SmoothStep(0f, 10f, 0.5f); // 5f (with smooth easing)Calculates the inverse interpolation factor between two values.
float t = MathHelper.InverseLerp(0f, 10f, 5f); // 0.5fRemaps a value from one range to another.
float value = MathHelper.Remap(5f, 0f, 10f, 0f, 100f); // 50fMoves a value towards a target by a maximum delta.
float value = MathHelper.MoveTowards(5f, 10f, 2f); // 7fClamps a value between a minimum and maximum.
float value = MathHelper.Clamp(15f, 0f, 10f); // 10fClamps a value between 0 and 1.
float value = MathHelper.Saturate(1.5f); // 1fCalculates the center offset between two values.
float center = MathHelper.Center(100f, 50f, false); // 25f
float centerClamped = MathHelper.Center(100f, 50f, true); // 25f (rounded)Wraps a floating-point value within a specified range.
float value = MathHelper.Wrap(5f, 0f, 3f); // 2f
float value2 = MathHelper.Wrap(-1f, 0f, 3f); // 2fWraps an integer value within a specified range.
int value = MathHelper.Wrap(5, 0, 3); // 2
int value2 = MathHelper.Wrap(-1, 0, 3); // 2Ping-pongs a value between 0 and a specified length.
float value = MathHelper.PingPong(5f, 3f); // 1f (bounces between 0 and 3)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/2Converts an angle in radians to a direction vector.
Vect2 direction = MathHelper.AngleToDirection(0f); // (1, 0)
Vect2 direction2 = MathHelper.AngleToDirection(MathHelper.HalfPI); // (0, 1)Rounds a floating-point value to the nearest integer.
int value = MathHelper.RoundToInt(3.7f); // 4Floors a floating-point value to the nearest integer.
int value = MathHelper.FloorToInt(3.7f); // 3Ceils a floating-point value to the nearest integer.
int value = MathHelper.CeilToInt(3.2f); // 4Snaps 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); // 20fDetermines 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); // falseDetermines 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| 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 |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions