Mathf
Provides a comprehensive set of mathematical functions and constants for game development, including operations like absolute value, square root, and trigonometric calculations, as well as game-specific algorithms like smooth damping and lerp.
Properties
Represents the mathematical constant Pi for circular and trigonometric calculations.
Represents positive infinity, useful for comparisons.
local inf = Mathf.Infinity
print("Positive infinity: " .. inf)
Represents negative infinity, useful for comparisons.
local negInf = Mathf.NegativeInfinity
print("Negative infinity: " .. negInf)
Conversion factor for degrees to radians.
local deg2Rad = Mathf.Deg2Rad
print("Degrees to radians: " .. deg2Rad)
Conversion factor for radians to degrees.
local rad2Deg = Mathf.Rad2Deg
print("Radians to degrees: " .. rad2Deg)
Represents the smallest positive double greater than zero, preventing division by zero errors.
local eps = Mathf.Epsilon
print("Smallest positive double: " .. eps)
Methods
Calculates and returns the absolute value of its input.
local value = -5
local absValue = Mathf.Abs(value)
print("Absolute value: " .. absValue)
Parameters
value
A number to find the absolute value of
Returns
The absolute value of the input number.
Returns the angle in radians whose cosine is the specified number, for trigonometric math operations.
local f = 0.5
local angle = Mathf.Acos(f)
print("Angle in radians: " .. angle)
Parameters
f
A number representing a cosine, where -1 ≤ f ≤ 1
Returns
Angle in radians, within the range [0, π].
Compares two floating point values for approximate equality, to handle floating point arithmetic inconsistencies.
local a = 0.1
local b = 0.2
local equal = Mathf.Approximately(a, b)
print("Approximately equal: " .. tostring(equal))
Parameters
a
The first number to compare
b
The second number to compare
Returns
True if the values are approximately equal, else false.
Returns the angle in radians whose sine is the specified number, useful for trigonometric operations.
local f = 0.5
local angle = Mathf.Asin(f)
print("Angle in radians: " .. angle)
Parameters
f
A number representing a sine, where -1 ≤ f ≤ 1
Returns
Angle in radians within the range [-1/2π, 1/2π].
Returns the angle in radians whose tangent is the specified number, for trigonometric operations.
local f = 0.5
local angle = Mathf.Atan(f)
print("Angle in radians: " .. angle)
Parameters
f
A number representing a tangent
Returns
Angle in radians within the range [-π/2, π/2].
Returns the angle in radians whose tan is y/x, useful in game physics calculations.
local y = 1
local x = 1
local angle = Mathf.Atan2(y, x)
print("Angle in radians: " .. angle)
Parameters
y
The y-coordinate of a point
x
The x-coordinate of a point
Returns
Angle in radians within the range [-π, π].
Rounds a number up to the closest integer not less than the specified number, useful for setting upper limit values.
local f = 5.5
local ceilValue = Mathf.Ceil(f)
print("Ceiling value: " .. ceilValue)
Parameters
f
The number to round up
Returns
The smallest integer not less than f.
Rounds a number up to the nearest integer, returning an integer value, useful when working with discrete values.
local f = 5.5
local ceilValue = Mathf.CeilToInt(f)
print("Ceiling value: " .. ceilValue)
Parameters
f
The number to round up
Returns
The smallest integer not less than f.
Restricts a number to be within a specified range, great for setting limits on variables or return values.
local value = 5
local min = 0
local max = 10
local clampedValue = Mathf.Clamp(value, min, max)
print("Clamped value: " .. clampedValue)
Parameters
value
The number to clamp
min
The lower boundary of the range
max
The upper boundary of the range
Returns
The clamped number between min and max.
Restricts a number to be within the range [0,1], handy for ensuring proportions or percentages.
local value = 5
local clampedValue = Mathf.Clamp01(value)
print("Clamped value: " .. clampedValue)
Parameters
value
The number to clamp
Returns
The clamped value between 0 and 1.
Returns the power of two closest to a provided number, useful in texture resolutions.
local value = 100
local closestPower = Mathf.ClosestPowerOfTwo(value)
print("Closest power of two: " .. closestPower)
Parameters
value
The number to find the closest power of two for
Returns
The power of two closest to the input value.
Computes and returns the cosine of the specified angle in radians, for trigonometric operations.
local angle = 0.5
local cosine = Mathf.Cos(angle)
print("Cosine of the angle: " .. cosine)
Parameters
a
An angle, in radians
Returns
The cosine of the angle.
Determines and returns the smallest difference between two angles, key to minimal rotation direction.
local current = 45
local target = 315
local delta = Mathf.DeltaAngle(current, target)
print("Angle difference: " .. delta)
Parameters
current
The first angle, in degrees
target
The second angle, in degrees
Returns
The shortest angle difference, in degrees.