Skip to content

Math

Generated

This page is auto-generated from psxsplash-main/src/luaapi.hh by scripts/py/gen_lua_api_docs.py. Edits won't survive the next build — fix the source comments instead.

14 entries, 2 with worked examples mined from godot-ps1/demo/scripts/ and the bundled templates.

Methods

Math.Clamp(value, min, max) -> number

Returns value constrained to the [min, max] range.

Math.Lerp(a, b, t) -> number

Linear interpolation: returns a when t=0, b when t=1, blend in between. Scalar version of Vec3.lerp.

Math.Sign(value) -> number

Returns -1 for negative, 0 for zero, +1 for positive. Useful for flipping facing direction or "which way to step" decisions.

Math.Abs(value) -> number

Absolute value. Works on both integers and FixedPoint<12>.

Example

if Math.Abs(lx) < 200 and Math.Abs(ly) < 200 then

Source: godot-ps1/demo/scripts/boss_smoke_player.lua line 66.

Math.Min(a, b) -> number

Smaller of the two values. For lists, fold pair-wise.

Math.Max(a, b) -> number

Larger of the two values.

Math.Floor(fp) -> integer

Floors a FixedPoint<12> to the next integer toward -infinity. Accepts either a FixedPoint object or a plain integer (identity).

Math.Ceil(fp) -> integer

Ceilings a FixedPoint<12> to the next integer toward +infinity.

Math.Round(fp) -> integer

Rounds a FixedPoint<12> to the nearest integer. Half-values tie toward +infinity so round(0.5) = 1, round(-0.5) = 0.

Math.ToInt(fp) -> integer

Truncates a FixedPoint<12> toward zero. Inverse of Math.ToFixed for non-negative whole values. Use Floor if you want the round-toward-minus-infinity semantics.

Math.Sqrt(value) -> integer

Integer square root via bit-by-bit binary search — at most 16 iterations of shift + compare + sub, no divides. Accurate result floor(sqrt(value)). Returns 0 for value <= 0.

For "is this within range" checks, compare squared distances instead — it's free. For "how far is this thing" gameplay logic where 3-5% error is acceptable, use Math.LengthApprox which is ~5-10× faster.

Math.LengthApprox(x, y) -> integer

2D vector magnitude via the "alpha max plus beta min" approximation: |v| ≈ 0.9604·max(|x|,|y|) + 0.3978·min(|x|,|y|). Maximum error ~3.4%, zero iterations, two multiplies + a shift. Standard PSX-era game-math trick — much faster than Sqrt(xx + yy) and indistinguishable for "boss is N units away" feel.

Inputs are raw fp12 (or any consistent units); output matches.

Math.Atan2(y, x) -> number

Heading angle in pi-fractions (matches Entity.SetRotationY's convention: 1.0 = π, 0.5 = 90°). Inputs are FP12 — the runtime uses an octant-fold approximation, identical to the helper feeding lock-on yaw. Common AI pattern: local d = Vec3.sub(targetPos, selfPos) local heading = Math.Atan2(d.x, d.z)

Example

local heading = Math.Atan2(dx, dz)

Source: godot-ps1/demo/scripts/boss_smoke_brain.lua line 51.

Math.ToFixed(integer) -> FixedPoint

Promotes a plain integer to a FixedPoint<12>. Equivalent to FixedPoint.new(integer, 0) but shorter.