Phaser 4 — Geometry and Math
Geom classes (Circle, Ellipse, Line, Polygon, Rectangle, Triangle), intersection tests, and Math utilities (Vector2, Vector3, Matrix4, angles, distances, interpolation, easing, random, snap, clamp).
Related skills: graphics-and-shapes.md, physics-arcade.md
Quick Start
Core Concepts
Geometry objects are pure data containers holding coordinates and dimensions. They are NOT game objects and cannot be added to the display list. To render geometry, use the Graphics game object or Shape game objects (see graphics-and-shapes.md).
Every geom class has a type property set to a Phaser.Geom constant for fast type checks.
All geom classes share a common instance method pattern:
contains(x, y)-- point-in-shape testgetPoint(position, output?)-- point at normalized position (0-1) on perimetergetPoints(quantity, stepRate?, output?)-- evenly spaced points on perimetergetRandomPoint(output?)-- random point inside the shapesetTo(...)-- reset all propertiessetEmpty()-- zero out the shapesetPosition(x, y)-- move origin/center
Each geom type also has a folder of static helper functions (e.g., Phaser.Geom.Rectangle.Contains, Phaser.Geom.Circle.Area).
Geometry Classes
Rectangle Static Helpers
Phaser.Geom.Rectangle.*: Area, Ceil, CeilAll, CenterOn, Clone, Contains, ContainsPoint, ContainsRect, CopyFrom, Decompose, Equals, FitInside, FitOutside, Floor, FloorAll, FromPoints, FromXY, GetAspectRatio, GetCenter, GetPoint, GetPoints, GetSize, Inflate, Intersection, MarchingAnts, MergePoints, MergeRect, MergeXY, Offset, OffsetPoint, Overlaps, Perimeter, PerimeterPoint, Random, RandomOutside, SameDimensions, Scale, Union.
Circle Static Helpers
Phaser.Geom.Circle.*: Area, Circumference, CircumferencePoint, Clone, Contains, ContainsPoint, ContainsRect, CopyFrom, Equals, GetBounds, GetPoint, GetPoints, Offset, OffsetPoint, Random.
Line Static Helpers
Phaser.Geom.Line.*: Angle, BresenhamPoints, CenterOn, Clone, CopyFrom, Equals, Extend, GetEasedPoints, GetMidPoint, GetNearestPoint, GetNormal, GetPoint, GetPoints, GetShortestDistance, Height, Length, NormalAngle, NormalX, NormalY, Offset, PerpSlope, Random, ReflectAngle, Rotate, RotateAroundPoint, RotateAroundXY, SetToAngle, Slope, Width.
Triangle Static Helpers
Phaser.Geom.Triangle.*: Area, BuildEquilateral, BuildFromPolygon, BuildRight, CenterOn, Centroid, CircumCenter, CircumCircle, Clone, Contains, ContainsArray, ContainsPoint, CopyFrom, Decompose, Equals, GetPoint, GetPoints, InCenter, Offset, Perimeter, Random, Rotate, RotateAroundPoint, RotateAroundXY.
Polygon Input Formats
The Polygon constructor accepts multiple formats for the points argument:
- Space-separated string:
'40 0 40 20 100 20 100 80' - Array of
{x, y}objects - Flat number array:
[x1, y1, x2, y2, ...] - Array of
[x, y]sub-arrays
Intersection Tests
All under Phaser.Geom.Intersects.
Boolean Tests (return true/false)
Get Intersection Points (return Vector2[])
Math Functions by Category
All under Phaser.Math unless noted.
Constants
Angle Functions (Phaser.Math.Angle.*)
Distance Functions (Phaser.Math.Distance.*)
Interpolation (Phaser.Math.Interpolation.*)
Snap Functions (Phaser.Math.Snap.*)
Fuzzy Comparison (Phaser.Math.Fuzzy.*)
Easing Functions (Phaser.Math.Easing.*)
Each type has .In, .Out, .InOut variants. Used primarily by tweens (pass string names like 'Sine.easeOut').
Power aliases: Power0 = Linear, Power1 = Quad.Out, Power2 = Cubic.Out, Power3 = Quart.Out, Power4 = Quint.Out.
Short names also work: 'Quad' = Quad.Out, 'Sine' = Sine.Out, etc.
Core Math Helpers (directly on Phaser.Math)
Power-of-Two (Phaser.Math.Pow2.*)
Vector2 Quick Reference
Phaser.Math.Vector2 -- 2D vector used throughout Phaser for positions, velocities, directions.
Constructor: new Vector2(x?, y?) or new Vector2({x, y}). If only x given, y defaults to x.
Static: Vector2.ZERO, Vector2.RIGHT, Vector2.LEFT, Vector2.UP, Vector2.DOWN, Vector2.ONE.
Vector3 Quick Reference
Phaser.Math.Vector3 -- 3D vector for camera projections, lighting, 3D math.
Constructor: new Vector3(x?, y?, z?) or new Vector3({x, y, z}).
Key methods (same patterns as Vector2 plus): up(), min(v), max(v), addVectors(a, b), subVectors(a, b), crossVectors(a, b), cross(v), addScalar(s), addScale(v, scale), fromArray(array, offset?), setFromMatrixPosition(mat4), setFromMatrixColumn(mat4, index), applyMatrix3(mat), applyMatrix4(mat), transformCoordinates(mat), transformQuat(q), project(mat), projectViewMatrix(view, proj), unproject(viewport, invProjView).
Matrix4 Quick Reference
Phaser.Math.Matrix4 -- 4x4 matrix for 3D transforms, projection, and view matrices. Backed by Float32Array(16).
Constructor: new Matrix4(m?) -- copies from existing Matrix4, or defaults to identity.
Key methods: clone(), set(src), copy(src), identity(), transpose(), invert(), adjoint(), determinant(), multiply(src), multiplyLocal(src), translate(v), scale(v), rotate(angle, axis), rotateX(angle), rotateY(angle), rotateZ(angle), fromRotationTranslation(q, v), fromQuat(q), frustum(...), perspective(fovy, aspect, near, far), perspectiveLH(width, height, near, far), ortho(left, right, bottom, top, near, far), lookAt(eye, center, up), lookAtRH(eye, target, up), setWorldMatrix(rotation, position, scale, viewMatrix?, projectionMatrix?).
Also: Phaser.Math.Matrix3 -- 3x3 matrix for 2D transforms and normal matrices.
RandomDataGenerator
Phaser.Math.RandomDataGenerator -- seeded PRNG. Global instance at Phaser.Math.RND (seeded via game config seed property).
Create reproducible sequences by providing seeds: new RandomDataGenerator(['level-42']).
Color Utilities
Color Class (Phaser.Display.Color)
A mutable RGBA color representation with automatic conversion to WebGL floats, HSV, CSS strings, and packed integers.
Color Manipulation
Color Interpolation (Phaser.Display.Color.Interpolate)
Interpolate between colors over a given length. Returns { r, g, b, a, color }.
Gotchas
-
Geom objects are not game objects. They have no
scene, nosetPositionfrom the display list, no texture. Use Graphics or Shape game objects to render them. -
Point class removed in v4. Use
Phaser.Math.Vector2or plain{x, y}objects instead. TheGEOM_CONST.POINT(3) exists but the Point class does not. -
Angles are in radians throughout the math API. Use
Phaser.Math.DegToRad()andRadToDeg()for conversion. ThePhaser.Math.TAUconstant (2 * PI) is new in v4. -
Vector2 methods mutate in place and return
thisfor chaining. Call.clone()first if you need to preserve the original:const result = v.clone().add(other). -
Squared distance is faster than Euclidean distance (avoids
Math.sqrt). UseDistance.Squaredorvec.distanceSq()for comparisons where the actual distance value is not needed. -
Intersection Get functions allocate arrays.* Pass a reusable
outarray to avoid garbage collection pressure in hot loops. -
EaseMap short names default to the
.Outvariant.'Quad'meansQuadratic.Out, notQuadratic.In. Use full string keys like'Quad.easeIn'for other variants. -
RandomDataGenerator state is serializable. Call
rnd.state()to get a string you can store, andrnd.state(savedString)to restore it for deterministic replay. -
Polygon.contains uses ray-casting (even/odd rule). Complex self-intersecting polygons may give unexpected results.
-
Matrix4.val is a Float32Array. Access elements directly via
mat.val[index]using column-major order (OpenGL convention).


