Bastion Works: Towers, Turrets and Targeting Kit
README.md
projectileKit.vec
Small vector and quaternion maths on plain { x, y, z } arrays ({ [1], [2], [3] }).
Every function is pure (no engine calls), so every peer computes the same numbers and
the tests can check them directly.
Used by projectileKit.motion, projectileKit.render, projectileKit.api and by
turretsKit's aiming and targeting code. Require it as ~.vec from inside the package.
Type
export type Vec = { number }: a three-element array{ x, y, z }.
Functions
Reading positions
of(v: any): Vec?: reads a position from an array, a keyed table{ x, y, z }, avectoror an entity position proxy; returns a fresh array, ornilwhen it is not a position.
Arithmetic (all return new arrays)
add(a, b): Vec,sub(a, b): Vec,scale(a, k): Vecdot(a, b): number,cross(a, b): Veclen(a): number,dist(a, b): number,flatDist(a, b): number(ignores Y)lerp(a, b, t): Vecnorm(a): Vec: unit vector; a near-zero vector returns{ 0, 0, 1 }.segmentDistance(p, a, b): (number, number): distance frompto segmenta-b, and the segment parameter (0..1) of the closest point.
Angles and quaternions (quaternions are { x, y, z, w }, yaw 0 faces +Z, positive pitch is up)
yawPitchQuat(yaw, pitch): { number }: yaw about +Y, then pitch tilting local +Z upward.lookQuat(dir: Vec): { number }: rotation turning local +Z to point alongdir.axisQuat(axis: Vec, angle: number): { number }: rotation ofangleabout a unit axis.rotate(q, v: Vec): Vec: rotatesvbyq.yawPitchOf(dir: Vec): (number, number): yaw and pitch of a direction, in radians.dirOf(yaw, pitch): Vec: unit direction for a yaw and an upward pitch.wrap(a): number: wraps an angle to (-pi, pi].
Determinism
rng(seed: number): () -> number: a seeded random stream returning numbers in [0, 1); the same seed gives the same sequence on every peer.