Skip to content

Commit

Permalink
feat(rrt): rrt environment example started
Browse files Browse the repository at this point in the history
  • Loading branch information
jens-hj committed Apr 15, 2024
1 parent 92cb3d3 commit 60cc763
Show file tree
Hide file tree
Showing 25 changed files with 302 additions and 233 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
**/*.png
!**/assets/**/*

**/*rustc-ice*

.bacon-locations
29 changes: 13 additions & 16 deletions crates/gbpplanner-rs/examples/rrt3d.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
//! This is a simple example of the `rrt` crate within Bevy.
// mod super::theme;

use bevy::math::primitives::Cuboid;
use bevy::prelude::*;
// use bevy::render::primitives::Sphere;
use bevy_infinite_grid::{InfiniteGridBundle, InfiniteGridPlugin, InfiniteGridSettings};
use catppuccin::Flavour;
use gbpplanner_rs::environment::camera::{CameraMovementMode, CameraResetEvent};
use gbpplanner_rs::environment::{MainCamera, ObstacleMarker};
use gbpplanner_rs::input::camera::CameraInputPlugin;
use gbpplanner_rs::movement::{self, LinearMovementBundle, MovementPlugin, OrbitMovementBundle};
use gbpplanner_rs::theme::{CatppuccinTheme, ColorFromCatppuccinColourExt};
use gbpplanner_rs::ui::controls::ChangingBinding;
use gbpplanner_rs::ui::ActionBlock;
// use ncollide3d::na::{self, Isometry3, Vector3};
// use ncollide3d::query;
// use ncollide3d::query::Proximity;
// use ncollide3d::shape;
use gbpplanner_rs::{
environment::{
camera::{CameraMovementMode, CameraResetEvent},
MainCamera, ObstacleMarker,
},
input::{camera::CameraInputPlugin, ChangingBinding},
movement::{self, LinearMovementBundle, MovementPlugin, OrbitMovementBundle},
theme::{CatppuccinTheme, ColorFromCatppuccinColourExt},
ui::ActionBlock,
};
use parry3d::{
na::{self, Isometry3, Vector3},
query::{self, intersection_test},
Expand All @@ -40,7 +37,7 @@ fn main() {
))
.init_state::<CameraMovementMode>()
.insert_resource(AmbientLight {
color: Color::default(),
color: Color::default(),
brightness: 1000.0,
})
.insert_resource(ClearColor(Color::from_catppuccin_colour(
Expand Down Expand Up @@ -138,7 +135,7 @@ fn rrt_path(
let p = CollisionProblem {
obstacle: shape::Cuboid::new(Vector3::new(0.5f32, 0.5, 0.25)),
// intersection sphere does not need a very big radius
ball: shape::Ball::new(0.1f32),
ball: shape::Ball::new(0.1f32),
};

if *index == path.len() {
Expand Down Expand Up @@ -223,7 +220,7 @@ fn infinite_grid(mut commands: Commands, catppuccin_theme: Res<CatppuccinTheme>)

struct CollisionProblem {
obstacle: shape::Cuboid,
ball: shape::Ball,
ball: shape::Ball,
}

impl CollisionProblem {
Expand Down
62 changes: 62 additions & 0 deletions crates/gbpplanner-rs/examples/rrtenv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! example crate to take the environment config from the config file
//! and generate the necessary environment and colliders for rrt to work
use bevy::prelude::*;
use gbpplanner_rs::{
asset_loader::AssetLoaderPlugin,
cli,
config::{read_config, Config, Environment, FormationGroup},
environment::EnvironmentPlugin,
input::{camera::CameraInputPlugin, ChangingBinding, InputPlugin},
theme::ThemePlugin,
};

fn main() -> anyhow::Result<()> {
better_panic::debug_install();

let cli = cli::parse_arguments();

let (config, formation, environment): (Config, FormationGroup, Environment) = if cli.default {
(
Config::default(),
FormationGroup::default(),
Environment::default(),
)
} else {
let config = read_config(cli.config.as_ref())?;
if let Some(ref inner) = cli.config {
println!(
"successfully read config from: {}",
inner.as_os_str().to_string_lossy()
);
}

let formation = FormationGroup::from_file(&config.formation_group)?;
println!(
"successfully read formation config from: {}",
config.formation_group
);
let environment = Environment::from_file(&config.environment)?;
println!(
"successfully read environment config from: {}",
config.environment
);

(config, formation, environment)
};

let mut app = App::new();
app.insert_resource(config)
.insert_resource(formation)
.insert_resource(environment)
.init_resource::<ChangingBinding>()
.add_plugins((
DefaultPlugins,
AssetLoaderPlugin,
CameraInputPlugin,
EnvironmentPlugin,
ThemePlugin,
))
.run();

Ok(())
}
12 changes: 10 additions & 2 deletions crates/gbpplanner-rs/src/bevy_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(missing_docs)]
#![warn(missing_docs)]
//! Useful function when working with bevy
use bevy::{app::Plugin, ecs::prelude::*, hierarchy::DespawnRecursiveExt};
Expand Down Expand Up @@ -61,10 +61,18 @@ impl BevyPluginExt for bevy::app::App {

pub mod run_conditions {
use bevy::{
ecs::system::Res,
ecs::{
event::{Event, Events},
system::Res,
},
input::{keyboard::KeyCode, ButtonInput},
};

/// Trait for checking if an event exists
pub fn event_exists<T: Event>(res_event: Option<Res<Events<T>>>) -> bool {
res_event.is_some()
}

// pub fn any_input_just_pressed(
// // inputs: impl IntoIterator<Item = ButtonInput<KeyCode>>,
// // inputs: impl IntoIterator<Item = KeyCode>,
Expand Down
2 changes: 1 addition & 1 deletion crates/gbpplanner-rs/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(missing_docs)]
#![warn(missing_docs)]
//! cli argument parser module
use clap::Parser;
Expand Down
2 changes: 1 addition & 1 deletion crates/gbpplanner-rs/src/config/reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(missing_docs)]
#![warn(missing_docs)]

use std::path::Path;

Expand Down
62 changes: 32 additions & 30 deletions crates/gbpplanner-rs/src/environment/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use bevy::{
use bevy_infinite_grid::{InfiniteGridBundle, InfiniteGridPlugin, InfiniteGridSettings};
use catppuccin::Flavour;

use crate::{asset_loader::SceneAssets, config, theme::CatppuccinTheme, ui};
use crate::{
asset_loader::SceneAssets, bevy_utils::run_conditions::event_exists, config,
input::DrawSettingsEvent, theme::CatppuccinTheme,
};

pub struct MapPlugin;

Expand All @@ -21,7 +24,12 @@ impl Plugin for MapPlugin {
.init_state::<HeightMapState>()
.add_plugins(InfiniteGridPlugin)
.add_systems(Startup, (infinite_grid, lighting, flat_map))
.add_systems(Update, (obstacles.run_if(environment_png_is_loaded), show_or_hide_height_map, show_or_hide_flat_map));
.add_systems(Update,
(
obstacles.run_if(environment_png_is_loaded),
show_or_hide_height_map.run_if(event_exists::<DrawSettingsEvent>),
show_or_hide_flat_map.run_if(event_exists::<DrawSettingsEvent>))
);
}
}

Expand Down Expand Up @@ -88,21 +96,18 @@ fn flat_map(
});

// Spawn an entity with the mesh and material, and position it in 3D space
commands.spawn((
FlatMap,
PbrBundle {
mesh: scene_assets.meshes.plane.clone(),
material: material_handle,
visibility: if config.visualisation.draw.sdf {
Visibility::Visible
} else {
Visibility::Hidden
},
transform: Transform::from_xyz(0.0, -0.1, 0.0)
.with_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
..default()
commands.spawn((FlatMap, PbrBundle {
mesh: scene_assets.meshes.plane.clone(),
material: material_handle,
visibility: if config.visualisation.draw.sdf {
Visibility::Visible
} else {
Visibility::Hidden
},
));
transform: Transform::from_xyz(0.0, -0.1, 0.0)
.with_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
..default()
}));
}

/// **Bevy** [`Update`] system
Expand All @@ -111,7 +116,7 @@ fn flat_map(
/// set the visibility of the [`VariableVisualiser`] entities
fn show_or_hide_flat_map(
mut query: Query<(&FlatMap, &mut Visibility)>,
mut draw_setting_event: EventReader<ui::DrawSettingsEvent>,
mut draw_setting_event: EventReader<DrawSettingsEvent>,
) {
for event in draw_setting_event.read() {
if matches!(event.setting, config::DrawSetting::Sdf) {
Expand Down Expand Up @@ -245,19 +250,16 @@ fn obstacles(
..default()
});

commands.spawn((
HeightMap,
PbrBundle {
mesh: meshes.add(mesh),
material: material_handle,
visibility: if config.visualisation.draw.height_map {
Visibility::Visible
} else {
Visibility::Hidden
},
..default()
commands.spawn((HeightMap, PbrBundle {
mesh: meshes.add(mesh),
material: material_handle,
visibility: if config.visualisation.draw.height_map {
Visibility::Visible
} else {
Visibility::Hidden
},
));
..default()
}));
}

/// **Bevy** [`Component`] to represent the heightmap.
Expand All @@ -271,7 +273,7 @@ pub struct HeightMap;
/// to set the visibility of the [`HeightMap`] entities
fn show_or_hide_height_map(
mut query: Query<(&HeightMap, &mut Visibility)>,
mut draw_setting_event: EventReader<ui::DrawSettingsEvent>,
mut draw_setting_event: EventReader<DrawSettingsEvent>,
) {
for event in draw_setting_event.read() {
if matches!(event.setting, config::DrawSetting::HeightMap) {
Expand Down
11 changes: 7 additions & 4 deletions crates/gbpplanner-rs/src/environment/map_generator.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
use angle::Angle;
use bevy::prelude::*;
use parry2d::shape;
// use bevy_more_shapes::Cylinder;
// use bevy_math::RegularPolygon;
use serde::{Deserialize, Serialize};

use crate::{
asset_loader::SceneAssets,
bevy_utils::run_conditions::event_exists,
config::{environment::PlaceableShape, Config, DrawSetting, Environment, Obstacle, Obstacles},
ui::DrawSettingsEvent,
input::DrawSettingsEvent,
};

use parry2d::shape;

pub struct GenMapPlugin;

impl Plugin for GenMapPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Colliders>()
.add_systems(Startup, (build_tile_grid, build_obstacles))
.add_systems(Update, show_or_hide_generated_map);
.add_systems(
Update,
show_or_hide_generated_map.run_if(event_exists::<DrawSettingsEvent>),
);
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/gbpplanner-rs/src/factorgraph/factorgraph.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![warn(missing_docs)]
use std::ops::Range;

use bevy::{
Expand Down
2 changes: 1 addition & 1 deletion crates/gbpplanner-rs/src/factorgraph/message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(missing_docs)]
#![warn(missing_docs)]
//! Message module.
//!
//! Contains the message struct that variables and factors send between each
Expand Down
22 changes: 13 additions & 9 deletions crates/gbpplanner-rs/src/input/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use leafwing_input_manager::{common_conditions::action_just_pressed, prelude::*}
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

use super::super::{
environment::camera::{CameraMovementMode, MainCamera},
movement::{AngularVelocity, Orbit, Velocity},
use super::{
super::{
environment::camera::{CameraMovementMode, MainCamera},
movement::{AngularVelocity, Orbit, Velocity},
},
ChangingBinding,
};
use crate::{
environment::{self, camera::CameraResetEvent},
ui::{ActionBlock, ChangingBinding},
ui::ActionBlock,
};

#[derive(Default)]
Expand Down Expand Up @@ -164,7 +167,7 @@ fn camera_actions(
>,
// mut query_cameras: Query<&mut Camera>,
currently_changing: Res<ChangingBinding>,
action_block: Res<ActionBlock>,
action_block: Option<Res<ActionBlock>>,
mut camera_reset_event: EventWriter<CameraResetEvent>,
sensitivity: Res<CameraSensitivity>,
// mut window: Query<&PrimaryWindow>,
Expand All @@ -189,11 +192,12 @@ fn camera_actions(
if let Ok((action_state, mut velocity, mut angular_velocity, orbit, transform, camera)) =
query.get_single_mut()
{
// if currently_changing.on_cooldown() || currently_changing.is_changing() {
if currently_changing.on_cooldown()
|| currently_changing.is_changing()
|| action_block.is_blocked()
let is_action_blocked =
action_block.is_some() && action_block.as_ref().unwrap().is_blocked();

if currently_changing.on_cooldown() || currently_changing.is_changing() || is_action_blocked
{
info!("Camera actions are blocked");
velocity.value = Vec3::ZERO;
angular_velocity.value = Vec3::ZERO;
return;
Expand Down
Loading

0 comments on commit 60cc763

Please sign in to comment.