Skip to content

Commit

Permalink
primitive fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Aug 3, 2024
1 parent 51e9fac commit 4547d0b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 127 deletions.
2 changes: 1 addition & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn setup(mut commands: Commands, mut materials: ResMut<Assets<StandardMaterial>>
build_timeout: Some(0.5),
..default()
},
transform: Transform::from_rotation(Quat::from_rotation_x(FRAC_PI_2)),
transform: Transform::from_rotation(Quat::from_rotation_x(-FRAC_PI_2)),
// Mark it for update as soon as obstacles are changed.
// Other modes can be debounced or manually triggered.
update_mode: NavMeshUpdateMode::Debounced(0.2),
Expand Down
144 changes: 51 additions & 93 deletions examples/primitive_3d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::f32::consts::{FRAC_PI_2, PI};

use bevy::{color::palettes, math::vec2, pbr::NotShadowCaster, prelude::*, window::PrimaryWindow};
use bevy::{
color::palettes,
math::{vec2, vec3},
prelude::*,
window::PrimaryWindow,
};
use polyanya::Triangulation;
use rand::Rng;
use vleue_navigator::prelude::*;
Expand Down Expand Up @@ -44,7 +49,6 @@ fn main() {
.add_systems(
Update,
(
display_mesh,
spawn_obstacle_on_click.after(ui::update_settings::<10>),
ui::update_stats::<PrimitiveObstacle>,
remove_obstacles,
Expand Down Expand Up @@ -99,24 +103,46 @@ fn setup(
}

// Spawn a new navmesh that will be automatically updated.
commands.spawn(NavMeshBundle {
settings: NavMeshSettings {
// Define the outer borders of the navmesh.
fixed: Triangulation::from_outer_edges(&vec![
vec2(0.0, 0.0),
vec2(MESH_WIDTH as f32, 0.0),
vec2(MESH_WIDTH as f32, MESH_HEIGHT as f32),
vec2(0.0, MESH_HEIGHT as f32),
]),
simplify: 0.001,
merge_steps: 0,
commands.spawn((
NavMeshBundle {
settings: NavMeshSettings {
// Define the outer borders of the navmesh.
fixed: Triangulation::from_outer_edges(&vec![
vec2(0.0, 0.0),
vec2(MESH_WIDTH as f32, 0.0),
vec2(MESH_WIDTH as f32, MESH_HEIGHT as f32),
vec2(0.0, MESH_HEIGHT as f32),
]),
simplify: 0.001,
merge_steps: 0,

..default()
},
transform: Transform::from_rotation(Quat::from_rotation_x(-FRAC_PI_2)),
// Mark it for update as soon as obstacles are changed.
// Other modes can be debounced or manually triggered.
update_mode: NavMeshUpdateMode::Direct,

..default()
},
transform: Transform::from_rotation(Quat::from_rotation_x(FRAC_PI_2)),
// Mark it for update as soon as obstacles are changed.
// Other modes can be debounced or manually triggered.
update_mode: NavMeshUpdateMode::Direct,
NavMeshDebug(palettes::tailwind::SLATE_800.into()),
));

commands.spawn(PbrBundle {
mesh: meshes
.add(Plane3d::new(
Vec3::Y,
Vec2::new(MESH_WIDTH as f32 / 2.0, MESH_HEIGHT as f32 / 2.0),
))
.into(),
transform: Transform::from_translation(Vec3::new(
MESH_WIDTH as f32 / 2.0,
0.0,
MESH_HEIGHT as f32 / 2.0,
)),
material: materials.add(StandardMaterial::from(Color::Srgba(
palettes::tailwind::BLUE_800,
))),
..default()
});

Expand Down Expand Up @@ -159,8 +185,7 @@ fn new_obstacle(
};
commands
.spawn((
transform,
GlobalTransform::default(),
SpatialBundle::from_transform(transform),
PrimitiveObstacle::Rectangle(larger_primitive),
))
.with_children(|parent| {
Expand All @@ -181,8 +206,7 @@ fn new_obstacle(
};
commands
.spawn((
transform,
GlobalTransform::default(),
SpatialBundle::from_transform(transform),
PrimitiveObstacle::Circle(larger_primitive),
))
.with_children(|parent| {
Expand All @@ -203,8 +227,7 @@ fn new_obstacle(
};
commands
.spawn((
transform,
GlobalTransform::default(),
SpatialBundle::from_transform(transform),
PrimitiveObstacle::Ellipse(larger_primitive),
))
.with_children(|parent| {
Expand All @@ -225,8 +248,7 @@ fn new_obstacle(
});
commands
.spawn((
transform,
GlobalTransform::default(),
SpatialBundle::from_transform(transform),
PrimitiveObstacle::CircularSector(larger_primitive),
))
.with_children(|parent| {
Expand All @@ -246,8 +268,7 @@ fn new_obstacle(
});
commands
.spawn((
transform,
GlobalTransform::default(),
SpatialBundle::from_transform(transform),
PrimitiveObstacle::CircularSegment(larger_primitive),
))
.with_children(|parent| {
Expand All @@ -265,8 +286,7 @@ fn new_obstacle(
Capsule2d::new(primitive.radius + radius, primitive.half_length * 2.0);
commands
.spawn((
transform,
GlobalTransform::default(),
SpatialBundle::from_transform(transform),
PrimitiveObstacle::Capsule(larger_primitive),
))
.with_children(|parent| {
Expand All @@ -284,8 +304,7 @@ fn new_obstacle(
RegularPolygon::new(primitive.circumradius() + radius, primitive.sides);
commands
.spawn((
transform,
GlobalTransform::default(),
SpatialBundle::from_transform(transform),
PrimitiveObstacle::RegularPolygon(larger_primitive),
))
.with_children(|parent| {
Expand All @@ -305,8 +324,7 @@ fn new_obstacle(
);
commands
.spawn((
transform,
GlobalTransform::default(),
SpatialBundle::from_transform(transform),
PrimitiveObstacle::Rhombus(larger_primitive),
))
.with_children(|parent| {
Expand All @@ -322,66 +340,6 @@ fn new_obstacle(
}
}

fn display_mesh(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
navmeshes: Res<Assets<NavMesh>>,
mut current_mesh_entity: Local<Option<Entity>>,
navmesh: Query<(&Handle<NavMesh>, Ref<NavMeshStatus>)>,
) {
let (navmesh_handle, status) = navmesh.single();
if !status.is_changed() {
return;
}

let Some(navmesh) = navmeshes.get(navmesh_handle) else {
return;
};
if let Some(entity) = *current_mesh_entity {
commands.entity(entity).despawn_recursive();
}

*current_mesh_entity = Some(
commands
.spawn(PbrBundle {
mesh: meshes
.add(Plane3d::new(
Vec3::Y,
Vec2::new(MESH_WIDTH as f32 / 2.0, MESH_HEIGHT as f32 / 2.0),
))
.into(),
transform: Transform::from_translation(Vec3::new(
(MESH_WIDTH as f32) / 2.0,
0.0,
MESH_HEIGHT as f32 / 2.0,
)),
material: materials.add(StandardMaterial::from(Color::Srgba(
palettes::tailwind::BLUE_800,
))),
..default()
})
.with_children(|main_mesh| {
main_mesh.spawn((
PbrBundle {
mesh: meshes.add(navmesh.to_wireframe_mesh()).into(),
material: materials.add(StandardMaterial::from(Color::Srgba(
palettes::tailwind::BLUE_400,
))),
transform: Transform::from_translation(Vec3::new(
-(MESH_WIDTH as f32) / 2.0,
0.1,
-(MESH_HEIGHT as f32) / 2.0,
)),
..default()
},
NotShadowCaster,
));
})
.id(),
);
}

fn spawn_obstacle_on_click(
mouse_button_input: Res<ButtonInput<MouseButton>>,
primary_window: Query<&Window, With<PrimaryWindow>>,
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
missing_docs
)]

use std::{f32::consts::PI, sync::Arc};
use std::sync::Arc;

#[cfg(feature = "debug-with-gizmos")]
use bevy::{
Expand All @@ -24,7 +24,7 @@ use bevy::{
app::{App, Plugin},
asset::{Asset, AssetApp},
log::{debug, warn},
math::{vec2, Quat, Vec2, Vec3, Vec3Swizzles},
math::{Quat, Vec2, Vec3, Vec3Swizzles},
prelude::{Mesh, Transform},
reflect::TypePath,
render::{
Expand Down Expand Up @@ -334,13 +334,11 @@ impl NavMesh {
}

pub(crate) fn mesh_to_world(navmesh_transform: &Transform) -> Transform {
let mut transform = Transform {
Transform {
translation: navmesh_transform.translation,
rotation: navmesh_transform.rotation.inverse(),
scale: 1.0 / navmesh_transform.scale,
};
// transform.rotate(Quat::from_rotation_x(std::f32::consts::PI));
transform
}
}

fn get_vectors(
Expand Down
Loading

0 comments on commit 4547d0b

Please sign in to comment.