public static abstract sealed class Sandbox.MathX
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();`
Related Guides
Broader workflow and conceptual references connected to this API.
Methods
Showing 38 methods
public static bool AlmostEqual(float value, float b, float within = 0.0001)
Returns true if given value is close to given value within given tolerance.
Parameters
within: Default: 0.0001
public static float Approach(float f, float target, float delta)
Adds or subtracts given amount based on whether the input is smaller of bigger than the target.
public static int CeilToInt(float f)
Rounds up given float to next integer value.
public static float Clamp(float v, float min, float max)
Clamp a float between 2 given extremes. If given value is lower than the given minimum value, returns the minimum value, etc.
Parameters
v: The value to clamp.min: Minimum return value.max: Maximum return value.
Returns
The clamped float.
public static float DegreeToRadian(float deg)
Convert degrees to radians. 180 degrees is `System.Math.PI` (roughly 3.14) radians, etc.
Parameters
deg: A value in degrees to convert.
Returns
The given value converted to radians.
public static float DeltaDegrees(float from, float to)
Difference between two angles in degrees. Will always be between -180 and +180.
public static float DeltaRadians(float from, float to)
Difference between two angles in radians. Will always be between -PI and +PI.
public static float ExponentialDecay(float current, float target, float halflife, float deltaTime)
Smoothly approach the target value using exponential decay. Cheaper than SmoothDamp but doesn't track velocity for momentum. Good for non-physical smoothing.
Parameters
current: Current valuetarget: Target value to approachhalflife: Time for the difference to reduce by 50%deltaTime: Time step
public static float Floor(float f)
Remove the fractional part of given floating point number
public static int FloorToInt(float f)
Remove the fractional part and return the float as an integer.
public static float GradiansToDegrees(float grad)
Convert gradians to degrees. 100 gradian is 90 degrees, 200 gradian is 180 degrees, etc.
Parameters
grad: A value in gradians to convert.
Returns
The given value converted to degrees.
public static float GradiansToRadians(float grad)
Convert gradians to radians. 200 gradian is `System.Math.PI` (roughly 3.14) radians, etc.
Parameters
grad: A value in gradians to convert.
Returns
The given value converted to radians.
public static float InchToMeter(float inches)
Convert inches to meters.
public static float InchToMillimeter(float inches)
Convert inches to millimeters.
public static float LerpDegrees(float from, float to, float frac, bool clamp = True)
Linearly interpolates between two angles in degrees, taking the shortest arc.
Parameters
clamp: Default: True
public static float LerpDegreesTo(float from, float to, float frac, bool clamp = True)
Parameters
clamp: Default: True
public static float LerpInverse(float value, float from, float to, bool clamp = True)
Performs inverse of a linear interpolation, that is, the return value is the fraction of a linear interpolation.
Parameters
value: The value relative to `from` and `to`.from: The "starting value" of the interpolation. If `value` is at this value or less, the function will return 0 or less.to: The "final value" of the interpolation. If `value` is at this value or greater, the function will return 1 or greater.clamp: Whether the return value is allowed to exceed range of 0 - 1. Default: True
Returns
The resulting fraction.
public static float LerpRadians(float from, float to, float frac, bool clamp = True)
Linearly interpolates between two angles in radians, taking the shortest arc.
Parameters
clamp: Default: True
public static float LerpRadiansTo(float from, float to, float frac, bool clamp = True)
Parameters
clamp: Default: True
public static float MeterToInch(float meters)
Convert meters to inches.
public static float MillimeterToInch(float millimeters)
Convert millimeters to inches.
public static float NormalizeDegrees(float degree)
Convert angle to between 0 - 360
public static float RadianToDegree(float rad)
Convert radians to degrees. 180 degrees is `System.Math.PI` (roughly 3.14) radians, etc.
Parameters
rad: A value in radians to convert.
Returns
The given value converted to degrees.
public static float SmoothDamp(float current, float target, float velocity, float smoothTime, float deltaTime)
Smoothly move towards the target
public static float SphereCameraDistance(float radius, float fieldOfView)
Given a sphere and a field of view, how far from the camera should we be to fully see the sphere?
Parameters
radius: The radius of the spherefieldOfView: The field of view in degrees
Returns
The 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)
Smoothly move towards the target using a spring-like motion
Parameters
frequency: Default: 2damping: Default: 0.5
public static float UnsignedMod(float a, float b)
Does what you expected to happen when you did "a % b"
No results match this filter.
Metadata
| Field | Value |
|---|---|
| Namespace | Sandbox |
| Type | class |
| Assembly | Sandbox.System |
| Doc ID | T:Sandbox.MathX |