Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add agent_radius to aabb obstacles #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions examples/auto_navmesh_avian3d.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use std::time::Duration;

use avian3d::{math::*, prelude::*};
use bevy::{color::palettes, math::vec2, prelude::*, time::common_conditions::on_timer};
use bevy::{
color::palettes,
math::vec2,
prelude::*,
render::{
settings::{Backends, PowerPreference, RenderCreation, WgpuSettings},
RenderPlugin,
},
time::common_conditions::on_timer,
};

use vleue_navigator::prelude::*;

Expand All @@ -11,14 +20,23 @@ struct Obstacle;
fn main() {
let mut app = App::new();
app.add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Navmesh with Polyanya".to_string(),
fit_canvas_to_parent: true,
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: "Navmesh with Polyanya".to_string(),
fit_canvas_to_parent: true,
..default()
}),
..default()
})
.set(RenderPlugin {
render_creation: RenderCreation::Automatic(WgpuSettings {
backends: Some(Backends::DX12),
power_preference: PowerPreference::HighPerformance,
..default()
}),
..default()
}),
..default()
}),
PhysicsPlugins::default().with_length_unit(20.0),
VleueNavigatorPlugin,
NavmeshUpdaterPlugin::<Collider, Obstacle>::default(),
Expand Down Expand Up @@ -189,9 +207,10 @@ fn setup(
build_timeout: Some(1.0),
simplify: 0.005,
merge_steps: 0,
agent_radius: 2.0,
..default()
},
update_mode: NavMeshUpdateMode::Direct,
update_mode: NavMeshUpdateMode::Debounced(0.5),
transform: Transform::from_xyz(0.0, idx as f32 * height_step + 0.1, 0.0)
.with_rotation(Quat::from_rotation_x(FRAC_PI_2)),
handle: Handle::<NavMesh>::weak_from_u128(idx as u128),
Expand Down
8 changes: 2 additions & 6 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,8 @@ fn life_of_obstacle(
if cachable.remove(&entity) {
commands.entity(entity).remove::<CachableObstacle>();
}
} else {
if example_settings.cache_enabled {
if cachable.insert(entity) {
commands.entity(entity).insert(CachableObstacle);
}
}
} else if example_settings.cache_enabled && cachable.insert(entity) {
commands.entity(entity).insert(CachableObstacle);
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions examples/helpers/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use vleue_navigator::prelude::*;
pub enum UiSettings {
Simplify,
MergeSteps,
AgentRadius,
Cache,
}

Expand All @@ -14,6 +15,8 @@ pub enum UiSettingsButtons {
SimplifyDec,
MergeStepsInc,
MergeStepsDec,
AgentRadiusInc,
AgentRadiusDec,
ToggleCache,
}

Expand Down Expand Up @@ -137,6 +140,36 @@ pub fn setup_settings<const WITH_CACHE: bool>(mut commands: Commands) {
button(" - ", UiSettingsButtons::MergeStepsDec, parent);
button(" + ", UiSettingsButtons::MergeStepsInc, parent);
});
parent
.spawn(NodeBundle { ..default() })
.with_children(|parent| {
parent.spawn((
TextBundle {
text: Text::from_sections(
[("Agent Radius: ", 30.0), ("{}", 30.0)].into_iter().map(
|(text, font_size): (&str, f32)| {
TextSection::new(
text,
TextStyle {
font_size,
..default()
},
)
},
),
),
style: Style {
margin: UiRect::all(Val::Px(12.0)),
..default()
},
..default()
}
.with_text_justify(JustifyText::Right),
UiSettings::AgentRadius,
));
button(" - ", UiSettingsButtons::AgentRadiusDec, parent);
button(" + ", UiSettingsButtons::AgentRadiusInc, parent);
});
if WITH_CACHE {
parent
.spawn((
Expand Down Expand Up @@ -187,6 +220,9 @@ pub fn display_settings(
UiSettings::MergeSteps => {
text.sections[1].value = format!("{}", settings.merge_steps)
}
UiSettings::AgentRadius => {
text.sections[1].value = format!("{}", settings.agent_radius)
}
UiSettings::Cache => (),
}
}
Expand All @@ -196,6 +232,7 @@ pub fn display_settings(
match param {
UiSettings::Simplify => (),
UiSettings::MergeSteps => (),
UiSettings::AgentRadius => (),
UiSettings::Cache => {
*color = if example_settings.cache_enabled {
palettes::tailwind::GREEN_400.into()
Expand Down Expand Up @@ -233,6 +270,12 @@ pub fn update_settings<const STEP: u32>(
UiSettingsButtons::MergeStepsInc => {
settings.merge_steps = (settings.merge_steps + 1).min(5);
}
UiSettingsButtons::AgentRadiusDec => {
settings.agent_radius = (settings.agent_radius - 0.5).max(0.0);
}
UiSettingsButtons::AgentRadiusInc => {
settings.agent_radius = (settings.agent_radius + 0.5).min(10.0);
}
UiSettingsButtons::ToggleCache => {
example_settings.cache_enabled = !example_settings.cache_enabled;
}
Expand Down
1 change: 1 addition & 0 deletions src/obstacles/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl ObstacleSource for Aabb {
obstacle_transform: &GlobalTransform,
navmesh_transform: &Transform,
(up, _shift): (Dir3, f32),
agent_radius: f32,
) -> Vec<Vec2> {
let transform = obstacle_transform.compute_transform();
let world_to_mesh = world_to_mesh(navmesh_transform);
Expand Down
10 changes: 7 additions & 3 deletions src/obstacles/avian2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ impl ObstacleSource for Collider {
obstacle_transform: &GlobalTransform,
navmesh_transform: &Transform,
up: (Dir3, f32),
agent_radius: f32,
) -> Vec<Vec2> {
self.shape_scaled()
.as_typed_shape()
.get_polygon(obstacle_transform, navmesh_transform, up)
self.shape_scaled().as_typed_shape().get_polygon(
obstacle_transform,
navmesh_transform,
up,
agent_radius,
)
}
}

Expand Down
Loading
Loading