Skip to content

feat: Add the flame_navigation package for path finding - #4259

Merged
ufrshubham merged 54 commits into
mainfrom
devkage/flame-navigation
Dec 4, 2026
Merged

ufrshubham merged 54 commits into
mainfrom
devkage/flame-navigation

Conversation

@ufrshubham

@ufrshubham ufrshubham commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Description

This adds flame_navigation, a new package for path finding in Flame games.

Flame has no path finding today. With this package, you bake the obstacles of your world into a
navigation grid once, and then agents find their way through it. You don't place waypoints or
build a graph by hand.

world.add(
  NavigationRegion(
    bounds: const Rect.fromLTWH(0, 0, 800, 600),
    cellSize: 8,
    sources: [HitboxNavigationSource(world)],
  ),
);

final agent = NavigationAgent(
  agentType: const NavigationAgentType(radius: 12),
  maxSpeed: 120,
);
world.add(PositionComponent(children: [agent]));
agent.target = Vector2(700, 300);

What the package has:

  • NavigationRegion bakes a NavigationGrid from its sources, and finds paths through it.
  • NavigationAgent is added as a child of a component. It finds a path to its target and moves
    its parent along it. With autoMove: false, it only gives a desiredVelocity, for games that
    move their characters themselves.
  • Sources: HitboxNavigationSource bakes the hitboxes of the world, ShapesNavigationSource
    bakes shapes made in code, and you can write your own.
  • One grid works for agents of any size. Each cell stores its clearance, the exact distance to the
    closest obstacle, and an agent only walks on the cells where it fits.
  • Areas with costs, like mud that is slow to walk through, and area masks to keep some agents out
    of some areas.
  • NavigationObstacle is for things that move, like doors and crates. Only the part of the grid
    around them is baked again.
  • NavigationGrid.toBytes and NavigationGrid.fromBytes save and load a grid, so big levels can
    be baked ahead of time and shipped as an asset.
  • Debug drawing behind debugMode, in the same way as hitboxes. Flags choose what is drawn: the
    grid, the blocked cells, the areas, the clearance, the cells that a search explored, the paths
    of the agents, and more.
  • NavigationBaker and AStar can also be used without any components.
1-click-to-move.mp4
2-agent-sizes.mp4
3-areas.mp4
4-obstacles.mp4
5-path-finding.mp4

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

@ufrshubham ufrshubham changed the title feat: Add the flame_navigation package for path finding feat: Add the flame_navigation package for path finding Nov 29, 2026
@ufrshubham

Copy link
Copy Markdown
Member Author

My clanker's response:

1. Clearance is too high near diagonal obstacles (confirmed) NavigationGrid.clearanceOf takes the distance between cell centers and subtracts half a cell. That is only right when the obstacle is straight left, right, above or below. When the blocked cell is diagonal, the obstacle can be as close as about 0.71 cell, not 0.5, so clearance can be up to about 0.21 cell too high.

I ran a test against the PR branch: an 8×8 grid with 1-unit cells and a tiny obstacle at (4.01, 4.01).
What was checked Result
clearanceOf cell (3, 3) 0.875
Real distance to the obstacle 0.721
Walkable for an agent of radius 0.85 yes, overlapping by about 0.13

This contradicts the PR description's "exact distance to the closest obstacle" and the docs' "an agent only walks on the cells where it fits".

* **Simplest fix:** subtract `sqrt2 / 2` instead of `0.5`. That is safe but a bit too strict when the obstacle is straight to the side.

* **Exact fix:** measure the distance to the nearest point of the blocked cell instead of its center.

* **Or:** document the clearance as approximate.

Clearance is now measured to the closest point of each blocked cell, not to its center, so it's exact in every direction (5b9736c). I went with the exact fix: the same distance transform on a lattice of half-cell steps. The √2/2 version was too strict for agents that are small compared to the cells. Your repro is now a test.

2. region.revision can miss grid changes Agents watch NavigationRegion.revision to know when to find a new path, but it is a separate counter from grid.revision. Calling region.grid.setAreaCosts(...), or changing the grid by hand, updates grid.revision and leaves region.revision alone, so agents keep their old paths. Using grid.revision (or adding it in) would fix this.

region.revision now picks up changes made to the grid directly, like setAreaCosts (5c541bf)

3. Per-frame work that grows with the size of the game

* While no region exists, `NavigationAgent._findRegion` and `NavigationObstacle.update` (through `_register`) call `NavigationRegion.of` every frame. That runs `findGame().descendants()`, a walk over the whole component tree, for each agent and obstacle.

* Every frame a carving obstacle moves, `NavigationRegion.update` calls `_collect()`. With a `HitboxNavigationSource`, that walks every component in the world and checks the parents of each hitbox, even though only the shapes overlapping the changed area are used.

* `NavigationObstacle.update` builds new shapes on every frame just to compare bounds.

This could be noticeable in large worlds with obstacles that move all the time. I'd at least mention it in the docs; better, cache the shapes from the sources between full bakes.

Regions are tracked per game instead of being looked up through the component tree (8d9cf60). Rebakes caused only by obstacles reuse the shapes of the sources, while markDirty still reads them again, since that's how users report moved hitboxes (33f5cc2). Obstacles now compare their transform instead of building shapes every frame (2315dbc)

4. NavigationGrid.fromBytes only checks the header with asserts (nit) columns, rows and maxClearanceCells come from the bytes and are only checked by the constructor's asserts, which don't run in release builds. Bad bytes with columns: 0 or maxClearanceCells: 0 load quietly instead of throwing a FormatException. It's cheap to check them explicitly, since loading grids from assets is a promoted use case.

fromBytes checks the header explicitly and throws a FormatException (7229e15).

@ufrshubham ufrshubham self-assigned this Dec 3, 2026
@ufrshubham ufrshubham changed the title feat: Add the flame_navigation package for path finding feat: Add the flame_navigation package for path findin Dec 3, 2026
@ufrshubham ufrshubham changed the title feat: Add the flame_navigation package for path findin feat: Add the flame_navigation package for path finding Dec 3, 2026
@ufrshubham
ufrshubham added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit 62be148 Dec 4, 2026
9 checks passed
@ufrshubham
ufrshubham deleted the devkage/flame-navigation branch December 4, 2026 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants