Navigation Mesh API
createNavMesh
Section titled “createNavMesh”function createNavMesh(): NavMeshBuilding Navigation Mesh
Section titled “Building Navigation Mesh”const navmesh = createNavMesh();
// Add convex polygonsconst id1 = navmesh.addPolygon([ { x: 0, y: 0 }, { x: 10, y: 0 }, { x: 10, y: 10 }, { x: 0, y: 10 }]);
const id2 = navmesh.addPolygon([ { x: 10, y: 0 }, { x: 20, y: 0 }, { x: 20, y: 10 }, { x: 10, y: 10 }]);
// Method 1: Auto-detect shared edges and build connectionsnavmesh.build();
// Method 2: Manually set connectionsnavmesh.setConnection(id1, id2, { left: { x: 10, y: 0 }, right: { x: 10, y: 10 }});Querying and Pathfinding
Section titled “Querying and Pathfinding”// Find polygon containing pointconst polygon = navmesh.findPolygonAt(5, 5);
// Check if position is walkablenavmesh.isWalkable(5, 5);
// Pathfinding (uses funnel algorithm internally)const result = navmesh.findPath(1, 1, 18, 8);Use Cases
Section titled “Use Cases”Navigation mesh is suitable for:
- Complex irregular terrain
- Scenarios requiring precise paths
- Large maps where polygon count is much less than grid cells
// Load navigation mesh data from editorconst navData = await loadNavMeshData('level1.navmesh');
const navmesh = createNavMesh();for (const poly of navData.polygons) { navmesh.addPolygon(poly.vertices);}navmesh.build();