Navigation meshes with Recast and Detour¶
A navigation mesh, or NavMesh, represents the walkable parts of a level as connected polygons. An agent can use it to find a route around obstacles without searching every triangle in the visible scene. Your game still controls how the agent moves and animates, and decides when to request a new path.
Recast Navigation provides two complementary tools:
- Recast builds a navigation mesh from level geometry and settings such as the agent's height, radius, maximum slope and step height.
- Detour uses navigation data to find paths and perform spatial queries. It supports both a single navigation mesh and tiled meshes, which can be loaded, removed or rebuilt in parts as a world changes.
Building a navigation mesh¶
Choose the level geometry agents should be able to walk on, leaving out decorative geometry that should not affect navigation. Recast then builds the mesh in stages:
- Configure the bounds, voxel resolution and dimensions of the agent, and rasterize the selected triangles into a heightfield.
- Filter surfaces the agent cannot reach or stand on, then build a compact heightfield and account for the agent's radius.
- Divide the walkable area into regions, trace and simplify their contours, and create a polygon mesh.
- Build a detail mesh to better represent surface height, then create Detour navigation data and initialize a query object.
The settings depend on your world's scale and the agent's size. Smaller cells retain more geometric detail but increase the amount of data to process. Visualize the resulting mesh and test doorways, stairs, narrow ledges and disconnected areas before using it for gameplay.
Using the result¶
Find valid positions on the mesh near the agent and its destination, then use a Detour query to find a path between them. Feed the resulting path to your character controller and request a new path when the destination or navigable world changes. For a changing or streamed world, consider dividing navigation data into tiles rather than rebuilding the whole mesh at once.
Java example¶
The Recast Navigation for jME example walks through configuration, mesh construction and initialization of a Detour query in Java. It uses jNavigation, a Java binding to the native Recast and Detour libraries; its classes and setup are specific to that binding. Check its dependencies and native binaries for your jMonkeyEngine version and target platforms before using the example. The example initializes the query; your game must still request paths and move agents along them.
For implementation details, see the Recast Navigation project and the jNavigation source.