LittleJS 1.28.0

PathFinding

PathFinder

Grid pathfinder using A* with two optional smoothing passes

Constructor

new PathFinder(source)pathFinder.js:91

Make a path finder over a tile layer or a grid

source TileCollisionLayer|Vector2

Either a TileCollisionLayer (size and walkability auto-derived) or a Vector2 grid size (user overrides isWalkable).

// Tile-layer driven (most common):
const pf = new PathFinder(myTileCollisionLayer);
const path = pf.findPath(player.pos, mousePos);

// Bare grid with custom walkability:
const gridFinder = new PathFinder(vec2(50, 50));
gridFinder.isWalkable = (x, y) => myGrid[y*50 + x] === 0;

Members

Methods

Members

debug : booleanpathFinder.js:131

If true, draw debug visualization during findPath

debugTime : numberpathFinder.js:133

Debug primitive lifetime in seconds (0 disables drawing)

heuristicWeight : numberpathFinder.js:120

A* heuristic multiplier (1 = admissible, higher = greedier)

maxLoop : number|undefinedpathFinder.js:124

Most A* expansions before giving up, undefined for the number of cells, so a search always finishes; a lower one caps the time a search takes, see searchGaveUp

Flat row-major array of size.x*size.y nodes

searchGaveUp : booleanpathFinder.js:127

True when the last search stopped at maxLoop with no path, so it gave up rather than that there is no way through

Grid dimensions in tiles

smoothPath : booleanpathFinder.js:129

If true, post-process paths with two-pass smoothing

Tile layer driving walkability, if any

Methods

buildNodeData()pathFinder.js:214

Reset all nodes and re-populate walkable / cost / posWorld from the current isWalkable / getCost overrides. Called at the start of findPath; call it directly before searches made with rebuild=false.

findPath(startPos, endPos, rebuild) → Array<Vector2>pathFinder.js:759

Find a path from startPos to endPos in world space. Returns an array of world-space Vector2 points; empty array if no path exists.

Start and end are snapped to the nearest walkable tile (a costed one counts), within 10 tiles. Intermediate points are tile centers unless the string-pulling smoothing pass moves them off-grid.

By default, calls buildNodeData() first, which asks isWalkable and getCost about every cell. When finding many paths with unchanged walkability, pass rebuild=false and call buildNodeData() once externally; the paths found are the same.

startPos Vector2

World-space start

endPos Vector2

World-space end

rebuild boolean optional, default true

Whether to call buildNodeData first

getCost(x, y) → numberpathFinder.js:175

Default extra cost for stepping on a cell. Returns 0 (free) by default. Override to add cost-weighted terrain (mud, swamp, etc).

x number

Tile x

y number

Tile y

getNearestClearNode(worldPos, searchRange, rebuild) → PathFinderNode|nullpathFinder.js:394

Find the clear (walkable, zero-cost) node closest to the given world position. Spirals outward in expanding boxes until a clear node is found or the search range is exhausted. Useful for snapping a click or NPC spawn position to the nearest open tile.

By default, calls buildNodeData() first so it works correctly on a fresh PathFinder. If you're calling it many times in a row with unchanged walkability, pass rebuild=false and call buildNodeData() once externally to avoid redundant work.

worldPos Vector2
searchRange number optional, default 10

Max box-radius in tiles

rebuild boolean optional, default true

Whether to call buildNodeData first

getNode(x, y) → PathFinderNode|nullpathFinder.js:184

Get the node at tile coords, or null if out of bounds

x number
y number
isWalkable(x, y) → booleanpathFinder.js:164

Default walkability: if a tile layer was provided, returns true when the cell has no solid (positive) collision data, so negative data is open like it is to the engine's collision; otherwise returns true. Override on the instance or via a subclass.

x number

Tile x

y number

Tile y

tileToWorld(x, y) → Vector2pathFinder.js:204

Convert integer tile coords to the world-space center of that tile

x number
y number
worldToTile(worldPos) → Vector2pathFinder.js:193

Convert a world-space position to integer tile coords (no clamping)

worldPos Vector2