Class MathGm
- Namespace
- Gamem
- Assembly
- Gamem.dll
Provides general-purpose static mathematical functions including interpolation and random number generation.
public static class MathGm
- Inheritance
-
MathGm
- Inherited Members
Methods
Accelerate(double, double, double, double)
Smoothly accelerates current velocity toward a target velocity using an acceleration rate over a given time step.
public static double Accelerate(double Vcurrent, double Vtarget, double a, double t)
Parameters
VcurrentdoubleThe current velocity.
VtargetdoubleThe desired target velocity.
adoubleThe acceleration rate multiplier.
tdoubleThe time elapsed since the last frame in seconds.
Returns
- double
The updated velocity approaching the target value.
Accelerate(float, float, float, float)
Smoothly accelerates current velocity toward a target velocity using an acceleration rate over a given time step.
public static float Accelerate(float Vcurrent, float Vtarget, float a, float t)
Parameters
VcurrentfloatThe current velocity.
VtargetfloatThe desired target velocity.
afloatThe acceleration rate multiplier.
tfloatThe time elapsed since the last frame in seconds.
Returns
- float
The updated velocity approaching the target value.
Approximately(double, double)
Compares two floating-point values and determines if they are approximately equal within a small tolerance.
public static bool Approximately(double a, double b)
Parameters
Returns
Approximately(float, float)
Compares two floating-point values and determines if they are approximately equal within a small tolerance.
public static bool Approximately(float a, float b)
Parameters
Returns
EaseOutElastic(double)
Calculates an Ease-Out Elastic easing value, producing an oscillating decay effect that settles at 1.0.
public static double EaseOutElastic(double t)
Parameters
tdoubleThe normalized progress time, typically clamped in the range [0.0, 1.0].
Returns
- double
The interpolated value using the Ease-Out Elastic function, clamped to 0.0 at
t≤ 0 and 1.0 att≥ 1.
EaseOutElastic(float)
Calculates an Ease-Out Elastic easing value, producing an oscillating decay effect that settles at 1.0.
public static float EaseOutElastic(float t)
Parameters
tfloatThe normalized progress time, typically clamped in the range [0.0, 1.0].
Returns
- float
The interpolated value using the Ease-Out Elastic function, clamped to 0.0 at
t≤ 0 and 1.0 att≥ 1.
InverseLerp(double, double, double)
Calculates the linear parameter t that produces the given value within a specific range, clamped between 0.0 and 1.0.
public static double InverseLerp(double value, double start, double end)
Parameters
valuedoubleThe value to find the interpolation factor for.
startdoubleThe start value of the range.
enddoubleThe end value of the range.
Returns
- double
The normalized linear parameter t between 0.0 and 1.0, or 0.0 if the range is zero.
InverseLerp(float, float, float)
Calculates the linear parameter t that produces the given value within a specific range, clamped between 0.0 and 1.0.
public static float InverseLerp(float value, float start, float end)
Parameters
valuefloatThe value to find the interpolation factor for.
startfloatThe start value of the range.
endfloatThe end value of the range.
Returns
- float
The normalized linear parameter t between 0.0 and 1.0, or 0.0 if the range is zero.
Lerp(double, double, double)
Linearly interpolates between start and end values, clamping the interpolation percentage between 0.0 and 1.0.
public static double Lerp(double start, double end, double t)
Parameters
startdoubleThe start value.
enddoubleThe end value.
tdoubleThe interpolation value, clamped between 0.0 and 1.0.
Returns
- double
The interpolated value between start and end.
Lerp(float, float, float)
Linearly interpolates between start and end values, clamping the interpolation percentage between 0.0 and 1.0.
public static float Lerp(float start, float end, float t)
Parameters
startfloatThe start value.
endfloatThe end value.
tfloatThe interpolation value, clamped between 0.0 and 1.0.
Returns
- float
The interpolated value between start and end.
LerpAngle(double, double, double)
Linearly interpolates between two angles in degrees, properly handling wrapping around 360 degrees.
public static double LerpAngle(double start, double end, double t)
Parameters
startdoubleThe starting angle in degrees.
enddoubleThe target angle in degrees.
tdoubleThe interpolation factor, which will be clamped between 0.0 and 1.0.
Returns
- double
The interpolated angle in degrees, adjusted to take the shortest path around the circle.
LerpAngle(float, float, float)
Linearly interpolates between two angles in degrees, properly handling wrapping around 360 degrees.
public static float LerpAngle(float start, float end, float t)
Parameters
startfloatThe starting angle in degrees.
endfloatThe target angle in degrees.
tfloatThe interpolation factor, which will be clamped between 0.0 and 1.0.
Returns
- float
The interpolated angle in degrees, adjusted to take the shortest path around the circle.
LerpUnclamped(double, double, double)
Linearly interpolates between start and end values without clamping the interpolation percentage.
public static double LerpUnclamped(double start, double end, double t)
Parameters
startdoubleThe start value.
enddoubleThe end value.
tdoubleThe interpolation value, allowing extrapolation outside the start and end range.
Returns
- double
The interpolated or extrapolated value.
LerpUnclamped(float, float, float)
Linearly interpolates between start and end values without clamping the interpolation percentage.
public static float LerpUnclamped(float start, float end, float t)
Parameters
startfloatThe start value.
endfloatThe end value.
tfloatThe interpolation value, allowing extrapolation outside the start and end range.
Returns
- float
The interpolated or extrapolated value.
Map(double, double, double, double, double)
Maps a value from an input range to an output range without clamping.
public static double Map(double toMin, double v, double fromMin, double toMax, double fromMax)
Parameters
toMindoubleThe lower bound of the output range.
vdoubleThe value to be mapped.
fromMindoubleThe lower bound of the input range.
toMaxdoubleThe upper bound of the output range.
fromMaxdoubleThe upper bound of the input range.
Returns
- double
The mapped value in the output range, or 0.0 if the input range size is zero.
Map(float, float, float, float, float)
Maps a value from an input range to an output range without clamping.
public static float Map(float toMin, float v, float fromMin, float toMax, float fromMax)
Parameters
toMinfloatThe lower bound of the output range.
vfloatThe value to be mapped.
fromMinfloatThe lower bound of the input range.
toMaxfloatThe upper bound of the output range.
fromMaxfloatThe upper bound of the input range.
Returns
- float
The mapped value in the output range, or 0.0 if the input range size is zero.
MoveTowards(double, double, double, double)
Moves a value toward a target value at a specified speed over a given time step.
public static double MoveTowards(double current, double target, double speed, double dt)
Parameters
currentdoubleThe current value.
targetdoubleThe target value to move towards.
speeddoubleThe rate of movement per second.
dtdoubleThe time elapsed since the last frame in seconds.
Returns
- double
The updated value closer to the target, or the target itself if it is within reaching distance.
MoveTowards(float, float, float, float)
Moves a value toward a target value at a specified speed over a given time step.
public static float MoveTowards(float current, float target, float speed, float dt)
Parameters
currentfloatThe current value.
targetfloatThe target value to move towards.
speedfloatThe rate of movement per second.
dtfloatThe time elapsed since the last frame in seconds.
Returns
- float
The updated value closer to the target, or the target itself if it is within reaching distance.
PingPong(double, double)
Ping-pongs the value t, causing it to bounce back and forth between 0 and length.
public static double PingPong(double t, double length)
Parameters
tdoubleThe incoming value (typically an accumulating time variable).
lengthdoubleThe maximum value the result can reach at its peak before bouncing back.
Returns
- double
A value between 0 and
lengththat oscillates continuously back and forth.
PingPong(float, float)
Ping-pongs the value t, causing it to bounce back and forth between 0 and length.
public static float PingPong(float t, float length)
Parameters
tfloatThe incoming value (typically an accumulating time variable).
lengthfloatThe maximum value the result can reach at its peak before bouncing back.
Returns
- float
A value between 0 and
lengththat oscillates continuously back and forth.
RandomRange(double, double)
Generates a random floating-point value within a specified inclusive range.
[Obsolete("Use RandomGm.RandomRange() instead")]
public static double RandomRange(double min, double max)
Parameters
Returns
- double
A random value greater than or equal to min, and less than max.
RandomRange(float, float)
Generates a random floating-point value within a specified inclusive range.
[Obsolete("Use RandomGm.RandomRange() instead")]
public static float RandomRange(float min, float max)
Parameters
Returns
- float
A random value greater than or equal to min, and less than max.
Remap(double, double, double, double, double)
Remaps a value from an input range to an output range, behaving identically to Map method.
public static double Remap(double toMin, double v, double fromMin, double toMax, double fromMax)
Parameters
toMindoubleThe lower bound of the output range.
vdoubleThe value to be remapped.
fromMindoubleThe lower bound of the input range.
toMaxdoubleThe upper bound of the output range.
fromMaxdoubleThe upper bound of the input range.
Returns
- double
The remapped value in the output range.
Remap(float, float, float, float, float)
Remaps a value from an input range to an output range, behaving identically to Map method.
public static float Remap(float toMin, float v, float fromMin, float toMax, float fromMax)
Parameters
toMinfloatThe lower bound of the output range.
vfloatThe value to be remapped.
fromMinfloatThe lower bound of the input range.
toMaxfloatThe upper bound of the output range.
fromMaxfloatThe upper bound of the input range.
Returns
- float
The remapped value in the output range.
Repeat(double, double)
Loops the value t so that it is never larger than length and never smaller than 0.
public static double Repeat(double t, double length)
Parameters
Returns
- double
The looped value wrapped within the range [0,
length).
Repeat(float, float)
Loops the value t so that it is never larger than length and never smaller than 0.
public static float Repeat(float t, float length)
Parameters
Returns
- float
The looped value wrapped within the range [0,
length).
RollChance(double)
Evaluates a percentage-based chance to determine a success outcome using a double-precision floating-point value.
[Obsolete("Use RandomGm.RollChance() instead")]
public static bool RollChance(double chance)
Parameters
chancedoubleThe success probability as a percentage (e.g., 75.5 for a 75.5% chance).
Returns
RollChance(int)
Evaluates a percentage-based chance to determine a success outcome using an integer value.
[Obsolete("Use RandomGm.RollChance() instead")]
public static bool RollChance(int chance)
Parameters
chanceintThe success probability as an integer percentage (e.g., 50 for a 50% chance).
Returns
RollChance(float)
Evaluates a percentage-based chance to determine a success outcome using a single-precision floating-point value.
[Obsolete("Use RandomGm.RollChance() instead")]
public static bool RollChance(float chance)
Parameters
chancefloatThe success probability as a percentage (e.g., 12.3f for a 12.3% chance).
Returns
SafeDivide(double, double, double)
Divides one double-precision floating-point number by another, returning a fallback value if the denominator is zero.
public static double SafeDivide(double a, double b, double fallback = 0)
Parameters
adoubleThe dividend (numerator).
bdoubleThe divisor (denominator).
fallbackdoubleThe value to return if
bis zero.
Returns
- double
The result of
a/b, or the fallback value ifbis 0.0.
SafeDivide(int, int, int)
Divides one integer by another, returning a fallback value if the denominator is zero to prevent a division-by-zero exception.
public static int SafeDivide(int a, int b, int fallback = 0)
Parameters
aintThe dividend (numerator).
bintThe divisor (denominator).
fallbackintThe value to return if
bis zero.
Returns
- int
The result of
a/b, or the fallback value ifbis 0.
SafeDivide(float, float, float)
Divides one single-precision floating-point number by another, returning a fallback value if the denominator is zero.
public static float SafeDivide(float a, float b, float fallback = 0)
Parameters
afloatThe dividend (numerator).
bfloatThe divisor (denominator).
fallbackfloatThe value to return if
bis zero.
Returns
- float
The result of
a/b, or the fallback value ifbis 0.0f.
Slerp(double, double, double, double, double, double, double)
Performs spherical linear interpolation (Slerp) between two 3D unit vectors represented by individual components.
public static (double, double, double) Slerp(double v1X, double v1Y, double v1Z, double v2X, double v2Y, double v2Z, double t)
Parameters
v1XdoubleThe X component of the start unit vector.
v1YdoubleThe Y component of the start unit vector.
v1ZdoubleThe Z component of the start unit vector.
v2XdoubleThe X component of the target unit vector.
v2YdoubleThe Y component of the target unit vector.
v2ZdoubleThe Z component of the target unit vector.
tdoubleThe interpolation parameter, where 0.0 returns the start vector and 1.0 returns the target vector.
Returns
Slerp(Vector3, Vector3, float)
Performs spherical linear interpolation (Slerp) between two 3D unit vectors.
public static Vector3 Slerp(Vector3 v1, Vector3 v2, float t)
Parameters
v1Vector3The start unit vector.
v2Vector3The target unit vector.
tfloatThe interpolation parameter, where 0.0f returns the start vector and 1.0f returns the target vector.
Returns
Slerp(float, float, float, float, float, float, float)
Performs spherical linear interpolation (Slerp) between two 3D unit vectors represented by individual components.
public static (float, float, float) Slerp(float v1X, float v1Y, float v1Z, float v2X, float v2Y, float v2Z, float t)
Parameters
v1XfloatThe X component of the start unit vector.
v1YfloatThe Y component of the start unit vector.
v1ZfloatThe Z component of the start unit vector.
v2XfloatThe X component of the target unit vector.
v2YfloatThe Y component of the target unit vector.
v2ZfloatThe Z component of the target unit vector.
tfloatThe interpolation parameter, where 0.0 returns the start vector and 1.0 returns the target vector.
Returns
SmoothDamp(double, double, ref double, double, double, double)
Smoothly damps a value toward a target destination over time using a critically damped spring-like function.
public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime)
Parameters
currentdoubleThe current position or value.
targetdoubleThe target position or value to reach.
currentVelocitydoubleA reference to the tracking velocity, which is updated internally by the function.
smoothTimedoubleThe approximate time it will take to reach the target. Shorter values reach the target faster.
maxSpeeddoubleThe maximum speed allowed during the movement transition.
deltaTimedoubleThe time elapsed since the last frame in seconds.
Returns
- double
The newly smoothed value approaching the target.
SmoothDamp(float, float, ref float, float, float, float)
Smoothly damps a value toward a target destination over time using a critically damped spring-like function.
public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
Parameters
currentfloatThe current position or value.
targetfloatThe target position or value to reach.
currentVelocityfloatA reference to the tracking velocity, which is updated internally by the function.
smoothTimefloatThe approximate time it will take to reach the target. Shorter values reach the target faster.
maxSpeedfloatThe maximum speed allowed during the movement transition.
deltaTimefloatThe time elapsed since the last frame in seconds.
Returns
- float
The newly smoothed value approaching the target.
SmoothDampAngle(ref double, double, ref double, double, double)
Smoothly damps an angle toward a target angle over time in degrees, handling wrapping around 360 degrees.
public static double SmoothDampAngle(ref double current, double target, ref double currentVelocity, double smoothTime, double deltaTime)
Parameters
currentdoubleA reference to the current angle in degrees, which is updated internally by the function.
targetdoubleThe target angle to reach in degrees.
currentVelocitydoubleA reference to the tracking angular velocity, which is updated internally by the function.
smoothTimedoubleThe approximate time it will take to reach the target. Shorter values reach the target faster.
deltaTimedoubleThe time elapsed since the last frame in seconds.
Returns
- double
The newly smoothed angle in degrees, clamped between 0 and 360.
SmoothDampAngle(ref float, float, ref float, float, float)
Smoothly damps an angle toward a target angle over time in degrees, handling wrapping around 360 degrees.
public static float SmoothDampAngle(ref float current, float target, ref float currentVelocity, float smoothTime, float deltaTime)
Parameters
currentfloatA reference to the current angle in degrees, which is updated internally by the function.
targetfloatThe target angle to reach in degrees.
currentVelocityfloatA reference to the tracking angular velocity, which is updated internally by the function.
smoothTimefloatThe approximate time it will take to reach the target. Shorter values reach the target faster.
deltaTimefloatThe time elapsed since the last frame in seconds.
Returns
- float
The newly smoothed angle in degrees, clamped between 0 and 360.
SmoothStep(double, double, double)
Performs a smooth cubic interpolation between two values based on a given percentage.
public static double SmoothStep(double start, double end, double t)
Parameters
startdoubleThe start value.
enddoubleThe end value.
tdoubleThe interpolation value, clamped between 0.0 and 1.0.
Returns
- double
The smoothly interpolated value between start and end.
SmoothStep(float, float, float)
Performs a smooth cubic interpolation between two values based on a given percentage.
public static float SmoothStep(float start, float end, float t)
Parameters
startfloatThe start value.
endfloatThe end value.
tfloatThe interpolation value, clamped between 0.0 and 1.0.
Returns
- float
The smoothly interpolated value between start and end.