Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created November 28, 2021 13:47
Show Gist options
  • Save agmcleod/c3b3336c6cd3e2bc8f6448485329cad6 to your computer and use it in GitHub Desktop.
Save agmcleod/c3b3336c6cd3e2bc8f6448485329cad6 to your computer and use it in GitHub Desktop.
use debug_gfx::draw_nav_mesh;
use std::fs::File;
use std::io::prelude::*;
use std::io::Result;
mod debug_gfx;
fn read_file(path: &str) -> Result<String> {
let mut text = String::new();
let mut file = File::open(path)?;
file.read_to_string(&mut text)?;
Ok(text)
}
fn main() -> nav::AnyResult<()> {
// let params = nav::MeshParams {
// agent_height: None,
// agent_max_climb: None,
// agent_radius: 1.0,
// max_polys_per_region: 20,
// mesh_origin: nav::Point::new(0.0, 0.0, 0.0),
// voxels_per_region: nav::Count3D::new(10, 10, 10),
// voxel_length: 1.0,
// };
let params = nav::MeshParams {
agent_height: None,
agent_max_climb: None,
agent_radius: 1.0,
max_polys_per_region: 2000,
mesh_origin: nav::Point::new(-16.0, 0.0, -16.0),
voxel_length: 1.0,
voxels_per_region: nav::Count3D::new(2500, 10, 2500),
};
let mut mesh = nav::NavMesh::new(&params)?;
let data = read_file("example-mesh2d/coords-data.csv")?;
let polygons = data.lines().map(|line| {
let columns: Vec<&str> = line.split(",").collect();
let points = columns.chunks(2).map(|vector| {
let x: f32 = vector[0].parse().expect("Could not parse value for x");
let z: f32 = vector[1].parse().expect("Could not parse value for z");
nav::Point::new(x, 0.0, z)
}).collect();
nav::PolyData::new(points).unwrap()
}).collect();
let region = nav::Region {
index_in_mesh: nav::Index3D::new(0, 0, 0),
polygons,
};
mesh.put_region(&region).unwrap();
// add_region(&mut mesh, (0, 1), vec![
// vec![(0, 10), (0, 20), (3, 17), (3, 13)],
// vec![(0, 20), (10, 20), (7, 17), (3, 17)],
// vec![(10, 20), (10, 10), (7, 13), (7, 17)],
// vec![(10, 10), (0, 10), (3, 13), (7, 13)],
// ]);
// add_region(&mut mesh, (1, 0), vec![
// vec![(15, 2), (10, 0), (10, 10), (15, 10)],
// vec![(15, 2), (20, 2), (20, 0), (10, 0)],
// ]);
// add_region(&mut mesh, (1, 1), vec![
// vec![(15, 15), (15, 10), (10, 10), (10, 20)],
// vec![(15, 15), (10, 20), (20, 20), (20, 15)],
// ]);
// add_region(&mut mesh, (2, 0), vec![
// vec![(20, 0), (20, 10), (30, 10), (30, 0)], // 1 polygon, full tile
// ]);
// add_region(&mut mesh, (2, 1), vec![
// vec![(20, 10), (20, 20), (30, 20), (30, 10)], // 1 polygon, full tile
// ]);
// let from = nav::Point::new(2.0, 0.0, 14.0);
// let to = nav::Point::new(26.0, 0.0, 8.0);
let from = nav::Point::new(0.0, 0.0, 0.0);
let to = nav::Point::new(10.0, 0.0, 10.0);
// let from = nav::Point::new(0.0, 0.0, 0.0);
// let to = nav::Point::new(2000.0 / 32.0, 0.0, 600.0 / 32.0);
let path = mesh.find_path(from, to).ok();
println!("{:?}", path);
draw_nav_mesh(&mesh, path.as_ref());
Ok(())
}
fn add_region(
mesh: &mut nav::NavMesh,
(x, z): (i32, i32),
polygons: Vec<Vec<(i32, i32)>>,
) {
let polygons = polygons.into_iter()
.map(|verts: Vec<(i32, i32)>| {
let points = verts.into_iter()
.map(|(x, z)| nav::Point::new(x as f32, 0.0, z as f32))
.collect();
nav::PolyData::new(points).unwrap()
})
.collect();
let region = nav::Region {
index_in_mesh: nav::Index3D::new(x, 0, z),
polygons,
};
mesh.put_region(&region).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment