Phaser AE

Game maths

Free packv3.0.0-3Logic

The maths a game uses every frame: easing curves, vectors, angles, ranges, smoothing and hit tests, as plain functions on `game.maths`. It saves writing a dot product, a shortest-arc turn or a point-in-polygon test by hand, and getting the edge cases wrong.

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

Released October 14, 2026 · updated October 20, 2026

Game 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.

What it does

Every function takes plain numbers or { x, y } objects and returns a new value. Nothing touches the game's objects, its clock or its renderer, so the functions are safe to call anywhere, any number of times, from any step or test. It has no dimension of its own, though its vectors and geometry are 2D.

The easing table has ten families (quad, cubic, quart, quint, sine, expo, circ, back, elastic and bounce), each in in, out and inOut forms, plus linear and swing, short aliases such as 'elastic', and bezier for a CSS-style cubic bezier. ease() resolves a name, a function or a bezier tuple, and throws on an unknown name so a typo shows at once. The tween pack takes its easings from here, so a name that works in one works in the other.

The rest covers the small jobs that come up constantly. turnToward turns a turret along the shortest arc at a limited rate. approach smooths a camera zoom the same way at any frame rate. mapRange turns hit points into a bar width. geom.segmentHitsCircle checks a shot against an enemy, geom.lineOfSight checks a view past circular cover, and geom.predictPath gives the points of a ballistic arc for an aiming preview. hashCode turns a level code into a seed.

Features

  • Thirty-two named easings — ten families in three shapes each, plus linear and swing, short aliases and cubic bezier curves.
  • 2D vectors — add, subtract, scale, dot, cross, length, distance, normalise, clamp length, polar conversion, rotate, lerp, step toward, perpendicular and reflect.
  • Angles — radians throughout, wrapping, the shortest signed turn and a rate-limited turn toward.
  • Scalars and ranges — lerp, clamp, range mapping, rounding, smoothstep and exponential approach.
  • Hit tests — points in rectangles, circles and polygons, segment intersection, a segment against a circle and line of sight past circles.
  • Shape helpers — polygon area and centroid, convex hull, closest point on a segment and a ballistic path preview.
  • A seed hash — FNV-1a from a string to a 32-bit number, for seeding from a level code.

Good for

  • Any game that tweens, where one set of easing names serves code and tweens alike.
  • Shooters and top-down action, with turrets that turn at a set rate and shots tested against enemies.
  • Artillery and throwing games that preview a ballistic arc before the shot.
  • Smooth cameras and interface motion that behave the same at 30 or 144 frames a second.
  • Seeded levels and daily challenges made from a level code.

Make it yours

There is nothing to configure: it is a library of functions. It arrives with core-2d, core-3d and core-vector, so most games already have it. Two different uses: a twin-stick shooter that normalises the stick with vec.normalize, turns its turret with turnToward and tests bullets with segmentHitsCircle; and a puzzle game's interface that uses nothing but ease('easeOutBack') for pops and approach for smooth scrolling.

A quick look

A few everyday calls:

const { ease, vec, geom, approach, turnToward, hashCode } = game.maths;
ease('easeOutBack')(0.5);
vec.normalize({ x: 3, y: 4 });
h.rot = turnToward(h.rot, vec.angleOf(vec.sub(player, h)), 3 * dt);
cam.zoom = approach(cam.zoom, wanted, 6, dt);
if (geom.segmentHitsCircle(muzzle, muzzleEnd, enemy, enemy.width / 2)) hit(enemy);
hashCode('level-3');

Works with

  • tween — requires this pack and shares its easing table.
  • core-2d, core-3d and core-vector — include it.
  • geometry — for whole shapes and curves as data: points along, random inside and overlaps.
  • vector-math — for the maths of vector-line games.

Good to know

  • Vectors and geometry are 2D only.
  • vec returns a fresh object on every call; in a hot loop over thousands of objects, write the arithmetic inline.
  • pointInPolygon is undefined exactly on the boundary.
  • hashCode is a 32-bit hash, so collisions are possible, and it is not cryptographic.

More Logic packs

Show all →