Caffeine

Math

Reference Core Last updated: Lua 23 · Dryad 0

Vector tables ({x, y[, z]}), scalar helpers and angle conversion. Also defines the constants caffeine.math.pi and caffeine.math.epsilon (1e-5).

All namespaces

Namespace: caffeine.math

vec2#

Lua: caffeine.math.vec2

text
vec2(x, y) -> table

Creates a 2D vector table.

ParametersTypeDescription
xnumberX.
ynumberY.

Returns: table. {x, y}

Example

lua
local v = caffeine.math.vec2(1, 2)

vec3#

Lua: caffeine.math.vec3

text
vec3(x, y, z) -> table

Creates a 3D vector table.

ParametersTypeDescription
xnumberX.
ynumberY.
znumberZ.

Returns: table. {x, y, z}

Example

lua
local v = caffeine.math.vec3(1, 2, 3)

add#

Lua: caffeine.math.add

text
add(a, b) -> table

Component-wise sum of two vectors.

ParametersTypeDescription
atableVector table {x, y[, z]}.
btableVector table {x, y[, z]}.

Returns: table. Vector table; has z only when an operand has z.

Example

lua
local p = caffeine.math.add(a, b)

sub#

Lua: caffeine.math.sub

text
sub(a, b) -> table

Component-wise difference a - b.

ParametersTypeDescription
atableVector table {x, y[, z]}.
btableVector table {x, y[, z]}.

Returns: table. Vector table; has z only when an operand has z.

Example

lua
local d = caffeine.math.sub(a, b)

mul#

Lua: caffeine.math.mul

text
mul(a, b) -> table

Component-wise product of two vectors (not a dot product).

ParametersTypeDescription
atableVector table {x, y[, z]}.
btableVector table {x, y[, z]}.

Returns: table. Vector table; has z only when an operand has z.

Example

lua
local m = caffeine.math.mul(a, b)

scale#

Lua: caffeine.math.scale

text
scale(v, s) -> table

Multiplies a vector by a scalar.

ParametersTypeDescription
vtableVector table {x, y[, z]}.
snumberScalar factor.

Returns: table. Scaled vector.

Example

lua
local v2 = caffeine.math.scale(v, 2)

dot#

Lua: caffeine.math.dot

text
dot(a, b) -> number

Dot product of two vectors; a missing z counts as 0.

ParametersTypeDescription
atableVector table {x, y[, z]}.
btableVector table {x, y[, z]}.

Returns: number. Dot product.

Example

lua
local d = caffeine.math.dot(a, b)

cross#

Lua: caffeine.math.cross

text
cross(a, b) -> table

Cross product; the result is always 3D.

ParametersTypeDescription
atableVector table {x, y[, z]}.
btableVector table {x, y[, z]}.

Returns: table. {x, y, z} vector.

Example

lua
local n = caffeine.math.cross(a, b)

length#

Lua: caffeine.math.length

text
length(v) -> number

Length of a vector.

ParametersTypeDescription
vtableVector table {x, y[, z]}.

Returns: number. Vector length.

Example

lua
local l = caffeine.math.length(v)

lengthSquared#

Lua: caffeine.math.lengthSquared

text
lengthSquared(v) -> number

Squared length of a vector, without the square root.

ParametersTypeDescription
vtableVector table {x, y[, z]}.

Returns: number. Squared length.

Example

lua
local l2 = caffeine.math.lengthSquared(v)

distance#

Lua: caffeine.math.distance

text
distance(a, b) -> number

Distance between two points.

ParametersTypeDescription
atableVector table {x, y[, z]}.
btableVector table {x, y[, z]}.

Returns: number. Distance between the points.

Example

lua
local d = caffeine.math.distance(a, b)

normalize#

Lua: caffeine.math.normalize

text
normalize(v) -> table

Returns the vector scaled to length 1.

ParametersTypeDescription
vtableVector table {x, y[, z]}.

Returns: table. Unit vector, or zeros when the length is at most 0.0001.

Example

lua
local dir = caffeine.math.normalize(caffeine.math.sub(b, a))

lerpVec#

Lua: caffeine.math.lerpVec

text
lerpVec(a, b, t) -> table

Linear interpolation between two vectors.

ParametersTypeDescription
atableVector table {x, y[, z]}.
btableVector table {x, y[, z]}.
tnumberBlend factor; not clamped.

Returns: table. Vector table; has z only when an operand has z.

Example

lua
local mid = caffeine.math.lerpVec(a, b, 0.5)

clamp#

Lua: caffeine.math.clamp

text
clamp(v, lo, hi) -> number

Limits a value to a range; swapped bounds are accepted.

ParametersTypeDescription
vnumberValue.
lonumberLower bound.
hinumberUpper bound.

Returns: number. Clamped value.

Example

lua
local hp = caffeine.math.clamp(hp, 0, 100)

clamp01#

Lua: caffeine.math.clamp01

text
clamp01(v) -> number

Limits a value to 0..1.

ParametersTypeDescription
vnumberValue.

Returns: number. Value limited to 0..1.

Example

lua
local t = caffeine.math.clamp01(t)

lerp#

Lua: caffeine.math.lerp

text
lerp(a, b, t) -> number

Linear interpolation between two numbers.

ParametersTypeDescription
anumberStart.
bnumberEnd.
tnumberBlend factor; not clamped.

Returns: number. a + (b - a) * t

Example

lua
local x = caffeine.math.lerp(0, 10, 0.25)

inverseLerp#

Lua: caffeine.math.inverseLerp

text
inverseLerp(a, b, v) -> number

The inverse of lerp; the result is not clamped.

ParametersTypeDescription
anumberStart.
bnumberEnd.
vnumberValue.

Returns: number. Position of v between a and b; 0 when a and b are equal.

Example

lua
local t = caffeine.math.inverseLerp(0, 10, 5)

remap#

Lua: caffeine.math.remap

text
remap(v, inLo, inHi, outLo, outHi) -> number

Maps a value from one range to another.

ParametersTypeDescription
vnumberValue.
inLonumberInput range start.
inHinumberInput range end.
outLonumberOutput range start.
outHinumberOutput range end.

Returns: number. Remapped value (not clamped).

Example

lua
local pct = caffeine.math.remap(hp, 0, 100, 0, 1)

smoothstep#

Lua: caffeine.math.smoothstep

text
smoothstep(e0, e1, v) -> number

Smooth Hermite interpolation between two edges.

ParametersTypeDescription
e0numberLower edge.
e1numberUpper edge.
vnumberValue.

Returns: number. Smooth value in 0..1.

Example

lua
local s = caffeine.math.smoothstep(0, 1, t)

approach#

Lua: caffeine.math.approach

text
approach(cur, target, step) -> number

Moves a value toward a target by at most step.

ParametersTypeDescription
curnumberCurrent value.
targetnumberTarget value.
stepnumberMaximum change; its absolute value is used.

Returns: number. New value, never past the target.

Example

lua
speed = caffeine.math.approach(speed, 0, 20 * caffeine.time.delta())

sign#

Lua: caffeine.math.sign

text
sign(v) -> number

Sign of a number.

ParametersTypeDescription
vnumberValue.

Returns: number. -1, 0 or 1.

Example

lua
local s = caffeine.math.sign(-3)

radians#

Lua: caffeine.math.radians

text
radians(deg) -> number

Converts degrees to radians.

ParametersTypeDescription
degnumberDegrees.

Returns: number. Radians.

Example

lua
local r = caffeine.math.radians(90)

degrees#

Lua: caffeine.math.degrees

text
degrees(rad) -> number

Converts radians to degrees.

ParametersTypeDescription
radnumberRadians.

Returns: number. Degrees.

Example

lua
local d = caffeine.math.degrees(caffeine.math.pi)