phaserjs/phaser/geometry-and-math
Claude Code and agent skill by phaserjs/phaser
- community
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-mathSKILL.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
What changed in v4
Phaser.Geom.Pointis gone. UsePhaser.Math.Vector2or any{ x, y }. EverygetPoint,getPoints,getRandomPointand the staticGetPoint,RandomandCircumferencePointhelpers return Vector2s. The oldPoint.*functions map to Vector2 methods (ceil,floor,invert,negate,length,setLength,project,projectUnit) and toMath.GetCentroid(points),Math.GetVec2Bounds(points)andMath.LinearXY(v1, v2, t). Full table: v3-to-v4-migration.Phaser.Math.TAUisMath.PI * 2. In v3 it wasPI / 2.Phaser.Math.PI2is removed: useTAU.Phaser.Math.PI_OVER_2is new, forPI / 2.- The interpolation functions have short names:
Phaser.Math.Interpolation.Bezier, notBezierInterpolation. Phaser.Math.Pow2has had long names since 4.2:GetPowerOfTwo,IsSizePowerOfTwo,IsValuePowerOfTwo. The short namesGetNext,IsSizeandIsValuestill work.- New:
Math.Hash,Math.HashCellandMath.HashSimplex(noise),Math.GetCentroid,Math.GetVec2Bounds,Display.ColorBandandDisplay.ColorRamp. Removed:Math.SinCosTableGenerator,Geom.Mesh,Structs.Set(use a nativeSet). - 4.3:
Display.Color.GetColor32andcolor.color32are unsigned, so a color with alpha 128 or more is no longer negative.
Geom shapes
| Class | Constructor | Instance methods | |
|---|---|---|---|
Rectangle | (x, y, width, height), top-left | contains, getPoint, getPoints, getRandomPoint, setTo, setPosition, setSize, setEmpty, isEmpty, getLineA to getLineD. Also centerX, centerY, left, right, top, bottom | |
Circle | (x, y, radius), centre | contains, getPoint, getPoints, getRandomPoint, setTo, setPosition, setEmpty, isEmpty. Also diameter and the edges | |
Ellipse | (x, y, width, height), centre | as 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,PolygonandTrianglehave nosetPositionorsetEmpty. 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.SmoothandPolygon.Earcut. Seesrc/geom/<shape>/. - A Polygon takes
'x y x y ...',[x, y, x, y],[[x, y], ...]or[{ x, y }, ...]. Itspointsare plain{ x, y }objects. - Each shape has a
type, one ofPhaser.Geom.CIRCLE,ELLIPSE,LINE,POINT(unused),POLYGON,RECTANGLE,TRIANGLE. - A shape works as a hit area with its
Containsfunction: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)orfillCircleShape(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 oneVector3, wherezis how far alongline1the hit is, as a fraction of its length, ornull.GetLineToPoints(line, points, isRay, out?)(the points form a closed loop) returns the closest hit as aVector3, andGetLineToPolygon(line, polygons, isRay, out?)as aVector4whosewis the index of the polygon. Both returnnullfor no hit.GetRaysFromPointToPolygon(x, y, polygons)casts a ray at every polygon vertex and returns the hits asVector4[]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.
| Area | Functions | |
|---|---|---|
| Range | Clamp(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 ease | Linear(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) | |
| Interpolation | Phaser.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) | |
| Rounding | RoundTo(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 | |
| Distance | Distance.Between(x1, y1, x2, y2), BetweenPoints(a, b), Squared, BetweenPointsSquared, Snake (Manhattan), Chebyshev, Power(x1, y1, x2, y2, pow = 2) | |
| Points | Rotate(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) | |
| Other | Between(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: wherexsits between the two edges.Phaser.Math.Interpolation.SmoothStep(t, min, max)returns a value betweenminandmax. Same arguments, opposite jobs.RoundTo(123.456, -2)is123.46. A positiveplacerounds to tens, hundreds and so on:RoundTo(123.456, 2)is100.Angle.Betweenreturns -PI to PI, butvector.angle()returns 0 to 2 PI.Snap.To(12, 5)is 10,Snap.Ceil(11, 5)is 15. Passtrueas 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()swapsxandy.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) andDOWNare shared: never change them.Matrix4.valis a column-majorFloat32Array(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:
- A seed is an array, or a single string or number, in the constructor and the
seedgame 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 GeomgetRandomPointandRandom, andUtils.Array.GetRandomandShuffleuseMath.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
- SKILL.md in the repositoryCheckingskills/geometry-and-math/SKILL.md
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
[](https://verified-skill.com/skills/phaserjs/phaser/geometry-and-math)