NavMesh for Bevy

MIT/Apache 2.0 Release Doc Crate

Navigation mesh for Bevy using Polyanya.

map with many points finding their paths

Check out the WASM demo

Usage

Loading a mesh from a gLTF file, then building a PathMesh from it and using it for getting paths between random points.

```rust,no_run use bevy::{ gltf::{Gltf, GltfMesh}, prelude::*, };

use bevy_pathmesh::{PathMesh, PathMeshPlugin}; use rand::Rng;

fn main() { App::new() .addplugins(DefaultPlugins) .addplugin(PathMeshPlugin) .addstartupsystem(load) .addsystem(getpath) .run() }

[derive(Resource)]

struct Handles(Handle, Option>);

fn load(mut commands: Commands, assetserver: Res) { commands.insertresource(Handles(asset_server.load("navmesh.glb"), None)); }

fn getpath( mut handles: ResMut, gltfs: Res>, gltfmeshes: Res>, meshes: Res>, mut pathmeshes: ResMut>, ) { if handles.1.isnone() { // Get the gltf struct loaded from the file let Some(gltf) = gltfs.get(&handles.0) else { return }; // Get the mesh called navmesh let Some(gltfmesh) = gltfmeshes.get(&gltf.namedmeshes["navmesh"]) else { return }; // Get the actual mesh let Some(mesh) = meshes.get(&gltfmesh.primitives[0].mesh) else { return }; // Build a PathMesh from that mesh, then save it as an asset handles.1 = Some(pathmeshes.add(PathMesh::frombevymesh(mesh))); } else { // Get the path mesh, then search for a path let Some(pathmesh) = pathmeshes.get(handles.1.asref().unwrap()) else { return }; // Find two random point let from = Vec2::new( rand::threadrng().genrange(-50.0..50.0), rand::threadrng().genrange(-50.0..50.0), ); let to = Vec2::new( rand::threadrng().genrange(-50.0..50.0), rand::threadrng().genrange(-50.0..50.0), ); if let Some(path) = path_mesh.path(from, to) { info!("path from {} to {}: {:?}", from, to, path); } else { info!("no path between {} and {}", from, to) } } } ```