Math

The Math module provides a comprehensive set of mathematical functions and utilities for FiveM resource development. It extends the standard Lua math library with additional functionality.

round

Rounds a number to the specified number of decimal places.

B2Lib.Math.round(num, decimals)

Parameters:

  • num (number): The number to round

  • decimals (number, optional): Number of decimal places (default: 0)

Returns: number - The rounded number

Example:

local rounded = B2Lib.Math.round(3.14159, 2) -- Returns 3.14
local wholeNumber = B2Lib.Math.round(3.7) -- Returns 4

clamp

Clamps a value between minimum and maximum bounds.

B2Lib.Math.clamp(value, min, max)

Parameters:

  • value (number): The value to clamp

  • min (number): The minimum allowed value

  • max (number): The maximum allowed value

Returns: number - The clamped value

Example:

lerp

Performs linear interpolation between two values.

Parameters:

  • a (number): The start value

  • b (number): The end value

  • t (number): The interpolation factor (0.0 = a, 1.0 = b)

Returns: number - The interpolated value

Example:

distance2D

Calculates the Euclidean distance between two 2D points.

Parameters:

  • x1 (number): X coordinate of the first point

  • y1 (number): Y coordinate of the first point

  • x2 (number): X coordinate of the second point

  • y2 (number): Y coordinate of the second point

Returns: number - The distance between the two points

Example:

distance3D

Calculates the Euclidean distance between two 3D points or vectors.

Parameters:

  • pos1 (vector3|table): The first position

  • pos2 (vector3|table): The second position

Returns: number - The distance between the two positions

Example:

normalize

Maps a value from one range to another range.

Parameters:

  • value (number): The value to normalize

  • min (number): The current range minimum

  • max (number): The current range maximum

  • newMin (number): The target range minimum

  • newMax (number): The target range maximum

Returns: number - The mapped value in the new range

Example:

toRadians

Converts degrees to radians.

Parameters:

  • degrees (number): The angle in degrees

Returns: number - The angle in radians

Example:

toDegrees

Converts radians to degrees.

Parameters:

  • radians (number): The angle in radians

Returns: number - The angle in degrees

Example:

getAngle

Calculates the angle between two 2D points.

Parameters:

  • x1 (number): X coordinate of the first point

  • y1 (number): Y coordinate of the first point

  • x2 (number): X coordinate of the second point

  • y2 (number): Y coordinate of the second point

Returns: number - The angle in radians

Example:

randomFloat

Generates a random floating-point number between min and max.

Parameters:

  • min (number): The minimum value (inclusive)

  • max (number): The maximum value (exclusive)

Returns: number - A random float in the specified range

Example:

randomInt

Generates a random integer between min and max.

Parameters:

  • min (number): The minimum value (inclusive)

  • max (number): The maximum value (inclusive)

Returns: number - A random integer in the specified range

Example:

isEven

Checks if a number is even.

Parameters:

  • num (number): The number to check

Returns: boolean - True if the number is even, false otherwise

isOdd

Checks if a number is odd.

Parameters:

  • num (number): The number to check

Returns: boolean - True if the number is odd, false otherwise

isPrime

Checks if a number is prime.

Parameters:

  • num (number): The number to check

Returns: boolean - True if the number is prime, false otherwise

factorial

Calculates the factorial of a number.

Parameters:

  • num (number): The number to calculate factorial for

Returns: number - The factorial of the number

gcd

Calculates the greatest common divisor of two numbers.

Parameters:

  • a (number): First number

  • b (number): Second number

Returns: number - The greatest common divisor

lcm

Calculates the least common multiple of two numbers.

Parameters:

  • a (number): First number

  • b (number): Second number

Returns: number - The least common multiple

formatNumber

Formats a number with thousands separators.

Parameters:

  • num (number): The number to format

  • separator (string, optional): The separator character (default: ",")

Returns: string - The formatted number

Example:

formatCurrency

Formats a number as currency.

Parameters:

  • amount (number): The amount to format

  • symbol (string, optional): The currency symbol (default: "$")

  • decimals (number, optional): Number of decimal places (default: 2)

Returns: string - The formatted currency

Example:

percentage

Calculates the percentage of a value relative to a total.

Parameters:

  • value (number): The value

  • total (number): The total

Returns: number - The percentage (0-100)

Example:

pointInCircle

Checks if a point is inside a circle.

Parameters:

  • pointX (number): X coordinate of the point

  • pointY (number): Y coordinate of the point

  • circleX (number): X coordinate of the circle center

  • circleY (number): Y coordinate of the circle center

  • radius (number): Radius of the circle

Returns: boolean - True if the point is inside the circle

pointInRectangle

Checks if a point is inside a rectangle.

Parameters:

  • pointX (number): X coordinate of the point

  • pointY (number): Y coordinate of the point

  • rectX (number): X coordinate of the rectangle (top-left)

  • rectY (number): Y coordinate of the rectangle (top-left)

  • width (number): Width of the rectangle

  • height (number): Height of the rectangle

Returns: boolean - True if the point is inside the rectangle

rgbToHex

Converts RGB color values to hexadecimal.

Parameters:

  • r (number): Red component (0-255)

  • g (number): Green component (0-255)

  • b (number): Blue component (0-255)

Returns: string - The hexadecimal color code

Example:

hexToRgb

Converts hexadecimal color to RGB values.

Parameters:

  • hex (string): The hexadecimal color code

Returns: table - RGB values {r, g, b}

Example:

rgbToHsv

Converts RGB color values to HSV.

Parameters:

  • r (number): Red component (0-255)

  • g (number): Green component (0-255)

  • b (number): Blue component (0-255)

Returns: table - HSV values {h, s, v}

hsvToRgb

Converts HSV color values to RGB.

Parameters:

  • h (number): Hue (0-360)

  • s (number): Saturation (0-100)

  • v (number): Value (0-100)

Returns: table - RGB values {r, g, b}

Configuration

Configure the math system in config.lua:

Troubleshooting

Precision Issues

  1. Use appropriate decimal places for rounding

  2. Be aware of floating-point precision limitations

  3. Use integer operations when possible for exact results

  4. Consider using string formatting for display purposes

Performance Considerations

  1. Cache frequently calculated values

  2. Use simpler functions when precision isn't critical

  3. Avoid unnecessary conversions between number types

  4. Use built-in math functions when available

Invalid Input Handling

  1. Validate input parameters before calculations

  2. Check for division by zero in custom functions

  3. Ensure angles are in expected ranges

  4. Verify color values are within valid ranges (0-255 for RGB)

Common Mistakes

  1. Mixing degrees and radians in calculations

  2. Not handling edge cases in geometric functions

  3. Incorrect range mapping with normalize function

  4. Using wrong coordinate systems for distance calculations

Last updated