Recursive AI

Rebuild a navmesh for dynamic obstacles in C#

OnePath is a free, open-source pathfinding library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Rebuild affected navigation tiles when a wall appears or disappears.

Terminal
dotnet add package RecursiveAI.OnePath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OnePath;
using RecursiveAI.OnePath.Mesh.Build3D;
using RecursiveAI.OnePath.Mesh.Queries;
 
double[] v = [0,0,0, 0,0,6,
              20,0,6, 20,0,0];
int[] ix = [0,1,2, 0,2,3];
var cache = new Mesh3DPipeline(new())
    .BuildSoupCached(v, ix);
var query = new NavMeshQuery(cache.Mesh);
Console.WriteLine($"Open: {query.FindPath(
    new(2,0,3), new(18,0,3)).Status}");
int wall = cache.AddObstacle(
    TileObstacle.Box(9,-1,0, 11,3,6));
cache.UpdateAll();
Console.WriteLine($"Blocked: {query.FindPath(
    new(2,0,3), new(18,0,3)).Status}");
Text
Open: Found
Blocked: Partial

Unzip the example, then dotnet run.

The full program

The input is a 20-by-6 floor made from two upward-facing triangles, with Y as up. A box spans the floor’s width and splits it into two disconnected sides. The program queries the same endpoints before adding it, after rebuilding and after removing it.

C#
using RecursiveAI.OnePath;
using RecursiveAI.OnePath.Mesh.Build3D;
using RecursiveAI.OnePath.Mesh.Queries;
 
double[] vertices = [0,0,0, 0,0,6, 20,0,6, 20,0,0];
int[] indices = [0,1,2, 0,2,3];
var cache = new Mesh3DPipeline(new NavSettings()).BuildSoupCached(vertices, indices);
var query = new NavMeshQuery(cache.Mesh);
Console.WriteLine($"Open: {query.FindPath(new(2,0,3), new(18,0,3)).Status}");
int wall = cache.AddObstacle(TileObstacle.Box(9,-1,0, 11,3,6));
cache.UpdateAll();
Console.WriteLine($"Blocked: {query.FindPath(new(2,0,3), new(18,0,3)).Status}");
cache.RemoveObstacle(wall);
cache.UpdateAll();
Console.WriteLine($"Reopened: {query.FindPath(new(2,0,3), new(18,0,3)).Status}");
Text
Open: Found
Blocked: Partial
Reopened: Found

Partial means the route stops on the reachable side, not that the character can cross the box. Invalid obstacle dimensions throw ArgumentException or ArgumentOutOfRangeException naming the value.

Variations

Process at most one queued tile per call instead of all at once. Replace the first cache.UpdateAll(); with:

C#
while (cache.PendingCount > 0)
    cache.Update(1);

The program still prints:

Text
Open: Found
Blocked: Partial
Reopened: Found

In a game loop, call Update(1) once per tick rather than looping until the queue empties.

The short answer

Build geometry with Mesh3DPipeline.BuildSoupCached, retain its TileCache, add a TileObstacle, and call UpdateAll or budgeted Update. Remove the obstacle by its returned ID and rebuild again to reopen the route. Queries use cache.Mesh, the same mesh object throughout.

Adding an obstacle only queues work. Until the relevant tile is processed, its old walkable surface is still visible to queries. Make that timing explicit in your movement code: either finish rebuilding before requesting the new route, or allow a period during which tiles reflect different stages of the update.

How do you update a navmesh at runtime?

Use a cached build from the start. The cache retains each tile’s compact heightfield after filtering, erosion and build-time area volumes. A rebuild copies that intermediate data, applies the current obstacles and runtime volumes, then regenerates regions, contours, polygons and tile data. It does not rerasterize the original triangles or erode the copied surface again.

The runtime builder documentation defines the sequence. Obstacles can remove walkability from existing cached surfaces; they cannot create a walkable floor where the initial geometry had none. Adding a new bridge as geometry is therefore a different operation from removing a blocking box.

Update(maxTiles) consumes a tile-count budget, not an elapsed-time budget. The variation drains the queue one tile at a time to show the API. A host can instead spread those calls across ticks. Every processed tile uses the obstacles and volumes in force when it is processed, so changing an obstacle again before its queued work runs applies the latest state.

How do dynamic navmesh obstacles block a path?

OnePath provides cylinders, axis-aligned boxes and oriented boxes with height ranges. The runtime pipeline grows the footprint by the character radius because the cached surface has already been eroded. It marks qualifying spans as non-walkable when their cell centers lie inside the grown footprint and their floor heights lie inside the obstacle’s vertical interval.

Use a box for a rectangular barricade, a cylinder for a round column, and an oriented box for a rotated rectangular footprint. Choose the height interval to include the floor being blocked. A shape over an upper floor need not block a lower floor outside that interval.

The example’s box crosses the entire narrow floor. A smaller obstacle might leave a route around it; a changed path length alone would not demonstrate lost reachability. Here, Found, Partial, then Found distinguishes the open route, the disconnected destination and the reopened route.

Request a new path when the route’s tiles change. A changed layer replaces its tile and gives it a new generation, so polygon references into that layer become stale. An unchanged layer keeps its tile and references. Query objects see replacements on the same mesh object; there is no need to construct a separate mesh merely to see an update.

Do not treat Partial as arrival at the requested destination. It represents progress toward a destination the search did not reach. Decide whether to wait for a door to reopen, choose another goal or stop at the returned endpoint. Keep the obstacle ID if you need to remove it later: removing all obstacles restores the initial walkable tile content, but previously stored references into replaced tiles should still be reacquired.

All onepath recipes