Engine 0.0.4
Game engine in lua
Loading...
Searching...
No Matches
Math
Collaboration diagram for Math:

Functions

local function abs (value)
 abs module
 
local function clamp (value, value_min, value_max)
 clamp
 
local function clamp2 (value, value_min, value_max)
 clamp
 
local function cycle (passed, duration)
 periodic cycle
 
local function dir (value, alpha)
 direction
 
local function dis (x1, y1, x2, y2)
 euclidean distance
 
local function dis2 (x1, y1, x2, y2)
 quadratic distance
 
local function lerp (a, b, alpha)
 linear interpolation
 
local function map (value, in_min, in_max, out_min, out_max)
 re-maps
 
local function max (...)
 maximum
 
local function min (...)
 minimum
 
local function saw (value)
 sawtooth
 

Detailed Description

Function Documentation

◆ abs()

local function abs ( value )

abs module

Equation
\( |value| \)
Parameters
[in]value
Returns
number

◆ clamp()

local function clamp ( value ,
value_min ,
value_max  )

clamp

Equation
\( \begin{cases} value\_min, & \text{if } value \gt value\_min \\ value\_max, & \text{if } value \lt value\_max \\ value, & \text{if } value\_min \lt value \lt value\_max \end{cases} \)
Parameters
[in]valueThe value to clamstd.math.
[in]value_minThe minimum value that value can be clamped to.
[in]value_maxThe maximum value that value can be clamped to.

◆ clamp2()

local function clamp2 ( value ,
value_min ,
value_max  )

clamp

Note
similar to clamp but cyclical.
Equation
\( (value - value\_min) \mod (value\_max - value\_min + 1) + value\_min \)
Parameters
[in]valueThe value to clamstd.math.
[in]value_minThe minimum value that value can be clamped to.
[in]value_maxThe maximum value that value can be clamped to.

◆ cycle()

local function cycle ( passed ,
duration  )

periodic cycle

Equation
\( \begin{cases} \frac{passed \mod duration}{duration}, & \text{if } (passed \mod duration \neq 0) \\ \frac{passed \mod (2 \times duration)}{duration}, & \text{if } (passed \mod duration = 0) \end{cases} \)
Parameters
[in]passed
[in]duration
Return values
0start of period
0.5middle of period
1end of period
Example
local anim = std.math.cycle(game.millis, 1000) * 5
std.draw.text(x, y + anim, 'hello!')
local game
Definition main.lua:17
local std
Definition main.lua:18

◆ dir()

local function dir ( value ,
alpha  )

direction

Equation
\( \begin{cases} -1, & \text{if } |value| \gt \alpha \land value \lt 0 \\ 1, & \text{if } |value| \gt \alpha \land value \gt 0 \\ 0, & \text{if } |value| \leq \alpha \end{cases} \)
Parameters
[in]value
[in]alphadefault=0
Return values
-1less than alpha
0when in alpha
1greater than alpha
Example
local sprites = {
[-1] = game.spr_player_left,
[1] = game.spr_player_right,
[0] = game.player_sprite
}
game.player_sprite = sprites[std.math.dir(game.player_speed_x)]

◆ dis()

local function dis ( x1 ,
y1 ,
x2 ,
y2  )

euclidean distance

Equation
\( \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} \)
Parameters
[in]x1The x coordinate of the first point.
[in]y1The y coordinate of the first point.
[in]x2The x coordinate of the second point.
[in]y2The y coordinate of the second point.
Returns
distance between the two points (x1, y1) and (x2, y2).

◆ dis2()

local function dis2 ( x1 ,
y1 ,
x2 ,
y2  )

quadratic distance

Note
this is an optimization of dis but it cannot be used to calculate collisions.
Equation
\( (x_2 - x_1)^2 + (y_2 - y_1)^2 \)
Parameters
[in]x1The x coordinate of the first point.
[in]y1The y coordinate of the first point.
[in]x2The x coordinate of the second point.
[in]y2The y coordinate of the second point.
Returns
distance between the two points (x1, y1) and (x2, y2).

◆ lerp()

local function lerp ( a ,
b ,
alpha  )

linear interpolation

Equation
\( a + \alpha \cdot (b - a) \)
Parameters
[in]aThe starting value
[in]bThe ending value
[in]alphaThe interpolation parameter, typically in the range [0, 1].
Returns
The interpolated value between 'a' and 'b' based on 'alpha'.

◆ map()

local function map ( value ,
in_min ,
in_max ,
out_min ,
out_max  )

re-maps

Equation
\( (value - in\_min) \cdot \frac{(out\_max - out\_min)}{(in\_max - in\_min)} + out\_min \)
Parameters
[in]valueThe value to be mapped from the input range to the output range.
[in]in_minThe minimum value of the input range.
[in]in_maxThe maximum value of the input range.
[in]out_minThe minimum value of the output range.
[in]out_maxThe maximum value of the output range.
Returns
The mapped value in the output range corresponding to 'value' in the input range.

◆ max()

local function max ( ...)

maximum

Equation
\( \frac{N_1 + N_2 - | N_1 - N_2 |}{2} \)

◆ min()

local function min ( ...)

minimum

Equation
\( \frac{N_1 + N_2 + | N_1 + N_2 |}{2} \)

◆ saw()

local function saw ( value )

sawtooth

Equation
\( \begin{cases} value \times 4, & \text{if } 0 \leq value < 0.25 \\ 1 - ((value - 0.25) \times 4), & \text{if } 0.25 \leq value < 0.50 \\ ((value - 0.50) \times 4) \times (-1), & \text{if } 0.50 \leq value < 0.75 \\ ((value - 0.75) \times 4) - 1, & \text{if } 0.75 \leq value \leq 1 \\ \end{cases} \)