s&box docs

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();`

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 value
  • target: Target value to approach
  • halflife: 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 sphere
  • fieldOfView: 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: 2
  • damping: Default: 0.5

public static float UnsignedMod(float a, float b)

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

Metadata

FieldValue
NamespaceSandbox
Typeclass
AssemblySandbox.System
Doc IDT:Sandbox.MathX

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