🔢Math

Usage: math.{func}

This table extends the existing math table that is a part of Lua.

calc_angle

Function

Calculates angles between 2 vectors.

Arguments

Name
Type
Description

src

Source vector.

dst

Destination vector.

Returns

Type
Description

Angles.

Example

local ang = math.calc_angle(vec1, vec2);

angle_normalize

Function

Normalizes an angle.

Arguments

Name
Type
Description

angle

float

Input angle.

Returns

Type
Description

float

Normalized angle.

Example

local norm = math.angle_normalize(560);

approach_angles

Function

Approaches an angle over time.

Arguments

Name
Type
Description

from

Start angle.

to

End angle.

speed

float

Approach speed.

Returns

Type
Description

Delta angle.

Example

local ang = math.approach_angles(from, to, 1.0 / game.global_vars.frametime);

edge_point

Function

Returns a point on the edge of a box.

Arguments

Name
Type
Description

mins

Box mins.

maxs

Box maxs.

dir

Point direction (angle).

radius

float

Area radius.

Returns

Type
Description

Point.

Example

local point = math.edge_point(mins, maxs, dir, 5);

lerp

Function

Linear interpolation.

Arguments

Name
Type
Description

t1

float

Start value.

t2

float

End value.

progress

float

Interpolation amount.

Returns

Type

Description

float

Interpolated value.

Example

local mid = math.lerp(0, 100, 0.5);

vector_angles

Function

Calculates angles from a vector.

Arguments

Name
Type
Description

forward

Source vector.

up

Up vector. Defaults to nil.

Returns

Type
Description

Angles.

Example

local ang = math.vector_angles(fw);

world_to_screen

Function

Transforms a point in the game world onto the viewport.

Arguments

Name
Type
Description

xyz

Point in the world.

round

bool

Whether should round the output. Defaults to true.

Returns

Type
Description

Point on the viewport.

Example

local point = math.world_to_screen(game_point);

clamp

Function

Clamps a value between min and max.

Arguments

Name
Type
Description

n

float

Value.

lower

float

Lowest value.

upper

float

Highest value.

Returns

Type
Description

float

Clamped value.

Example

local x = math.clamp(50, 5, 25); -- 25

remap_val

Function

Maps the value from one range to another range.

Arguments

Name
Type
Description

val

float

Value.

a

float

Lowest source value.

b

float

Highest source value.

c

float

Lowest destination value.

d

float

Highest destination value.

Returns

Type
Description

float

Mapped value.

Example

local mapped = math.remap_val(0.5, 0, 1, 0, 100); -- 50

remap_val_clamped

Function

Maps the value from one range to another range. Additionally, clamps the value based on the source range.

Arguments

Name
Type
Description

val

float

Value.

a

float

Lowest source value.

b

float

Highest source value.

c

float

Lowest destination value.

d

float

Highest destination value.

Returns

Type
Description

float

Mapped value.

Example

local mapped = math.remap_val_clamped(5, 0, 1, 0, 100); -- 100

vec2

Function

An alias to draw.vec2().

Example

local vec = math.vec2(5, 5);

vec3

Function

An alias to vector().

Example

local vec = math.vec3(5, 12, 5);

Last updated