Log inGet started
World

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 }, a vector or an entity position proxy; returns a fresh array, or nil when it is not a position.

Arithmetic (all return new arrays)

  • add(a, b): Vec, sub(a, b): Vec, scale(a, k): Vec
  • dot(a, b): number, cross(a, b): Vec
  • len(a): number, dist(a, b): number, flatDist(a, b): number (ignores Y)
  • lerp(a, b, t): Vec
  • norm(a): Vec: unit vector; a near-zero vector returns { 0, 0, 1 }.
  • segmentDistance(p, a, b): (number, number): distance from p to segment a-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 along dir.
  • axisQuat(axis: Vec, angle: number): { number }: rotation of angle about a unit axis.
  • rotate(q, v: Vec): Vec: rotates v by q.
  • 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.