PathFinding

LittleJS PathFinder Plugin

  • Grid-based A* pathfinder with two-pass smoothing for natural-looking paths
  • Works directly on a TileCollisionLayer, or override isWalkable/getCost for any grid
  • Debug visualization via engine debug primitives (stripped in release builds)
  • Port of frankforce.com pathFindingBase.cpp (2018)

Classes

PathFinder
PathFinderNode

Methods

(static) PathFinder#findPath(startPos, endPos) → {Array.<Vector2>}

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 via getNearestClearNode. Intermediate points are tile centers unless the string-pulling smoothing pass moves them off-grid.

Parameters:
NameTypeDescription
startPosVector2

World-space start

endPosVector2

World-space end

Returns:
Type: 
Array.<Vector2>

(static) PathFinder#getNearestClearNode(worldPos, searchRangeopt, rebuildopt) → {PathFinderNode|null}

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.

Parameters:
NameTypeAttributesDefaultDescription
worldPosVector2
searchRangenumber<optional>
10

Max box-radius in tiles

rebuildboolean<optional>
true

Whether to call buildNodeData first

Returns:
Type: 
PathFinderNode | null

(static) PathFinder#tileToWorld(x, y) → {Vector2}

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

Parameters:
NameTypeDescription
xnumber
ynumber
Returns:
Type: 
Vector2

(static) PathFinder#worldToTile(worldPos) → {Vector2}

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

Parameters:
NameTypeDescription
worldPosVector2
Returns:
Type: 
Vector2