phaserjs/phaser/geometry-and-math

Claude Code and agent skill by phaserjs/phaser

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Phaser 4 geometry, math, random number and colour utilities: Phaser.Geom shapes (Rectangle, Circle, Ellipse, Line, Polygon, Triangle) and Geom.Intersects; Phaser.Math (Vector2, Vector3, Matrix4, Angle, Distance, Clamp, Wrap, Snap, RoundTo, Interpolation, Pow2, Hash noise, TAU, PI_OVER_2); seeded random numbers (RandomDataGenerator, Phaser.Math.RND); Phaser.Display.Color; Phaser.Structs. Use when computing positions, angles, distances, line of sight or shape overlaps, aiming at the pointer, lerping, snapping to a grid, rolling dice or loot, a procedural level from a seed, or converting and blending colours. Corrects v3 habits: Geom.Point is gone (use Vector2), Math.PI2 is gone, TAU is now 2 PI, Interpolation.BezierInterpolation is now Interpolation.Bezier. Triggers on: Vector2, Rectangle, Circle, intersects, angle between, distance, lerp, clamp, random, seed, RND, color, hex, HSV.

Install

npx vskill@latest install phaserjs/phaser/geometry-and-math

SKILL.md

The same SKILL.md was published earlier as phaserjs/phaser/geometry-and-math.

Geometry and math

The geometry classes in Phaser.Geom, the functions and vector classes in Phaser.Math, seeded random numbers, Phaser.Display.Color and Phaser.Structs.

Source: src/geom/, src/math/, src/math/random-data-generator/, src/display/color/, src/structs/. Related: graphics-and-shapes (drawing shapes), curves-and-paths, tweens (easing), v3-to-v4-migration.

Quick start

js
const { width, height } = this.scale;
const player = this.add.sprite(width / 2, height / 2, 'player');
const pointer = this.input.activePointer;
// Aim at the pointer, in radians, and move 200 px/s along that angle
const angle = Phaser.Math.Angle.Between(player.x, player.y, pointer.worldX, pointer.worldY);
const velocity = new Phaser.Math.Vector2().setToPolar(angle, 200);
const dist = Phaser.Math.Distance.Between(player.x, player.y, pointer.worldX, pointer.worldY);
const hp = Phaser.Math.Clamp(150, 0, 100); // 100
// Shapes are data, not Game Objects
const zone = new Phaser.Geom.Rectangle(100, 100, 200, 150); // x, y is the top-left
const blast = new Phaser.Geom.Circle(width / 2, height / 2, 80); // x, y is the centre
zone.contains(player.x, player.y);
Phaser.Geom.Intersects.CircleToRectangle(blast, zone);
// Repeatable random numbers
const rnd = new Phaser.Math.RandomDataGenerator([ 'level-42' ]);
const loot = rnd.pick([ 'sword', 'shield', 'potion' ]);

What changed in v4

  • Phaser.Geom.Point is gone. Use Phaser.Math.Vector2 or any { x, y }. Every getPoint, getPoints, getRandomPoint and the static GetPoint, Random and CircumferencePoint helpers return Vector2s. The old Point.* functions map to Vector2 methods (ceil, floor, invert, negate, length, setLength, project, projectUnit) and to Math.GetCentroid(points), Math.GetVec2Bounds(points) and Math.LinearXY(v1, v2, t). Full table: v3-to-v4-migration.
  • Phaser.Math.TAU is Math.PI * 2. In v3 it was PI / 2. Phaser.Math.PI2 is removed: use TAU. Phaser.Math.PI_OVER_2 is new, for PI / 2.
  • The interpolation functions have short names: Phaser.Math.Interpolation.Bezier, not BezierInterpolation.
  • Phaser.Math.Pow2 has had long names since 4.2: GetPowerOfTwo, IsSizePowerOfTwo, IsValuePowerOfTwo. The short names GetNext, IsSize and IsValue still work.
  • New: Math.Hash, Math.HashCell and Math.HashSimplex (noise), Math.GetCentroid, Math.GetVec2Bounds, Display.ColorBand and Display.ColorRamp. Removed: Math.SinCosTableGenerator, Geom.Mesh, Structs.Set (use a native Set).
  • 4.3: Display.Color.GetColor32 and color.color32 are unsigned, so a color with alpha 128 or more is no longer negative.

Geom shapes

ClassConstructorInstance methods
Rectangle(x, y, width, height), top-leftcontains, getPoint, getPoints, getRandomPoint, setTo, setPosition, setSize, setEmpty, isEmpty, getLineA to getLineD. Also centerX, centerY, left, right, top, bottom
Circle(x, y, radius), centrecontains, getPoint, getPoints, getRandomPoint, setTo, setPosition, setEmpty, isEmpty. Also diameter and the edges
Ellipse(x, y, width, height), centreas Circle, plus setSize, getMinorRadius, getMajorRadius
Triangle(x1, y1, x2, y2, x3, y3)contains, getPoint, getPoints, getRandomPoint, setTo, getLineA to getLineC
Line(x1, y1, x2, y2)getPoint, getPoints, getRandomPoint, setTo, setFromObjects, getPointA, getPointB. No contains
Polygon(points)contains, setTo, getPoints, calculateArea. Also points and area
  • getPoint(t) takes 0 to 1 around the perimeter. getPoints(quantity, stepRate?, output?) spaces points evenly.
  • Line, Polygon and Triangle have no setPosition or setEmpty. Use the static helpers instead: Phaser.Geom.Line.Offset, Phaser.Geom.Polygon.Translate, Phaser.Geom.Triangle.Offset.
  • Each class also has static helpers on its namespace, such as Phaser.Geom.Rectangle.Union(a, b), Inflate, CenterOn, FitInside, Overlaps, Phaser.Geom.Line.GetNearestPoint(line, point), Line.Angle, Line.Length, Line.Extend, Phaser.Geom.Triangle.Centroid, Phaser.Geom.Polygon.Simplify, Polygon.Smooth and Polygon.Earcut. See src/geom/<shape>/.
  • A Polygon takes 'x y x y ...', [x, y, x, y], [[x, y], ...] or [{ x, y }, ...]. Its points are plain { x, y } objects.
  • Each shape has a type, one of Phaser.Geom.CIRCLE, ELLIPSE, LINE, POINT (unused), POLYGON, RECTANGLE, TRIANGLE.
  • A shape works as a hit area with its Contains function: sprite.setInteractive(new Phaser.Geom.Circle(32, 32, 32), Phaser.Geom.Circle.Contains). The shape is in the object's local space.
  • To draw one, use Graphics, such as graphics.strokeRectShape(rect) or fillCircleShape(circle). See graphics-and-shapes.

Intersections (Phaser.Geom.Intersects)

True or false: CircleToCircle, CircleToRectangle, RectangleToRectangle, RectangleToTriangle, RectangleToValues(rect, left, right, top, bottom, tolerance?), TriangleToCircle, TriangleToLine, TriangleToTriangle, LineToCircle(line, circle, nearest?), LineToRectangle, LineToLine(line1, line2, out?), PointToLine(point, line, lineThickness = 1), PointToLineSegment(point, line).

The Get* versions return the points where the edges cross, as Vector2[], or an empty array: GetCircleToCircle, GetCircleToRectangle, GetLineToCircle, GetLineToRectangle, GetRectangleToRectangle, GetRectangleToTriangle, GetTriangleToCircle, GetTriangleToLine, GetTriangleToTriangle. Each takes an optional out array: reuse one in a hot loop.

These three are different:

  • GetLineToLine(line1, line2, isRay = false, out?) returns one Vector3, where z is how far along line1 the hit is, as a fraction of its length, or null.
  • GetLineToPoints(line, points, isRay, out?) (the points form a closed loop) returns the closest hit as a Vector3, and GetLineToPolygon(line, polygons, isRay, out?) as a Vector4 whose w is the index of the polygon. Both return null for no hit.
  • GetRaysFromPointToPolygon(x, y, polygons) casts a ray at every polygon vertex and returns the hits as Vector4[] sorted by angle, for line of sight or 2D shadows.

GetRectangleIntersection(rectA, rectB, out?) returns the overlapping Rectangle.

Phaser.Math

Constants: TAU, PI_OVER_2, EPSILON (1e-6), DEG_TO_RAD, RAD_TO_DEG, MIN_SAFE_INTEGER, MAX_SAFE_INTEGER, and RND.

AreaFunctions
RangeClamp(v, min, max), Wrap(v, min, max) (result in [min, max)), Within(a, b, tolerance), Percent(v, min, max, upperMax?) (0 to 1), FromPercent(p, min, max), MaxAdd, MinSub, Difference
Lerp and easeLinear(p0, p1, t), LinearXY(v1, v2, t), SmoothStep(x, min, max), SmootherStep(x, min, max), Bernstein, CatmullRom, Phaser.Math.Easing.Sine.InOut and the others (see tweens)
InterpolationPhaser.Math.Interpolation.Linear(values, k), Bezier(values, k), CatmullRom(values, k), CubicBezier(t, p0, p1, p2, p3), QuadraticBezier(t, p0, p1, p2), SmoothStep(t, min, max), SmootherStep(t, min, max)
RoundingRoundTo(v, place = 0, base = 10), CeilTo, FloorTo, RoundAwayFromZero, Snap.To(v, gap, start?, divide?), Snap.Floor, Snap.Ceil, Fuzzy.Equal(a, b, epsilon = 0.0001), Fuzzy.LessThan, GreaterThan, Ceil, Floor
Angles (radians)Angle.Between(x1, y1, x2, y2), Angle.BetweenPoints(a, b), Angle.Wrap (to -PI..PI), Angle.Normalize (to 0..2 PI), Angle.RotateTo(current, target, step = 0.05), Angle.GetShortestDistance(a, b), Angle.Reverse, Angle.Random. Degrees: Angle.WrapDegrees, Angle.ShortestBetween, Angle.RandomDegrees. DegToRad, RadToDeg
DistanceDistance.Between(x1, y1, x2, y2), BetweenPoints(a, b), Squared, BetweenPointsSquared, Snake (Manhattan), Chebyshev, Power(x1, y1, x2, y2, pow = 2)
PointsRotate(point, angle), RotateAround(point, x, y, angle), RotateAroundDistance(point, x, y, angle, distance), RotateTo(point, x, y, angle, distance), TransformXY, ToXY(index, width, height) (grid index to x/y), GetCentroid, GetVec2Bounds, RandomXY(vector, scale)
OtherBetween(min, max) (random integer, inclusive), FloatBetween(min, max), Average, Median, Factorial, IsEven, IsEvenStrict, GetSpeed(distance, seconds) (px per ms), Pow2.*
  • Phaser.Math.SmoothStep(x, min, max) returns 0 to 1: where x sits between the two edges. Phaser.Math.Interpolation.SmoothStep(t, min, max) returns a value between min and max. Same arguments, opposite jobs.
  • RoundTo(123.456, -2) is 123.46. A positive place rounds to tens, hundreds and so on: RoundTo(123.456, 2) is 100.
  • Angle.Between returns -PI to PI, but vector.angle() returns 0 to 2 PI.
  • Snap.To(12, 5) is 10, Snap.Ceil(11, 5) is 15. Pass true as the fourth argument to get the cell index rather than the position.

Vectors and matrices

Vector2, Vector3, Vector4, Matrix3, Matrix4, Quaternion and Euler are in src/math/. Their methods work as in v3, mutate the vector and return this, so clone() first to keep the original: const dir = target.clone().subtract(origin).normalize();.

  • new Vector2(5) is (5, 5). new Vector2({ x, y }) copies an object.
  • invert() swaps x and y. negate() flips the signs.
  • project(v) changes this vector. projectUnit(unitVec, out?) returns a new Vector2 and leaves this one alone.
  • setToPolar(angle, length = 1), setAngle, rotate, limit(max), setLength, lerp(v, t), reflect(normal), mirror(axis), fuzzyEquals(v, epsilon), ceil, floor, transformMat3, transformMat4.
  • Vector2.ZERO, ONE, RIGHT, LEFT, UP (0, -1) and DOWN are shared: never change them.
  • Matrix4.val is a column-major Float32Array(16).

Random numbers

Phaser.Math.RND is a RandomDataGenerator that the Game creates from the seed game config. It is null until new Phaser.Game() runs. Make your own for each thing that must repeat, such as one per level:

js
const level = 3;
const drops = [ 'gem', 'coin', 'key' ];
const rnd = new Phaser.Math.RandomDataGenerator([ 'level-' + level ]);
rnd.between(1, 6); // integer, inclusive. integerInRange is the same
rnd.realInRange(0.5, 1.5);
rnd.frac(); // 0 to 1
rnd.pick(drops); // any element
rnd.weightedPick(drops); // favours the START of the array
rnd.shuffle(drops); // in place
rnd.sign(); // -1 or 1
rnd.angle(); // integer DEGREES, -180 to 180
rnd.rotation(); // radians, -PI to PI
rnd.normal(); // uniform -1 to 1, not a bell curve
rnd.uuid();
const saved = rnd.state(); // a string starting '!rnd': store it
rnd.state(saved); // later: carry on the same sequence
rnd.sow([ 'new-seed' ]); // restart from a new seed
  • A seed is an array, or a single string or number, in the constructor and the seed game config. 'level-3' is the same as [ 'level-3' ]. Before 4.3 a single seed was ignored, and every one gave the same sequence, so use an array for code which must run on 4.2.
  • Only a RandomDataGenerator is seeded. Math.Between, FloatBetween, Angle.Random, RandomXY, every Geom getRandomPoint and Random, and Utils.Array.GetRandom and Shuffle use Math.random.
Hash noise (v4)

For random-looking values which are always the same for the same input, such as placing trees across a world:

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
39,633
Repository forks
7,130
7-day trend score
53

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 17 May 2026, 6 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Grok Build (Cursor)
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/phaserjs/phaser/geometry-and-math/badge)](https://verified-skill.com/skills/phaserjs/phaser/geometry-and-math)

Something wrong with this skill or its page? Tell us.