Skip to main content

Math

Contains functions which Figura adds to the default Lua "math" library table

warning

This page is a WIP. It contains all the information in Figura's documentation but we're working on adding more helpful descriptions.


Methods

clamp()

Clamps the given value between min and max

clamp(value, min, max)

Parameters:

NameTypeDescriptionDefault
valueNumber--
minNumber--
maxNumber--

Returns:

TypeDescription
Number-

Example:

math.clamp(58, 0, 50) -- returns 50
math.clamp(-12, 0, 50) -- returns 0
math.clamp(26, 0, 50) -- returns 26

lerp()

Linearly interpolates from its first argument to its second argument, with the third argument as the parameter

Works on both regular numbers, vectors of the same type, and matrices of the same type

lerp(a, b, t)

Parameters:

NameTypeDescriptionDefault
aNumber--
bNumber--
tNumber--

Returns:

TypeDescription
Number-

Example:

local rot = 0
local _rot = 0

function events.tick()
_rot = rot
rot = rot + 1
end

function events.render(delta)
models.model:setRot(math.lerp(_rot, rot, delta))
end

lerpAngle()

Similar to the default lerp function, but numbers are limited to the range of 0-360

Lerp is done towards the shortest angle

For example, a lerp of 340 and 20, with a factor of 0.75, will return 10

lerpAngle(a, b, t)

Parameters:

NameTypeDescriptionDefault
aNumber--
bNumber--
tNumber--

Returns:

TypeDescription
Number-

Example:

math.lerpAngle(340, 20, 0.75) -- returns 10

map()

Maps the given value from one range to another

For example, if you have a value of 20 in the range 0-200, and you want to map it to the range 100-200, the result will be 110

map(value, oldMin, oldMax, newMin, newMax)

Parameters:

NameTypeDescriptionDefault
valueNumber--
oldMinNumber--
oldMaxNumber--
newMinNumber--
newMaxNumber--

Returns:

TypeDescription
Number-

Example:

math.map(20, 0, 200, 100, 200) -- returns 110

round()

Rounds the given number to the nearest whole integer

round(value)

Parameters:

NameTypeDescriptionDefault
valueNumber--

Returns:

TypeDescription
Number-

Example:

math.round(21.74) -- returns 22

shortAngle()

Returns the shortest angle between two angles

For example, if you have an angle of 350 degrees and you want to get the shortest angle between it and 0 degrees, the result will be 10 degrees

shortAngle(from, to)

Parameters:

NameTypeDescriptionDefault
fromNumber--
toNumber--

Returns:

TypeDescription
Number-

Example:

math.shortAngle(350, 0) -- returns 10

sign()

Returns the sign of the given number

Returns 1 if the number is positive, -1 if it's negative, and 0 if it's 0

sign(value)

Parameters:

NameTypeDescriptionDefault
valueNumber--

Returns:

TypeDescription
Number-

Example:

math.sign(124) -- returns 1
math.sign(-12) -- returns -1
math.sign(0) -- returns 0

Fields

playerScale

The constant of the player scaling related to the world

Example:

math.playerScale

worldScale

The constant of the world scaling related with the player

Example:

math.worldScale