Module: nupp.math
Math helpers#
nupp.math adds the scalar and two-dimensional operations missing from Lua's built-in math table. It is pure generated Lua and adds no native dependency.
lerp(from, to, t) linearly interpolates without clamping t, so factors outside [0, 1] extrapolate. It returns from exactly at zero and to exactly at one. wrapAngle(radians) returns the equivalent angle in [-π, π). deltaAngle(from, to) returns the shortest signed rotation from one angle to another.
nupp.math.vec2 represents vectors as (x, y) number pairs rather than allocated objects. Multiple return values make composition direct:
Arithmetic operations are add, subtract, scale, dot, and cross. Measurement operations are length, lengthSquared, distance, and distanceSquared. Motion and orientation operations are normalize, lerp, moveTowards, rotate, angle, angleBetween, and signedAngleBetween. project projects onto another vector; reflect reflects across a normal.
| Operation | Signature shape |
|---|---|
add, subtract |
(ax, ay, bx, by) -> x, y |
scale |
(x, y, factor) -> x, y |
dot, cross |
(ax, ay, bx, by) -> number |
length, lengthSquared |
(x, y) -> number |
distance, distanceSquared |
(ax, ay, bx, by) -> number |
normalize |
(x, y) -> x, y |
lerp |
(ax, ay, bx, by, t) -> x, y |
moveTowards |
(ax, ay, bx, by, maxDistance) -> x, y |
rotate |
(x, y, radians) -> x, y |
angle |
(x, y) -> radians |
angleBetween, signedAngleBetween |
(ax, ay, bx, by) -> radians |
project, reflect |
(x, y, axisX, axisY) -> x, y |
normalize(0, 0) and projection onto the zero vector return (0, 0). moveTowards snaps exactly to the destination when the remaining distance is within the requested step, and a nonpositive step leaves the start unchanged. Angle-between operations involving a zero vector return zero; reflection across a zero normal returns the original vector. lerp is unclamped. Use squared length/distance when only comparing magnitudes, to avoid an unnecessary square root.
See the standard-library overview for selection and lazy-loading rules.
Scalar helpers and two-dimensional vectors.
Submodules
| Module | Description |
|---|---|
nupp.math.vec2 | Two-dimensional vector operations over (x, y) number pairs. |
Module contents
Functions
| Function | Kind | Description |
|---|---|---|
deltaAngle | function | Computes the shortest signed rotation from one angle to another. |
lerp | function | Linearly interpolates between two numbers without clamping the factor. |
wrapAngle | function | Wraps an angle to the equivalent value in [-pi, pi). |
Functions#
deltaAnglefunction#
Computes the shortest signed rotation from one angle to another.
local deltaAngle: function(from: number, to: number): numberArguments
| Name | Type | Description |
|---|---|---|
from | number | the starting angle in radians |
to | number | the target angle in radians |
Returns
| Type | Description |
|---|---|
number | the signed rotation in |
lerpfunction#
Linearly interpolates between two numbers without clamping the factor.
local lerp: function(from: number, to: number, t: number): numberArguments
| Name | Type | Description |
|---|---|---|
from | number | the value returned when |
to | number | the value returned when |
t | number | the interpolation factor |
Returns
| Type | Description |
|---|---|
number |
|
wrapAnglefunction#
Wraps an angle to the equivalent value in [-pi, pi).
local wrapAngle: function(radians: number): numberArguments
| Name | Type | Description |
|---|---|---|
radians | number | the angle in radians |
Returns
| Type | Description |
|---|---|
number | the wrapped angle |