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.