Table of Contents

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

Vcurrent double

The current velocity.

Vtarget double

The desired target velocity.

a double

The acceleration rate multiplier.

t double

The 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

Vcurrent float

The current velocity.

Vtarget float

The desired target velocity.

a float

The acceleration rate multiplier.

t float

The 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

a double

The first value to compare.

b double

The second value to compare.

Returns

bool

true if the values are approximately equal; otherwise, false.

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

a float

The first value to compare.

b float

The second value to compare.

Returns

bool

true if the values are approximately equal; otherwise, false.

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

t double

The 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 at t ≥ 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

t float

The 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 at t ≥ 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

value double

The value to find the interpolation factor for.

start double

The start value of the range.

end double

The 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

value float

The value to find the interpolation factor for.

start float

The start value of the range.

end float

The 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

start double

The start value.

end double

The end value.

t double

The 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

start float

The start value.

end float

The end value.

t float

The 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

start double

The starting angle in degrees.

end double

The target angle in degrees.

t double

The 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

start float

The starting angle in degrees.

end float

The target angle in degrees.

t float

The 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

start double

The start value.

end double

The end value.

t double

The 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

start float

The start value.

end float

The end value.

t float

The 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

toMin double

The lower bound of the output range.

v double

The value to be mapped.

fromMin double

The lower bound of the input range.

toMax double

The upper bound of the output range.

fromMax double

The 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

toMin float

The lower bound of the output range.

v float

The value to be mapped.

fromMin float

The lower bound of the input range.

toMax float

The upper bound of the output range.

fromMax float

The 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

current double

The current value.

target double

The target value to move towards.

speed double

The rate of movement per second.

dt double

The 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

current float

The current value.

target float

The target value to move towards.

speed float

The rate of movement per second.

dt float

The 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

t double

The incoming value (typically an accumulating time variable).

length double

The maximum value the result can reach at its peak before bouncing back.

Returns

double

A value between 0 and length that 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

t float

The incoming value (typically an accumulating time variable).

length float

The maximum value the result can reach at its peak before bouncing back.

Returns

float

A value between 0 and length that 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

min double

The minimum bound of the range.

max double

The maximum bound of the range.

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

min float

The minimum bound of the range.

max float

The maximum bound of the range.

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

toMin double

The lower bound of the output range.

v double

The value to be remapped.

fromMin double

The lower bound of the input range.

toMax double

The upper bound of the output range.

fromMax double

The 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

toMin float

The lower bound of the output range.

v float

The value to be remapped.

fromMin float

The lower bound of the input range.

toMax float

The upper bound of the output range.

fromMax float

The 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

t double

The input value to loop.

length double

The length of the loop (period).

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

t float

The input value to loop.

length float

The length of the loop (period).

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

chance double

The success probability as a percentage (e.g., 75.5 for a 75.5% chance).

Returns

bool

true if the random roll succeeds; otherwise, false.

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

chance int

The success probability as an integer percentage (e.g., 50 for a 50% chance).

Returns

bool

true if the random roll succeeds; otherwise, false.

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

chance float

The success probability as a percentage (e.g., 12.3f for a 12.3% chance).

Returns

bool

true if the random roll succeeds; otherwise, false.

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

a double

The dividend (numerator).

b double

The divisor (denominator).

fallback double

The value to return if b is zero.

Returns

double

The result of a / b, or the fallback value if b is 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

a int

The dividend (numerator).

b int

The divisor (denominator).

fallback int

The value to return if b is zero.

Returns

int

The result of a / b, or the fallback value if b is 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

a float

The dividend (numerator).

b float

The divisor (denominator).

fallback float

The value to return if b is zero.

Returns

float

The result of a / b, or the fallback value if b is 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

v1X double

The X component of the start unit vector.

v1Y double

The Y component of the start unit vector.

v1Z double

The Z component of the start unit vector.

v2X double

The X component of the target unit vector.

v2Y double

The Y component of the target unit vector.

v2Z double

The Z component of the target unit vector.

t double

The interpolation parameter, where 0.0 returns the start vector and 1.0 returns the target vector.

Returns

(double x, double y, double z)

A tuple containing the interpolated vector's X, Y, and Z components.

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

v1 Vector3

The start unit vector.

v2 Vector3

The target unit vector.

t float

The interpolation parameter, where 0.0f returns the start vector and 1.0f returns the target vector.

Returns

Vector3

A Vector3 representing the spherically interpolated vector.

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

v1X float

The X component of the start unit vector.

v1Y float

The Y component of the start unit vector.

v1Z float

The Z component of the start unit vector.

v2X float

The X component of the target unit vector.

v2Y float

The Y component of the target unit vector.

v2Z float

The Z component of the target unit vector.

t float

The interpolation parameter, where 0.0 returns the start vector and 1.0 returns the target vector.

Returns

(float x, float y, float z)

A tuple containing the interpolated vector's X, Y, and Z components.

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

current double

The current position or value.

target double

The target position or value to reach.

currentVelocity double

A reference to the tracking velocity, which is updated internally by the function.

smoothTime double

The approximate time it will take to reach the target. Shorter values reach the target faster.

maxSpeed double

The maximum speed allowed during the movement transition.

deltaTime double

The 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

current float

The current position or value.

target float

The target position or value to reach.

currentVelocity float

A reference to the tracking velocity, which is updated internally by the function.

smoothTime float

The approximate time it will take to reach the target. Shorter values reach the target faster.

maxSpeed float

The maximum speed allowed during the movement transition.

deltaTime float

The 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

current double

A reference to the current angle in degrees, which is updated internally by the function.

target double

The target angle to reach in degrees.

currentVelocity double

A reference to the tracking angular velocity, which is updated internally by the function.

smoothTime double

The approximate time it will take to reach the target. Shorter values reach the target faster.

deltaTime double

The 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

current float

A reference to the current angle in degrees, which is updated internally by the function.

target float

The target angle to reach in degrees.

currentVelocity float

A reference to the tracking angular velocity, which is updated internally by the function.

smoothTime float

The approximate time it will take to reach the target. Shorter values reach the target faster.

deltaTime float

The 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

start double

The start value.

end double

The end value.

t double

The 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

start float

The start value.

end float

The end value.

t float

The interpolation value, clamped between 0.0 and 1.0.

Returns

float

The smoothly interpolated value between start and end.