nacatamalon/math
#clampfunctionnacatamalon/math
clamp(value: number, min: number, max: number) => number
Keeps value between min and max: below min it gives min, above max it gives max,
and in between the value itself. It is what keeps a player inside the screen or a volume
between 0 and 1, and it replaces Math.min(max, Math.max(min, value)), which is easy to write
with the two the wrong way round.
Parámetros
valuenumberThe number to keep in range.
minnumberThe lowest it may be.
maxnumberThe highest it may be.
Ejemplo
declare const hero: TSprite;
declare const speed: number;
useUpdate((delta) => {
hero.transform.x = clamp(hero.transform.x + speed * delta, 8, 312);
});
Ver el código · math/clamp.ts:26#degToRadfunctionnacatamalon/math
degToRad(deg: number) => number
Converts deg (degrees) to radians: the unit every rotation in the engine uses
(sprite transform.rotation, a camera's rotation, a 3D object's rotationX and rotationY). Lets you author
angles the way you think about them: transform.rotation = degToRad(45).
Parámetros
degnumberThe angle in degrees.
Ver el código · math/angle/deg_to_rad.ts:15#lerpfunctionnacatamalon/math
lerp(a: number, b: number, t: number) => number
Linear interpolation between numbers a and b: t = 0 returns a, t = 1
returns b. Not clamped. The scalar building block under fades, manual tweens, and
lerpColor / vec2Lerp. GLSL calls this mix.
Parámetros
tnumberHow far along, 0 to 1.
Ver el código · math/lerp.ts:15#lerpAnglefunctionnacatamalon/math
lerpAngle(a: number, b: number, t: number) => number
Interpolates from angle a toward b (both radians) along the shortest arc,
so going from 350° to 10° turns 20° forward instead of 340° back. Ideal for
smoothly steering a sprite toward a target heading. t=0 returns a, t=1
returns an angle equivalent to b. Not clamped.
Parámetros
anumberThe angle it starts at, in radians.
bnumberThe angle it turns towards, in radians.
tnumberHow far along, 0 to 1.
Ver el código · math/angle/lerp_angle.ts:18#quatFromAxisAnglefunctionnacatamalon/math
quatFromAxisAngle(ax: number, ay: number, az: number, angle: number) => TQuat
Builds a quaternion that rotates by angle radians about the axis (ax, ay, az).
The natural way to author "spin this much around this direction": a barrel roll is
a rotation about the ship's forward axis, a turn is one about its up axis.
Parámetros
axnumberThe axis to turn about. Any length: it is made a unit first, and an axis of no length
is no turn at all.
aynumberThe axis to turn about.
aznumberThe axis to turn about.
anglenumberRotation about the axis, in radians.
Ver el código · math/quat/index.ts:71#quatFromEulerfunctionnacatamalon/math
quatFromEuler(x: number, y: number, z: number) => TQuat
Builds a quaternion from yaw/pitch/roll Euler angles (radians), using the exact
same axis order the renderer applies to Euler transforms: yaw (Y), then pitch
(X), then roll (Z). That equivalence is the point: a mesh authored with
rotation/rotationX/rotationY can be switched to a quaternion built here and
face the identical direction, so you only pay the Euler→quat conversion once, up
front, and evolve the quaternion from then on.
Parámetros
xnumberPitch, rotation about X, in radians.
ynumberYaw, rotation about Y, in radians.
znumberRoll, rotation about Z, in radians.
Ver el código · math/quat/index.ts:53#quatMultiplyfunctionnacatamalon/math
quatMultiply(a: TQuat, b: TQuat) => TQuat
Composes two rotations: the result applies b first, then a (same convention as
matrix multiplication). This is how you accumulate orientation incrementally:
orientation = multiply(deltaSpin, orientation) each frame rolls a ship a little
further without ever touching Euler angles or risking gimbal lock.
Ver el código · math/quat/index.ts:94#quatNormalizefunctionnacatamalon/math
quatNormalize(q: TQuat) => TQuat
Re-normalizes a quaternion back onto the unit sphere. Repeated multiplications
accumulate floating-point error that slowly scales the rotation; call this
periodically on a long-lived accumulated orientation to keep it a pure rotation.
Ver el código · math/quat/index.ts:120#quatRotateVec3functionnacatamalon/math
quatRotateVec3(q: TQuat, v: { x, y, z }) => { x, y, z }
Rotates a vector by a quaternion: q · v · q⁻¹, in its expanded (branch-free) form.
The scene graph's composition is expressed in position/rotation/scale rather than in
matrices (see composeWorldTransforms), and this is the one operation that needs: a child's
local offset, turned by its parent's orientation, is where the child actually sits.
Parámetros
v{ x, y, z }The vector to turn.
Ver el código · math/quat/index.ts:165#quatSlerpfunctionnacatamalon/math
quatSlerp(a: TQuat, b: TQuat, t: number) => TQuat
Spherically interpolates from a to b by t in [0, 1]: the correct way to
blend two orientations (a component-wise lerp warps angular speed and drifts off
the unit sphere). Use it to ease a camera or ship toward a target facing.
Ver el código · math/quat/index.ts:106#radToDegfunctionnacatamalon/math
radToDeg(rad: number) => number
Converts rad (radians) to degrees: handy for showing an engine angle in a debug
overlay or editor field, where degrees read more naturally than radians.
Parámetros
radnumberThe angle in radians.
Ver el código · math/angle/rad_to_deg.ts:14#TAUvariablenacatamalon/math
TAU: number
A full turn in radians (2π): the period of the trig functions. Useful for
expressing whole rotations and for wrapping angles back into range.
Ver el código · math/angle/constants.ts:9#useRandomfunctionhooknacatamalon/math
useRandom() => TRandomHandle
The random numbers of this game.
Use it instead of Math.random() whenever a result should be repeatable: give createGame a
seed and every run of the game draws exactly the same numbers in the same order, which is what
makes a level layout, a replay or a bug report reproducible. Without a seed each run is different.
seed(n) starts the sequence again from n at any moment.
Ejemplo
export const Level: TSceneFn = () => {
const random = useRandom();
for (let i = 0; i < 20; i++) {
createSprite({ key: 'tree', transform: { x: random.rand(0, 480), y: random.rand(0, 320) } });
}
return createScene();
};
Ver el código · hooks/random/use_random.ts:33#vec2Addfunctionnacatamalon/math
vec2Add(a: TVec2, b: TVec2) => TVec2
a + b, component by component: a position moved by an offset.
Parámetros
bTVec2The vector added to it.
Ver el código · math/vec2/add.ts:14#vec2Anglefunctionnacatamalon/math
vec2Angle(a: TVec2) => number
The angle a direction points at, in radians: 0 is right and it turns towards +y, which is
down on screen. It is what a sprite's transform.rotation wants, so
ship.transform.rotation = vec2Angle(velocity) turns a ship to face where it is going.
Ver el código · math/vec2/angle.ts:15#vec2Distancefunctionnacatamalon/math
vec2Distance(a: TVec2, b: TVec2) => number
How far apart two points are. With transforms, vec2Distance(player.transform, coin.transform) < 12
is a pickup check.
Ver el código · math/vec2/dist.ts:15#vec2Dotfunctionnacatamalon/math
vec2Dot(a: TVec2, b: TVec2) => number
Returns the dot product. Result is 0 if vectors are perpendicular, positive if same direction, negative if opposite.
Ver el código · math/vec2/dot.ts:14#vec2Equalsfunctionnacatamalon/math
vec2Equals(a: TVec2, b: TVec2) => boolean
Whether every component is exactly equal. Exact, so two results of arithmetic that should agree
can differ in the last decimal: compare a distance with a small number for those.
Ver el código · math/vec2/equals.ts:15#vec2Lengthfunctionnacatamalon/math
vec2Length(a: TVec2) => number
How long the vector is: the speed of a velocity, the reach of an offset.
Ver el código · math/vec2/len.ts:19#vec2Lerpfunctionnacatamalon/math
vec2Lerp(a: TVec2, b: TVec2, t: number) => TVec2
Linearly interpolates between a and b. t=0 returns a, t=1 returns b. Not clamped.
Parámetros
tnumberHow far along, 0 to 1.
Ver el código · math/vec2/lerp.ts:15#vec2Negatefunctionnacatamalon/math
vec2Negate(a: TVec2) => TVec2
The same vector pointing the opposite way.
Ver el código · math/vec2/negate.ts:13#vec2Normalizefunctionnacatamalon/math
vec2Normalize(a: TVec2) => TVec2
Returns a unit vector in the same direction. Returns (0, 0) if the input is a zero vector.
Ver el código · math/vec2/normalize.ts:15#vec2Scalefunctionnacatamalon/math
vec2Scale(a: TVec2, s: number) => TVec2
Every component multiplied by s: the same direction, s times as long.
Ver el código · math/vec2/scale.ts:14#vec2Subfunctionnacatamalon/math
vec2Sub(a: TVec2, b: TVec2) => TVec2
a - b, component by component. sub(target, from) is the arrow from from to target.
Parámetros
aTVec2The vector subtracted from.
bTVec2The vector taken away.
Ver el código · math/vec2/sub.ts:14#vec3Addfunctionnacatamalon/math
vec3Add(a: TVec3, b: TVec3) => TVec3
a + b, component by component: a position moved by an offset.
Parámetros
bTVec3The vector added to it.
Ver el código · math/vec3/add.ts:14#vec3Crossfunctionnacatamalon/math
vec3Cross(a: TVec3, b: TVec3) => TVec3
Returns a vector perpendicular to both a and b. The result magnitude equals the area of the parallelogram they form.
Ver el código · math/vec3/cross.ts:14#vec3Distancefunctionnacatamalon/math
vec3Distance(a: TVec3, b: TVec3) => number
How far apart two points are. With transforms, vec3Distance(player.transform, coin.transform) < 12
is a pickup check.
Ver el código · math/vec3/dist.ts:15#vec3Dotfunctionnacatamalon/math
vec3Dot(a: TVec3, b: TVec3) => number
Returns the dot product. Result is 0 if vectors are perpendicular, positive if same direction, negative if opposite.
Ver el código · math/vec3/dot.ts:14#vec3Equalsfunctionnacatamalon/math
vec3Equals(a: TVec3, b: TVec3) => boolean
Whether every component is exactly equal. Exact, so two results of arithmetic that should agree
can differ in the last decimal: compare a distance with a small number for those.
Ver el código · math/vec3/equals.ts:15#vec3Lengthfunctionnacatamalon/math
vec3Length(a: TVec3) => number
How long the vector is: the speed of a velocity, the reach of an offset.
Ver el código · math/vec3/len.ts:19#vec3Lerpfunctionnacatamalon/math
vec3Lerp(a: TVec3, b: TVec3, t: number) => TVec3
Linearly interpolates between a and b. t=0 returns a, t=1 returns b. Not clamped.
Parámetros
tnumberHow far along, 0 to 1.
Ver el código · math/vec3/lerp.ts:15#vec3Negatefunctionnacatamalon/math
vec3Negate(a: TVec3) => TVec3
The same vector pointing the opposite way.
Ver el código · math/vec3/negate.ts:13#vec3Normalizefunctionnacatamalon/math
vec3Normalize(a: TVec3) => TVec3
Returns a unit vector in the same direction. Returns (0, 0, 0) if the input is a zero vector.
Ver el código · math/vec3/normalize.ts:15#vec3Scalefunctionnacatamalon/math
vec3Scale(a: TVec3, s: number) => TVec3
Every component multiplied by s: the same direction, s times as long.
Ver el código · math/vec3/scale.ts:14#vec3Subfunctionnacatamalon/math
vec3Sub(a: TVec3, b: TVec3) => TVec3
a - b, component by component. sub(target, from) is the arrow from from to target.
Parámetros
aTVec3The vector subtracted from.
bTVec3The vector taken away.
Ver el código · math/vec3/sub.ts:14#wrapAnglefunctionnacatamalon/math
wrapAngle(rad: number) => number
Wraps rad into the range [-π, π). Rotations accumulated frame by frame
(transform.rotation += spin * dt) drift far outside that range over time;
wrapping keeps angle comparisons and interpolation well-behaved: call it before
lerpAngle, or whenever you need a canonical heading.
Parámetros
radnumberThe angle in radians, however many turns it has added up.
Ver el código · math/angle/wrap.ts:16#TQuattypenacatamalon/math
TQuat: [number, number, number, number]
A unit quaternion [x, y, z, w]: the engine's storage form for a full 3D
orientation. Chosen over Euler angles for anything that rotates freely in 3D (a
flying ship, a physics body): it never gimbal-locks, composes with a single
multiply, and interpolates smoothly with slerp. It is a plain number tuple on
purpose: four JSON-serializable numbers, so a TransformRecord.quaternion round-
trips through the editor/save with no special casing (a Float32Array would
stringify as {"0":...}, not an array). The matrix is only ever the derived
render form (toMat4); the quaternion is the state you keep and evolve.
Ver el código · math/quat/index.ts:18#TRandomHandletypenacatamalon/math
A per-game random handle, returned by useRandom. Every method draws from this
game's own seeded generator: two games on the same page never share a stream, and
seed only affects the game it belongs to. Seeding makes a sequence reproducible
(same seed → same numbers), which is what the editor/metadata model needs to rebuild
a procedural scene from a single saved seed.
Propiedades
randrandIntchancechooseshuffleseed
Ver el código · math/random/create_random.ts:12#TVec2typenacatamalon/math
A point or a direction in 2D: x, y.
Every vec2* function takes anything with those fields, so a transform works as it is:
vec2Distance(player.transform, coin.transform) needs no vector built first. They never change
what they are given: a vector that comes back is always a new object.
Propiedades
xnumberynumber
Ver el código · math/vec2/index.ts:12#TVec3typenacatamalon/math
A point or a direction in 3D: x, y, z.
Every vec3* function takes anything with those fields, so a 3D transform works as it is:
vec3Distance(player.transform, coin.transform) needs no vector built first. They never change
what they are given: a vector that comes back is always a new object.
Propiedades
xnumberynumberznumber
Ver el código · math/vec3/index.ts:12