MathXclass

A class to add functionality to the math library that System.Math and System.MathF don't provide. A lot of these methods are also extensions, so you can use for example `int i = 1.0f.FloorToInt();`

objectMathX
Namespace
Sandbox
Assembly
Sandbox.System
Declaration
public static abstract sealed class Sandbox.MathX

Methods38

Showing 38 methods

public static bool AlmostEqual(float value, float b, float within = 0.0001)PUBLICSTATIC

Returns true if given value is close to given value within given tolerance.

ParameterTypeDescription
valuefloat
bfloat
within = 0.0001float
Returns:bool

public static float Approach(float f, float target, float delta)PUBLICSTATIC

Adds or subtracts given amount based on whether the input is smaller of bigger than the target.

ParameterTypeDescription
ffloat
targetfloat
deltafloat
Returns:float

public static int CeilToInt(float f)PUBLICSTATIC

Rounds up given float to next integer value.

ParameterTypeDescription
ffloat
Returns:int

public static float Clamp(float v, float min, float max)PUBLICSTATIC

Clamp a float between 2 given extremes. If given value is lower than the given minimum value, returns the minimum value, etc.

ParameterTypeDescription
vfloatThe value to clamp.
minfloatMinimum return value.
maxfloatMaximum return value.
Returns:floatThe clamped float.

public static float DegreeToRadian(float deg)PUBLICSTATIC

Convert degrees to radians. 180 degrees is `System.Math.PI` (roughly 3.14) radians, etc.

ParameterTypeDescription
degfloatA value in degrees to convert.
Returns:floatThe given value converted to radians.

public static float DeltaDegrees(float from, float to)PUBLICSTATIC

Difference between two angles in degrees. Will always be between -180 and +180.

ParameterTypeDescription
fromfloat
tofloat
Returns:float

public static float DeltaRadians(float from, float to)PUBLICSTATIC

Difference between two angles in radians. Will always be between -PI and +PI.

ParameterTypeDescription
fromfloat
tofloat
Returns:float

public static float ExponentialDecay(float current, float target, float halflife, float deltaTime)PUBLICSTATIC

Smoothly approach the target value using exponential decay. Cheaper than SmoothDamp but doesn't track velocity for momentum. Good for non-physical smoothing.

ParameterTypeDescription
currentfloatCurrent value
targetfloatTarget value to approach
halflifefloatTime for the difference to reduce by 50%
deltaTimefloatTime step
Returns:float

public static float Floor(float f)PUBLICSTATIC

Remove the fractional part of given floating point number

ParameterTypeDescription
ffloat
Returns:float

public static int FloorToInt(float f)PUBLICSTATIC

Remove the fractional part and return the float as an integer.

ParameterTypeDescription
ffloat
Returns:int

public static float GradiansToDegrees(float grad)PUBLICSTATIC

Convert gradians to degrees. 100 gradian is 90 degrees, 200 gradian is 180 degrees, etc.

ParameterTypeDescription
gradfloatA value in gradians to convert.
Returns:floatThe given value converted to degrees.

public static float GradiansToRadians(float grad)PUBLICSTATIC

Convert gradians to radians. 200 gradian is `System.Math.PI` (roughly 3.14) radians, etc.

ParameterTypeDescription
gradfloatA value in gradians to convert.
Returns:floatThe given value converted to radians.

public static float InchToMeter(float inches)PUBLICSTATIC

Convert inches to meters.

ParameterTypeDescription
inchesfloat
Returns:float

public static float InchToMillimeter(float inches)PUBLICSTATIC

Convert inches to millimeters.

ParameterTypeDescription
inchesfloat
Returns:float

public static float LerpDegrees(float from, float to, float frac, bool clamp = True)PUBLICSTATIC

Linearly interpolates between two angles in degrees, taking the shortest arc.

ParameterTypeDescription
fromfloat
tofloat
fracfloat
clamp = Truebool
Returns:float

public static float LerpDegreesTo(float from, float to, float frac, bool clamp = True)PUBLICSTATIC

ParameterTypeDescription
fromfloat
tofloat
fracfloat
clamp = Truebool
Returns:float

public static float LerpInverse(float value, float from, float to, bool clamp = True)PUBLICSTATIC

Performs inverse of a linear interpolation, that is, the return value is the fraction of a linear interpolation.

ParameterTypeDescription
valuefloatThe value relative to `from` and `to`.
fromfloatThe "starting value" of the interpolation. If `value` is at this value or less, the function will return 0 or less.
tofloatThe "final value" of the interpolation. If `value` is at this value or greater, the function will return 1 or greater.
clamp = TrueboolWhether the return value is allowed to exceed range of 0 - 1.
Returns:floatThe resulting fraction.

public static float LerpRadians(float from, float to, float frac, bool clamp = True)PUBLICSTATIC

Linearly interpolates between two angles in radians, taking the shortest arc.

ParameterTypeDescription
fromfloat
tofloat
fracfloat
clamp = Truebool
Returns:float

public static float LerpRadiansTo(float from, float to, float frac, bool clamp = True)PUBLICSTATIC

ParameterTypeDescription
fromfloat
tofloat
fracfloat
clamp = Truebool
Returns:float

public static float MeterToInch(float meters)PUBLICSTATIC

Convert meters to inches.

ParameterTypeDescription
metersfloat
Returns:float

public static float MillimeterToInch(float millimeters)PUBLICSTATIC

Convert millimeters to inches.

ParameterTypeDescription
millimetersfloat
Returns:float

public static float NormalizeDegrees(float degree)PUBLICSTATIC

Convert angle to between 0 - 360

ParameterTypeDescription
degreefloat
Returns:float

public static float RadianToDegree(float rad)PUBLICSTATIC

Convert radians to degrees. 180 degrees is `System.Math.PI` (roughly 3.14) radians, etc.

ParameterTypeDescription
radfloatA value in radians to convert.
Returns:floatThe given value converted to degrees.

public static float SmoothDamp(float current, float target, float velocity, float smoothTime, float deltaTime)PUBLICSTATIC

Smoothly move towards the target

ParameterTypeDescription
currentfloat
targetfloat
velocityfloat
smoothTimefloat
deltaTimefloat
Returns:float

public static float SphereCameraDistance(float radius, float fieldOfView)PUBLICSTATIC

Given a sphere and a field of view, how far from the camera should we be to fully see the sphere?

ParameterTypeDescription
radiusfloatThe radius of the sphere
fieldOfViewfloatThe field of view in degrees
Returns:floatThe optimal distance from the center of the sphere

public static float SpringDamp(float current, float target, float velocity, float deltaTime, float frequency = 2, float damping = 0.5)PUBLICSTATIC

Smoothly move towards the target using a spring-like motion

ParameterTypeDescription
currentfloat
targetfloat
velocityfloat
deltaTimefloat
frequency = 2float
damping = 0.5float
Returns:float

public static float UnsignedMod(float a, float b)PUBLICSTATIC

Does what you expected to happen when you did "a % b"

ParameterTypeDescription
afloat
bfloat
Returns:float

On this page

Methodspublic static System.Boolean AlmostEqual(System.Single value, System.Single b, System.Single within = 0.0001)public static System.Single Approach(System.Single f, System.Single target, System.Single delta)public static System.Int32 CeilToInt(System.Single f)public static System.Single Clamp(System.Single v, System.Single min, System.Single max)public static System.Single DegreeToRadian(System.Single deg)public static System.Single DeltaDegrees(System.Single from, System.Single to)public static System.Single DeltaRadians(System.Single from, System.Single to)public static System.Single ExponentialDecay(System.Single current, System.Single target, System.Single halflife, System.Single deltaTime)public static System.Single Floor(System.Single f)public static System.Int32 FloorToInt(System.Single f)public static System.Single GradiansToDegrees(System.Single grad)public static System.Single GradiansToRadians(System.Single grad)public static System.Single InchToMeter(System.Single inches)public static System.Single InchToMillimeter(System.Single inches)public static System.Double Lerp(System.Double from, System.Double to, System.Double frac, System.Boolean clamp = True)public static System.Single Lerp(System.Single from, System.Single to, System.Single frac, System.Boolean clamp = True)public static System.Single LerpDegrees(System.Single from, System.Single to, System.Single frac, System.Boolean clamp = True)public static System.Single LerpDegreesTo(System.Single from, System.Single to, System.Single frac, System.Boolean clamp = True)public static System.Single LerpInverse(System.Single value, System.Single from, System.Single to, System.Boolean clamp = True)public static System.Single LerpRadians(System.Single from, System.Single to, System.Single frac, System.Boolean clamp = True)public static System.Single LerpRadiansTo(System.Single from, System.Single to, System.Single frac, System.Boolean clamp = True)public static System.Single LerpTo(System.Single from, System.Single to, System.Single frac, System.Boolean clamp = True)public static System.Single[] LerpTo(System.Single[] from, System.Single[] to, System.Single delta, System.Boolean clamp = True)public static System.Single MeterToInch(System.Single meters)public static System.Single MillimeterToInch(System.Single millimeters)public static System.Single NormalizeDegrees(System.Single degree)public static System.Single RadianToDegree(System.Single rad)public static System.Double Remap(System.Double value, System.Double oldLow, System.Double oldHigh, System.Double newLow, System.Double newHigh, System.Boolean clamp)public static System.Double Remap(System.Double value, System.Double oldLow, System.Double oldHigh, System.Double newLow = 0, System.Double newHigh = 1)public static System.Int32 Remap(System.Int32 value, System.Int32 oldLow, System.Int32 oldHigh, System.Int32 newLow, System.Int32 newHigh)public static System.Single Remap(System.Single value, System.Single oldLow, System.Single oldHigh, System.Single newLow, System.Single newHigh, System.Boolean clamp)public static System.Single Remap(System.Single value, System.Single oldLow, System.Single oldHigh, System.Single newLow = 0, System.Single newHigh = 1)public static System.Single SmoothDamp(System.Single current, System.Single target, System.Single velocity, System.Single smoothTime, System.Single deltaTime)public static System.Int32 SnapToGrid(System.Int32 f, System.Int32 gridSize)public static System.Single SnapToGrid(System.Single f, System.Single gridSize)public static System.Single SphereCameraDistance(System.Single radius, System.Single fieldOfView)public static System.Single SpringDamp(System.Single current, System.Single target, System.Single velocity, System.Single deltaTime, System.Single frequency = 2, System.Single damping = 0.5)public static System.Single UnsignedMod(System.Single a, System.Single b)Metadata