MathUtil
A collection of common math functions.
Namespace:
namespace Lenga\Engine\Core;
class MathUtil
Methods
abs
public static function abs(float $f)
Returns the absolute value of f.
ceil
public static function ceil(float $f)
Returns the smallest integer greater than or equal to f.
ceilToInt
public static function ceilToInt(float $f)
Returns the smallest integer greater than or equal to f (as int).
floor
public static function floor(float $f)
Returns the largest integer smaller than or equal to f.
floorToInt
public static function floorToInt(float $f)
Returns the largest integer smaller than or equal to f (as int).
round
public static function round(float $f)
Returns f rounded to the nearest integer.
roundToInt
public static function roundToInt(float $f)
Returns f rounded to the nearest integer (as int).
sign
public static function sign(float $f)
Returns the mathematical sign of f: -1, 0 or 1.
sqrt
public static function sqrt(float $f)
Returns the square root of f.
pow
public static function pow(float $f, float $p)
Returns f raised to the power p.
exp
public static function exp(float $power)
Returns e raised to the power power.
log
public static function log(float $f, float $base = M_E)
Returns the logarithm of f in a specified base.
log10
public static function log10(float $f)
Returns the base-10 logarithm of f.
acos
public static function acos(float $f)
Returns the arc-cosine of f (radians).
asin
public static function asin(float $f)
Returns the arc-sine of f (radians).
atan
public static function atan(float $f)
Returns the arc-tangent of f (radians).
atan2
public static function atan2(float $y, float $x)
Returns the angle in radians whose tangent is y/x.
cos
public static function cos(float $f)
Returns the cosine of angle f (radians).
sin
public static function sin(float $f)
Returns the sine of angle f (radians).
tan
public static function tan(float $f)
Returns the tangent of angle f (radians).
clamp
public static function clamp(float $value, float $min, float $max)
Clamps value between min and max and returns the clamped value.
clamp01
public static function clamp01(float $value)
Clamps value between 0 and 1.
min
public static function min(float $a, float ...$rest)
Returns the minimum of two or more values.
max
public static function max(float $a, float ...$rest)
Returns the maximum of two or more values.
repeat
public static function repeat(float $t, float $length)
Loops the value t so that it is never larger than length and never
pingPong
public static function pingPong(float $t, float $length)
PingPongs the value t between 0 and length.
wrap
public static function wrap(float $value, float $min, float $max)
Wraps a value into the [min, max) range.
lerp
public static function lerp(float $a, float $b, float $t)
Linearly interpolates between a and b by t (clamped to [0, 1]).
lerpUnclamped
public static function lerpUnclamped(float $a, float $b, float $t)
Linearly interpolates between a and b by t with no clamping.
lerpAngle
public static function lerpAngle(float $a, float $b, float $t)
Same as {@see lerp()} but interpolates correctly when values wrap
inverseLerp
public static function inverseLerp(float $a, float $b, float $t)
Determines the interpolation factor of value between a and b
smoothStep
public static function smoothStep(float $from, float $to, float $t)
Interpolates between from and to with smoothing at the limits.
smoothDamp
public static function smoothDamp(float $current,
float $target,
float &$currentVelocity,
float $smoothTime,
float $deltaTime,
float $maxSpeed = self::INFINITY,)
Gradually moves the current value towards a target value, over a
smoothDampAngle
public static function smoothDampAngle(float $current,
float $target,
float &$currentVelocity,
float $smoothTime,
float $deltaTime,
float $maxSpeed = self::INFINITY,)
Gradually changes an angle given in degrees towards a desired goal
moveTowards
public static function moveTowards(float $current, float $target, float $maxDelta)
Moves a value current towards target.
moveTowardsAngle
public static function moveTowardsAngle(float $current, float $target, float $maxDelta)
Same as {@see moveTowards()} but handles angles wrapping around
approximately
public static function approximately(float $a, float $b)
Compares two floating-point values and returns true if they are
deltaAngle
public static function deltaAngle(float $current, float $target)
Calculates the shortest difference between two angles in degrees.
closestPowerOfTwo
public static function closestPowerOfTwo(int $value)
Returns the closest power of two to the given value.
isPowerOfTwo
public static function isPowerOfTwo(int $value)
Returns true if the value is a power of two.
nextPowerOfTwo
public static function nextPowerOfTwo(int $value)
Returns the next power of two that is equal to or greater than the
perlinNoise
public static function perlinNoise(float $x, float $y)
Generates 2D Perlin noise for the given coordinates.
perlinNoise1D
public static function perlinNoise1D(float $x)
Generates 1D pseudo-random pattern of float values across a 2D
floatToHalf
public static function floatToHalf(float $f)
Encode a 32-bit float into its IEEE 754 half-precision (16-bit)
halfToFloat
public static function halfToFloat(int $half)
Convert a half-precision float (16-bit int) to a 32-bit float.
gammaToLinearSpace
public static function gammaToLinearSpace(float $value)
Converts a value from gamma (sRGB) to linear colour space.
linearToGammaSpace
public static function linearToGammaSpace(float $value)
Converts a value from linear to gamma (sRGB) colour space.
correlatedColorTemperatureToRGB
public static function correlatedColorTemperatureToRGB(float $kelvin)
Converts a colour temperature in Kelvin to an approximate RGB
Example
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\MathUtil;
class MovementController extends Behaviour
{
public function update(): void
{
// Clamp a value between min and max
$speed = MathUtil::clamp($desiredSpeed, 0.0, 10.0);
// Linear interpolation for smooth transitions
$currentAlpha = MathUtil::lerp($currentAlpha, $targetAlpha, 0.1);
// Wrap angle values
$angle = MathUtil::wrap($angle, 0.0, 360.0);
// Smooth movement towards a target
$velocity = 0.0;
$currentValue = MathUtil::smoothDamp(
$currentValue,
$targetValue,
$velocity,
0.3,
Time::deltaTime()
);
}
}