Phaser AE

Vector Maths

Free packv3.0.0-32DVector

The 2D vector arithmetic every line-art game otherwise writes again: add, scale, rotate, normalise, angles and lerps, as one namespace of pure functions that never change what you pass them.

This is a Free pack. Every phaser.io account can use it in their games.

Released October 14, 2026 · updated October 20, 2026

Vector Maths: a screenshot of its demo
A note about these graphics

Expansion packs don't come with graphics. This screenshot is one way of drawing what the pack does, made for its demo: none of the sprites, tiles or backgrounds in it are in the pack.

That's by design. A pack is a game's logic, mechanics and effects, and it works with whatever art you give it. Some packs draw effects, backdrops or 3D scenes of their own, but none carries a game's sprites.

So the look is yours. Make the art on phaser.io, bring your own, or let your agent draw it in code: the same pack can look like anything.

The pack

vectorMath

function vectorMath(): Pack<{ vecMath: VecMathApi }>

The vector-math pack. Pure maths, so it needs no backend and runs headless.

Helpers

dist

function dist(a: Vec2, b: Vec2): number

Returns the distance between two points.

fromAngle

function fromAngle(rad: number, mag: number = 1): Vec2

A unit (or mag-length) vector at the given angle in radians.

normalize

function normalize(a: Vec2): Vec2

Zero-safe normalize: a zero vector returns {0,0} rather than NaN.

Functions

add

function add(a: Vec2, b: Vec2): Vec2

Returns the sum of two vectors as a new vector.

angleOf

function angleOf(a: Vec2): number

The angle of a vector in radians (-PI..PI).

clampLen

function clampLen(a: Vec2, max: number): Vec2

Clamp a vector's magnitude to at most max (terminal-velocity caps).

dist2

function dist2(a: Vec2, b: Vec2): number

Returns the squared distance between two points. Use it to compare distances without a square root.

dot

function dot(a: Vec2, b: Vec2): number

Returns the dot product of two vectors.

len

function len(a: Vec2): number

Returns the length of a vector.

len2

function len2(a: Vec2): number

Returns the squared length of a vector. Use it to compare lengths without a square root.

lerp

function lerp(a: Vec2, b: Vec2, t: number): Vec2

Linear interpolation between two points.

rotate

function rotate(a: Vec2, rad: number): Vec2

Rotate a vector by rad radians.

scale

function scale(a: Vec2, s: number): Vec2

Returns the vector a multiplied by the scalar s.

stepToward

function stepToward(from: Vec2, to: Vec2, maxStep: number): Vec2

Move from toward to by at most maxStep, never overshooting (it clamps exactly at to).

sub

function sub(a: Vec2, b: Vec2): Vec2

Returns a minus b as a new vector.

Types

Vec2

interface Vec2

A 2D point or vector. Plain data, so anything can produce one.

  • x: number — Horizontal coordinate, in whatever units the caller uses.
  • y: number — Vertical coordinate, in the same units as x.

VecMathApi

type VecMathApi = typeof kit

game.vecMath — every function this pack carries, as one namespace.

VecPt

type VecPt = Vec2

A point on a path — the same shape, named for what it is used as.