Loading...
Loading...
Use this skill when working with curves and paths in Phaser 4. Covers splines, bezier curves, lines, ellipses, path followers, and mathematical curve types. Triggers on: curve, path, spline, bezier, path follower.
npx skill4agent add phaserjs/phaser curves-and-pathsCreating paths from curves, getting points along them, drawing them with Graphics, and making sprites follow paths automatically using PathFollower in Phaser 4.
src/curves/src/curves/path/src/gameobjects/pathfollower/src/gameobjects/components/PathFollower.js// In a Scene's create() method:
// 1. Create a Path starting at (50, 300)
const path = this.add.path(50, 300);
// 2. Add curves to the path
path.lineTo(200, 100);
path.splineTo([ new Phaser.Math.Vector2(300, 400), new Phaser.Math.Vector2(500, 200) ]);
path.lineTo(700, 300);
// 3. Draw the path using Graphics
const graphics = this.add.graphics();
graphics.lineStyle(2, 0xffffff, 1);
path.draw(graphics, 64);
// 4. Create a PathFollower sprite that moves along the path
const follower = this.add.follower(path, 50, 300, 'ship');
follower.startFollow({
duration: 5000,
rotateToPath: true,
repeat: -1,
yoyo: true
});Phaser.Curves.Paththis.add.path(x, y)curvesPhaser.Curves.CurvestartPointVector2autoClosegetPoints()defaultDivisionsgetPoints()namePhaser.Curves.CurvegetPoint(t, out)getPointAt(u, out)getPoints(divisions, stepRate, out)getSpacedPoints(divisions, stepRate, out)getDistancePoints(distance)getLength()getBounds(out, accuracy)getTangent(t, out)getTangentAt(u, out)getStartPoint(out)getEndPoint(out)getRandomPoint(out)draw(graphics, pointsTotal)activePhaser.GameObjects.PathFollowerComponents.PathFollowerthis.add.follower(path, x, y, texture, frame)pathPhaser.Curves.PathpathTweenpathOffsetVector2pathVectorVector2pathDeltaVector2rotateToPathpathRotationOffsetconst path = this.add.path(100, 500);
path.lineTo(300, 100); // straight line
path.cubicBezierTo(500, 100, 350, 50, 450, 50); // cubic bezier (endX, endY, cp1X, cp1Y, cp2X, cp2Y)
path.quadraticBezierTo(700, 400, 600, 100); // quadratic bezier (endX, endY, cpX, cpY)
path.splineTo([ // spline through points
new Phaser.Math.Vector2(750, 300),
new Phaser.Math.Vector2(600, 500)
]);
path.ellipseTo(50, 80, 0, 270, false, 0); // ellipse arc (xRadius, yRadius, startAngle, endAngle, clockwise, rotation)
path.circleTo(40); // shortcut for ellipseTo with equal radii and 0-360
// Jump to a new position without drawing (creates a gap)
path.moveTo(400, 400);
path.lineTo(500, 400);
// Close the path by connecting end to start
path.closePath();const path = new Phaser.Curves.Path(0, 0);
// Add pre-constructed curve objects
const line = new Phaser.Curves.Line(new Phaser.Math.Vector2(0, 0), new Phaser.Math.Vector2(200, 200));
path.add(line);
const spline = new Phaser.Curves.Spline([ 200, 200, 300, 100, 400, 300 ]);
path.add(spline);
const ellipse = new Phaser.Curves.Ellipse(400, 300, 100, 60, 0, 360, false, 0);
path.add(ellipse);// Array of points (uses defaultDivisions per curve)
const points = path.getPoints();
// With explicit divisions per curve
const detailed = path.getPoints(32);
// Equally spaced points along the entire path
const spaced = path.getSpacedPoints(100);
// Single point at normalized position (0-1)
const midpoint = path.getPoint(0.5);
// Tangent vector at a position
const tangent = path.getTangent(0.5);
// Total path length in pixels
const length = path.getLength();
// Bounding rectangle
const bounds = path.getBounds();const graphics = this.add.graphics();
// Draw entire path
graphics.lineStyle(2, 0x00ff00, 1);
path.draw(graphics, 64); // 64 = points per curve for smoothness
// Draw individual curves
graphics.lineStyle(1, 0xff0000, 1);
path.curves[0].draw(graphics, 32);
// Draw debug points
const points = path.getSpacedPoints(50);
points.forEach(p => {
graphics.fillStyle(0xffff00, 1);
graphics.fillCircle(p.x, p.y, 3);
});const path = this.add.path(100, 200);
path.lineTo(400, 400);
path.lineTo(700, 200);
// Create follower
const enemy = this.add.follower(path, 100, 200, 'enemy');
// Start following with config
enemy.startFollow({
duration: 3000, // ms to traverse path
positionOnPath: true, // snap to path start position
rotateToPath: true, // auto-rotate to face direction
rotationOffset: 90, // offset added to auto-rotation (degrees)
repeat: -1, // -1 = infinite repeat
yoyo: true, // reverse on each repeat
from: 0, // start position on path (0-1)
to: 1, // end position on path (0-1)
startAt: 0, // initial seek position
ease: 'Sine.easeInOut' // any valid Phaser ease
});
// Control during playback
enemy.pauseFollow();
enemy.resumeFollow();
enemy.stopFollow();
enemy.isFollowing(); // returns boolean
// Change path at runtime
enemy.setPath(newPath);
enemy.setPath(newPath, { duration: 2000 }); // auto-starts
// Set rotation independently
enemy.setRotateToPath(true, 90); // (value, offsetDegrees)// Shorthand: pass just a duration number
enemy.startFollow(5000);
// Equivalent to:
enemy.startFollow({ duration: 5000 });| Curve | Class | Constructor Params | Description |
|---|---|---|---|
| Line | | | Straight line segment between two points |
| Spline | | | Catmull-Rom spline through control points |
| CubicBezier | | | Cubic Bezier with start, 2 control points, end |
| QuadraticBezier | | | Quadratic Bezier with start, 1 control point, end |
| Ellipse | | | Elliptical arc; angles in degrees; yRadius defaults to xRadius |
xyxRadiusyRadiusstartAngleendAngleclockwiserotationanglerotationsetWidth(value)setHeight(value)Phaser.Curves.Path| API | Type | Description |
|---|---|---|
| method | Append any Curve to the path |
| method | Add a Line from current end point |
| method | Add a Spline from current end point |
| method | Add CubicBezier from current end point |
| method | Add QuadraticBezier from current end point |
| method | Add Ellipse arc from current end point |
| method | Shortcut for ellipseTo with equal radii |
| method | Move end point without drawing (creates gap) |
| method | Add Line from end to start if not already closed |
| method | Point at normalized position (0-1) on entire path |
| method | Array of points, divisions per curve |
| method | Equidistant points along entire path |
| method | Random point anywhere on the path |
| method | Path starting point |
| method | Path ending point |
| method | Unit tangent vector at position t |
| method | Return the Curve at normalized position t |
| method | Total path length in pixels |
| method | Array of cumulative curve lengths |
| method | Bounding Rectangle |
| method | Draw all curves onto a Graphics object |
| method | Serialization |
| method | Force recalculation of cached lengths |
| method | Clear internal references |
Phaser.Curves.Curve| API | Type | Description |
|---|---|---|
| method | Point at parameter t (0-1) -- abstract, each subclass implements |
| method | Point at arc-length position u (0-1) -- evenly spaced |
| method | Array of points |
| method | Equidistant points by arc length |
| method | Points spaced by pixel distance |
| method | Total curve arc length |
| method | Unit tangent vector |
| method | Convert pixel distance to t value |
| method | Render onto Graphics (default 32 points) |
| method | Bounding Rectangle |
| | When false, parent Path skips this curve |
| | Default 5 for standalone curves |
| | Precision for arc length calculations (default 100) |
| API | Type | Description |
|---|---|---|
| method | Set a new Path (optionally auto-start) |
| method | Begin following; config = duration number or PathConfig |
| method | Pause movement |
| method | Resume paused movement |
| method | Stop following |
| method | Returns true if actively moving on path |
| method | Enable/disable auto-rotation with offset |
| | Current path reference |
| | Internal tween driving movement |
| | Offset from path coordinates |
| | Current position on the path |
| | Movement delta since last update |
| | Auto-rotate to path direction |
| | Rotation offset in degrees |
Phaser.Types.GameObjects.PathFollower.PathConfig| Property | Type | Default | Description |
|---|---|---|---|
| | 1000 | Time in ms to traverse the path |
| | 0 | Start position on path (0-1) |
| | 1 | End position on path (0-1) |
| | false | Snap follower to path start on begin |
| | false | Auto-rotate to face path direction |
| | 0 | Degrees added to auto-rotation |
| | 0 | Initial seek position on path (0-1) |
easerepeatyoyodelayholdonCompletegetPoint(t)getPointAt(u)getPointgetPointAtgetPointmoveToMoveToactive: falsegetPoints()draw()startFollowscene.tweens.addCounter()persist: truepositionOnPath: falsepositionOnPath: truestartAngleendAnglerotationangleclosePathautoCloseclosePath()autoClose = truegetPoints()getSpacedPoints()getCurveLengths()path.updateArcLengths()cubicBezierTocubicBezierTo(endX, endY, cp1X, cp1Y, cp2X, cp2Y)cubicBezierTo(cp1, cp2, endPoint)arcLengthDivisions| File | Purpose |
|---|---|
| Path class -- combines multiple curves, factory registered as |
| MoveTo pseudo-curve for creating gaps in paths |
| Base Curve class -- shared methods for all curve types |
| Line curve (two-point segment) |
| Spline curve (Catmull-Rom through multiple points) |
| Cubic Bezier curve (4 control points) |
| Quadratic Bezier curve (3 control points) |
| Ellipse/arc curve with angle and rotation support |
| PathFollower Game Object (extends Sprite + PathFollower mixin) |
| |
| PathFollower component mixin (setPath, startFollow, pathUpdate, etc.) |
| PathConfig typedef |