Coder Social home page Coder Social logo

vleue_navigator's Introduction

Navigation for Bevy with NavMesh

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 NavMesh from it and using it for getting paths between random points.

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

use vleue_navigator::{NavMesh, VleueNavigatorPlugin};
use rand::Rng;

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, VleueNavigatorPlugin))
        .add_systems(Startup, load)
        .add_systems(Update, get_path)
        .run()
}

#[derive(Resource)]
struct Handles(Handle<Gltf>, Option<Handle<NavMesh>>);

fn load(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.insert_resource(Handles(asset_server.load("navmesh.glb"), None));
}

fn get_path(
    mut handles: ResMut<Handles>,
    gltfs: Res<Assets<Gltf>>,
    gltf_meshes: Res<Assets<GltfMesh>>,
    meshes: Res<Assets<Mesh>>,
    mut navmeshes: ResMut<Assets<NavMesh>>,
) {
    if handles.1.is_none() {
        // Get the gltf struct loaded from the file
        let Some(gltf) = gltfs.get(&handles.0) else {
            return
         };
        // Get the mesh called `navmesh`
        let Some(gltf_mesh) = gltf_meshes.get(&gltf.named_meshes["navmesh"]) else {
            return
         };
        // Get the actual mesh
        let Some(mesh) = meshes.get(&gltf_mesh.primitives[0].mesh) else {
            return
        };
        // Build a `NavMesh` from that mesh, then save it as an asset
        handles.1 = Some(navmeshes.add(NavMesh::from_bevy_mesh(mesh)));
    } else {
        // Get the navmesh, then search for a path
        let Some(navmesh) = navmeshes.get(handles.1.as_ref().unwrap()) else {
            return
        };
        // Find two random point
        let from = Vec2::new(
            rand::thread_rng().gen_range(-50.0..50.0),
            rand::thread_rng().gen_range(-50.0..50.0),
        );
        let to = Vec2::new(
            rand::thread_rng().gen_range(-50.0..50.0),
            rand::thread_rng().gen_range(-50.0..50.0),
        );
        if let Some(path) = navmesh.path(from, to) {
            info!("path from {} to {}: {:?}", from, to, path);
        } else {
            info!("no path between {} and {}", from, to)
        }
    }
}
Bevy vleue_navigator
0.13 0.7

vleue_navigator's People

Contributors

elabajaba avatar janhohenheim avatar mockersf avatar shnewto avatar vrixyz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vleue_navigator's Issues

Bake pathmeshes

Since they can no longer be modified after the PathMesh creation anyways, might as well bake them on creation.

Release on crates.io

Seeing as, in my opinion, the project is quite usable as-is, what do you think about releasing it? :)

Having issues running base example

➜  Bevy Engine git clone [email protected]:vleue/bevy_pathmesh.git                                                                
Cloning into 'bevy_pathmesh'...
remote: Enumerating objects: 154, done.
remote: Counting objects: 100% (154/154), done.
remote: Compressing objects: 100% (62/62), done.
remote: Total 154 (delta 63), reused 141 (delta 52), pack-reused 0
Receiving objects: 100% (154/154), 318.01 KiB | 37.00 KiB/s, done.
Resolving deltas: 100% (63/63), done.
➜  Bevy Engine cd bevy_pathmesh                      
➜  bevy_pathmesh git:(main) cargo run example --plane
error: a bin target must be available for `cargo run`
➜  bevy_pathmesh git:(main) cargo --version          
cargo 1.64.0-nightly (d8d30a753 2022-07-19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.