NavGrid
A coarse grid of walkable and blocked cells built from a BGE.SolidWorld, for finding paths around its solids with A*.
Each solid is grown by agentRadius and every cell it overlaps is blocked, so a path that keeps the agent's centre in open cells keeps its body clear of the solids. Paths are smoothed with line-of-sight checks across the cells, so they don't zig-zag along the grid.
The grid works in the SolidWorld's own 2D coordinates. For a top-down game that's world x/y; for a 3D ground plane, store world z as the SolidWorld's y and read each waypoint's y as z.
Properties
cellSize(float) — Width and height of each cellagentRadius(float) — How far solids are grown before they block cells: the agent's radiusminX(float) — Bottom-left corner of the gridminY(float)cols(integer) — Grid size in cellsrows(integer)maxPooledSearches(integer) — Finished searches leave their per-cell arrays (about 3 entries per cell) for later searches
Example
nav = new BGE.NavGrid(world, {cellSize: 16, agentRadius: 10})
path = nav.findPath(enemy.position.x, enemy.position.y, player.position.x, player.position.y)
' path[0] is the start, path[path.count() - 1] the goal; [] if there's no way thereConstructor
new NavGrid(
world: BGE.SolidWorld,
options?: BGE.NavGridOptions,
): NavGridBuilds the grid. Every cell is tested once, so build it at scene load (in a LoadTask if the map is big) rather than mid-game.
Parameters
world(BGE.SolidWorld) — the solids to path around. The grid keeps it, forrebuild()options(BGE.NavGridOptions, optional, default: "{}") —cellSize(default 32),agentRadius(default 0), and the area to cover (minX,minY,width,height; default: every solid, plusagentRadiusand one cell round the outside)
Instance Methods
rebuild(): void
Rebuilds every cell from the world's solids. Cells blocked with setCellBlocked() stay blocked.
Returns
void
rebuildArea(x: float, y: float, w: float, h: float): void
Rebuilds the cells in one area from the world's solids, e.g. after a door's solid was removed. Cheaper than rebuild() for a small area.
Parameters
x(float) — bottom-left x of the area that changedy(float) — bottom-left yw(float)h(float)
Returns
void
setCellBlocked(
col: integer,
row: integer,
blocked: boolean,
): void
Blocks or unblocks one cell by hand, on top of the solids: e.g. from a map layout image, or to keep agents off a hazard they could walk through. rebuild() keeps these.
Parameters
col(integer)row(integer)blocked(boolean)
Returns
void
isCellBlocked(col: integer, row: integer): boolean
Whether a cell is blocked. Cells outside the grid count as blocked.
Parameters
col(integer)row(integer)
Returns
boolean
isWalkable(x: float, y: float): boolean
Whether a point is in an open cell.
Parameters
x(float)y(float)
Returns
boolean
isInGrid(col: integer, row: integer): boolean
Parameters
col(integer)row(integer)
Returns
boolean— true if the cell is inside the grid
colAt(x: float): integer
Parameters
x(float)
Returns
integer— the column containing x (may be outside the grid)
rowAt(y: float): integer
Parameters
y(float)
Returns
integer— the row containing y (may be outside the grid)
cellCenter(col: integer, row: integer): BGE.NavPoint
Parameters
col(integer)row(integer)
Returns
BGE.NavPoint— the centre of a cell
hasLineOfSight(
x0: float,
y0: float,
x1: float,
y1: float,
snapRings?: integer,
): boolean
Whether a straight line between two points crosses only open cells. A line through a cell corner needs both cells beside the corner open, as paths can't cut corners either.
Solids are grown by agentRadius, so an agent standing against a wall is often in a blocked cell. An end in a blocked cell is looked at from the centre of the open cell nearest it instead, up to snapRings cells away (as findNearestOpenCell()). Points outside the grid aren't moved.
Parameters
x0(float)y0(float)x1(float)y1(float)snapRings(integer, optional, default: 2) — how far to move an end out of a blocked cell; 0 counts any blocked end as blocked
Returns
boolean
findNearestOpenCell(
x: float,
y: float,
maxRings?: integer,
): dynamic
The open cell nearest a point, searching outwards ring by ring. A point outside the grid only finds a cell if the grid's edge is within maxRings cells of it.
Parameters
x(float)y(float)maxRings(integer, optional, default: 8) — how many cells out to look
Returns
dynamic— aBGE.NavCell, or invalid if there's no open cell that close
findPath(
fromX: float,
fromY: float,
toX: float,
toY: float,
options?: BGE.NavSearchOptions,
): Array.<BGE.NavPoint>
Finds a path in one go. Fine for short paths or small grids; for long searches use startSearch() and spread the work over frames, so the watchdog doesn't kill the channel.
Parameters
fromX(float)fromY(float)toX(float)toY(float)options(BGE.NavSearchOptions, optional, default: "{}")
Returns
Array.<BGE.NavPoint>— waypoints from start to goal (or towards it, for a partial path); [] if there's no path
startSearch(
fromX: float,
fromY: float,
toX: float,
toY: float,
options?: BGE.NavSearchOptions,
): BGE.NavPathSearch
Starts a path search to run a little at a time: call step(budgetMs) each frame until it returns true, then read status and path. Several searches can run at once, but each reads the grid as it is, so finish or restart them after changing it.
Parameters
fromX(float)fromY(float)toX(float)toY(float)options(BGE.NavSearchOptions, optional, default: "{}")
Returns
smoothPath(points: Array.<BGE.NavPoint>): Array.<BGE.NavPoint>
Removes waypoints that can be skipped: drops points in the middle of a straight run, then keeps the first and last, and each point after which the line from the last kept point would be blocked.
Parameters
points(Array.<BGE.NavPoint>)
Returns
Array.<BGE.NavPoint>
releaseSearchMemory(): void
Frees the search memory the grid keeps for reuse (see maxPooledSearches), e.g. after a burst of searches on a big map. Searches still running are unaffected.
Returns
void
getPaddedCells(): roByteArray
For internal use by NavPathSearch: the padded cell array and its row length
Returns
roByteArray
getPaddedStride(): integer
Returns
integer
acquireSearchState(): BGE.NavSearchState
For internal use by NavPathSearch: state for a new search, reused from a finished one if there is one. Each set is about 3 entries per cell.
Returns
releaseSearchState(state: BGE.NavSearchState): void
Parameters
state(BGE.NavSearchState)
Returns
void