From f190786d1a0e789c77112473e13f41d63067f44d Mon Sep 17 00:00:00 2001 From: Philip Linden Date: Fri, 10 Jan 2025 00:48:08 -0500 Subject: [PATCH] forces: drop bevy-trait-query dependency --- Cargo.toml | 1 - crates/yahs-ui/Cargo.toml | 2 +- crates/yahs-ui/src/dev_tools.rs | 377 ++++---------------------------- crates/yahs-ui/src/gizmos.rs | 83 ------- crates/yahs-ui/src/main.rs | 5 +- crates/yahs-ui/src/scene.rs | 13 -- crates/yahs/Cargo.toml | 1 - crates/yahs/src/forces/aero.rs | 4 +- crates/yahs/src/forces/body.rs | 10 +- crates/yahs/src/forces/mod.rs | 36 +-- crates/yahs/src/time.rs | 8 +- 11 files changed, 61 insertions(+), 479 deletions(-) delete mode 100644 crates/yahs-ui/src/gizmos.rs diff --git a/Cargo.toml b/Cargo.toml index 2cb97da..70ba9fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,6 @@ bevy = { version = "0.15", default-features = false, features = [ "bevy_state", "multi_threaded" ] } avian3d = "0.2" -bevy-trait-query = "0.7" # Compile with Performance Optimizations: # https://bevyengine.org/learn/quick-start/getting-started/setup/#compile-with-performance-optimizations diff --git a/crates/yahs-ui/Cargo.toml b/crates/yahs-ui/Cargo.toml index 49f39ff..2099efc 100644 --- a/crates/yahs-ui/Cargo.toml +++ b/crates/yahs-ui/Cargo.toml @@ -33,7 +33,7 @@ bevy-inspector-egui = { version = "0.28", optional = true, features = [ ] } [features] -default = [] +default = ["dev"] dev = [ "avian3d/debug-plugin", "bevy/bevy_dev_tools", diff --git a/crates/yahs-ui/src/dev_tools.rs b/crates/yahs-ui/src/dev_tools.rs index 4f43e84..b962361 100644 --- a/crates/yahs-ui/src/dev_tools.rs +++ b/crates/yahs-ui/src/dev_tools.rs @@ -1,368 +1,77 @@ -#[allow(unused_imports)] use bevy::{ color::palettes::basic::*, - dev_tools::{states::log_transitions, fps_overlay::{FpsOverlayPlugin, FpsOverlayConfig}}, - diagnostic::*, - input::common_conditions::input_just_pressed, prelude::*, - time::common_conditions::on_timer, - - text::FontSmoothing, }; -use avian3d::debug_render::*; -use super::controls::KeyBindingsConfig; -use yahs_simulator::prelude::{Force, SimState}; - -// Define all Show and Hide events -#[derive(Event)] -struct ShowEngineDebug; - -#[derive(Event)] -struct HideEngineDebug; - -#[derive(Event)] -struct ShowWireframe; - -#[derive(Event)] -struct HideWireframe; - -#[derive(Event)] -struct ShowForceGizmos; - -#[derive(Event)] -struct HideForceGizmos; - -#[derive(Event)] -struct ShowPhysicsGizmos; - -#[derive(Event)] -struct HidePhysicsGizmos; +use yahs::prelude::{Balloon, Force, Weight, Buoyancy, Drag}; pub struct DevToolsPlugin; impl Plugin for DevToolsPlugin { fn build(&self, app: &mut App) { app.add_plugins(( - FrameTimeDiagnosticsPlugin, - EntityCountDiagnosticsPlugin, - RenderedDevToolsPlugin, - )); - - app.add_systems(Update, log_transitions::); - } -} - -struct RenderedDevToolsPlugin; - -impl Plugin for RenderedDevToolsPlugin { - fn build(&self, app: &mut App) { - app.add_plugins(( - PhysicsDebugPlugin::default(), - ForceArrowsPlugin, - #[cfg(not(target_arch = "wasm32"))] - bevy::pbr::wireframe::WireframePlugin, - FpsOverlayPlugin { - config: FpsOverlayConfig { - text_config: TextFont { - // Here we define size of our overlay - font_size: 14.0, - // If we want, we can use a custom font - font: default(), - // We could also disable font smoothing, - font_smoothing: FontSmoothing::default(), - }, - // We can also change color of the overlay - text_color: Color::srgb(0.0, 1.0, 0.0), - enabled: false, - }, - }, + KinematicsGizmos, )); - - app.init_resource::(); - - // Register all events - app.add_event::(); - app.add_event::(); - app.add_event::(); - app.add_event::(); - app.add_event::(); - app.add_event::(); - - app.add_systems(Startup, init_debug_ui); - app.add_systems(Update, trigger_debug_ui); - - // Register handler functions as observers - app.add_observer(show_engine_debug); - app.add_observer(show_force_gizmos); - app.add_observer(show_physics_gizmos); - #[cfg(not(target_arch = "wasm32"))] - { - app.add_event::(); - app.add_event::(); - app.add_observer(show_wireframe); - app.add_observer(hide_wireframe); - } - - app.add_observer(hide_engine_debug); - app.add_observer(hide_force_gizmos); - app.add_observer(hide_physics_gizmos); - - #[cfg(feature = "inspect")] - { - use bevy_inspector_egui::quick; - app.add_plugins(( - quick::FilterQueryInspectorPlugin::default(), - quick::ResourceInspectorPlugin::default(), - quick::StateInspectorPlugin::default(), - quick::WorldInspectorPlugin::default(), - quick::AssetInspectorPlugin::::default(), - quick::AssetInspectorPlugin::::default(), - )); - } - } -} - -#[derive(Debug, Resource)] -struct DebugState { - engine: bool, - wireframe: bool, - forces: bool, - physics: bool, -} - -impl Default for DebugState { - fn default() -> Self { - Self { - engine: true, - wireframe: false, - forces: true, - physics: false, - } - } -} - -impl DebugState { - fn toggle_engine(&mut self) { - self.engine = !self.engine; - } - fn toggle_wireframe(&mut self) { - self.wireframe = !self.wireframe; - } - fn toggle_forces(&mut self) { - self.forces = !self.forces; - } - fn toggle_physics(&mut self) { - self.physics = !self.physics; - } -} - -fn init_debug_ui(mut commands: Commands, debug_state: Res) { - info!("initializing debug ui"); - if debug_state.engine { - commands.trigger(ShowEngineDebug); } - #[cfg(not(target_arch = "wasm32"))] - if debug_state.wireframe { - commands.trigger(ShowWireframe); - } - if debug_state.forces { - commands.trigger(ShowForceGizmos); - } - if debug_state.physics { - commands.trigger(ShowPhysicsGizmos); - } -} - -fn trigger_debug_ui( - mut commands: Commands, - mut debug_state: ResMut, - key_input: Res>, - key_bindings: Res, -) { - if key_input.just_pressed(key_bindings.debug_controls.toggle_1) { - debug_state.toggle_engine(); - if debug_state.engine { - commands.trigger(ShowEngineDebug); - } else { - commands.trigger(HideEngineDebug); - } - } - #[cfg(not(target_arch = "wasm32"))] - if key_input.just_pressed(key_bindings.debug_controls.toggle_2) { - debug_state.toggle_wireframe(); - // Wireframe doesn't work on WASM - if debug_state.wireframe { - commands.trigger(ShowWireframe); - } else { - commands.trigger(HideWireframe); - } - } - if key_input.just_pressed(key_bindings.debug_controls.toggle_3) { - debug_state.toggle_forces(); - if debug_state.forces { - commands.trigger(ShowForceGizmos); - } else { - commands.trigger(HideForceGizmos); - } - } - if key_input.just_pressed(key_bindings.debug_controls.toggle_4) { - debug_state.toggle_physics(); - if debug_state.physics { - commands.trigger(ShowPhysicsGizmos); - } else { - commands.trigger(HidePhysicsGizmos); - } - } -} - -fn show_engine_debug( - _trigger: Trigger, - mut store: ResMut, - mut overlay_config: ResMut, -) { - for diag in store.iter_mut() { - info!("showing diagnostic {}", diag.path()); - diag.is_enabled = true; - } - overlay_config.enabled = true; -} - -fn hide_engine_debug( - _trigger: Trigger, - mut store: ResMut, - mut overlay_config: ResMut, -) { - for diag in store.iter_mut() { - info!("hiding diagnostic {}", diag.path()); - diag.is_enabled = false; - } - overlay_config.enabled = false; -} - -fn show_wireframe( - _trigger: Trigger, - mut wireframe_config: ResMut, -) { - info!("showing wireframe"); - wireframe_config.global = true; -} - -fn hide_wireframe( - _trigger: Trigger, - mut wireframe_config: ResMut, -) { - info!("hiding wireframe"); - wireframe_config.global = false; -} - -fn show_force_gizmos( - _trigger: Trigger, - mut store: ResMut, -) { - info!("showing force gizmos"); - let (_, force_config) = store.config_mut::(); - *force_config = ForceGizmos::all(); -} - -fn hide_force_gizmos( - _trigger: Trigger, - mut store: ResMut, -) { - info!("hiding force gizmos"); - let (_, force_config) = store.config_mut::(); - *force_config = ForceGizmos::none(); -} - -fn show_physics_gizmos( - _trigger: Trigger, - mut store: ResMut, -) { - info!("showing physics gizmos"); - let (_, physics_config) = store.config_mut::(); - *physics_config = PhysicsGizmos::all(); -} - -fn hide_physics_gizmos( - _trigger: Trigger, - mut store: ResMut, -) { - info!("hiding physics gizmos"); - let (_, physics_config) = store.config_mut::(); - *physics_config = PhysicsGizmos::none(); } const ARROW_SCALE: f32 = 0.1; -pub struct ForceArrowsPlugin; +pub struct KinematicsGizmos; -impl Plugin for ForceArrowsPlugin { +impl Plugin for KinematicsGizmos { fn build(&self, app: &mut App) { - app.init_gizmo_group::(); - app.register_type::(); app.add_systems( PostUpdate, - force_arrows - .run_if(|store: Res| store.config::().0.enabled), + (force_arrows, orientation_indicator, position_reference) ); } } -fn force_arrows(query: Query<&dyn Force>, mut gizmos: Gizmos) { - for forces in query.iter() { - for force in forces.iter() { - let start = force.point_of_application(); - let end = start + force.force() * ARROW_SCALE; - let color = match force.color() { - Some(c) => c, +fn force_arrows(weights: Query<&Weight>, buoys: Query<&Buoyancy>, drags: Query<&Drag>, mut gizmos: Gizmos) { + for weight in weights.iter() { + let start = weight.point_of_application(); + let end = start + weight.force() * ARROW_SCALE; + let color = match weight.color() { + Some(c) => c, None => RED.into(), }; - gizmos.arrow(start, end, color).with_tip_length(0.3); - } + gizmos.arrow(start, end, color).with_tip_length(0.3); } -} - -#[derive(Reflect, GizmoConfigGroup)] -pub struct ForceGizmos { - /// The scale of the force arrows. - pub arrow_scale: Option, - /// The color of the force arrows. If `None`, the arrows will not be rendered. - pub arrow_color: Option, - /// The length of the arrow tips. - pub tip_length: Option, - /// Determines if the forces should be hidden when not active. - pub enabled: bool, -} - -impl Default for ForceGizmos { - fn default() -> Self { - Self { - arrow_scale: Some(0.1), - arrow_color: Some(RED.into()), - tip_length: Some(0.3), - enabled: false, - } + for buoyancy in buoys.iter() { + let start = buoyancy.point_of_application(); + let end = start + buoyancy.force() * ARROW_SCALE; + + let color = match buoyancy.color() { + Some(c) => c, + None => BLUE.into(), + }; + gizmos.arrow(start, end, color).with_tip_length(0.3); + } + for drag in drags.iter() { + let start = drag.point_of_application(); + let end = start + drag.force() * ARROW_SCALE; + let color = match drag.color() { + Some(c) => c, + None => GREEN.into(), + }; + gizmos.arrow(start, end, color).with_tip_length(0.3); } } -impl ForceGizmos { - /// Creates a [`ForceGizmos`] configuration with all rendering options enabled. - pub fn all() -> Self { - Self { - arrow_scale: Some(0.1), - arrow_color: Some(RED.into()), - tip_length: Some(0.3), - enabled: true, - } +fn orientation_indicator(query: Query<&Transform, With>, mut gizmos: Gizmos) { + for transform in query.iter() { + gizmos.cross(transform.translation, 2.0, LIME); } +} - /// Creates a [`ForceGizmos`] configuration with all rendering options disabled. - pub fn none() -> Self { - Self { - arrow_scale: None, - arrow_color: None, - tip_length: None, - enabled: false, - } - } +/// A grid pinned to the world frame of reference. It is set back a little bit +/// so it doesn't z-fight with other gizmos. +fn position_reference(mut gizmos: Gizmos) { + gizmos.grid( + Isometry3d::new(Vec3::new(0.0, 0.0, -1.0), Quat::IDENTITY), + UVec2::splat(20), + Vec2::new(2., 2.), + LinearRgba::gray(0.65), + ); } diff --git a/crates/yahs-ui/src/gizmos.rs b/crates/yahs-ui/src/gizmos.rs deleted file mode 100644 index 04855de..0000000 --- a/crates/yahs-ui/src/gizmos.rs +++ /dev/null @@ -1,83 +0,0 @@ -use bevy::{color::palettes::basic::*, prelude::*}; - -use yahs::forces::Force; - -const ARROW_SCALE: f32 = 0.1; - -pub struct ForceArrowsPlugin; - -impl Plugin for ForceArrowsPlugin { - fn build(&self, app: &mut App) { - app.init_gizmo_group::(); - app.register_type::(); - app.add_systems( - PostUpdate, - force_arrows.run_if( - |store: Res| { - store.config::().0.enabled - }), - ); - } -} - -fn force_arrows( - query: Query<&dyn Force>, - mut gizmos: Gizmos, -) { - for forces in query.iter() { - for force in forces.iter() { - let start = force.point_of_application(); - let end = start + force.force() * ARROW_SCALE; - let color = match force.color() { - Some(c) => c, - None => RED.into(), - }; - gizmos.arrow(start, end, color).with_tip_length(0.3); - } - } -} - -#[derive(Reflect, GizmoConfigGroup)] -pub struct ForceGizmos { - /// The scale of the force arrows. - pub arrow_scale: Option, - /// The color of the force arrows. If `None`, the arrows will not be rendered. - pub arrow_color: Option, - /// The length of the arrow tips. - pub tip_length: Option, - /// Determines if the forces should be hidden when not active. - pub enabled: bool, -} - -impl Default for ForceGizmos { - fn default() -> Self { - Self { - arrow_scale: Some(0.1), - arrow_color: Some(RED.into()), - tip_length: Some(0.3), - enabled: false, - } - } -} - -impl ForceGizmos { - /// Creates a [`ForceGizmos`] configuration with all rendering options enabled. - pub fn all() -> Self { - Self { - arrow_scale: Some(0.1), - arrow_color: Some(RED.into()), - tip_length: Some(0.3), - enabled: true, - } - } - - /// Creates a [`ForceGizmos`] configuration with debug rendering enabled but all options turned off. - pub fn none() -> Self { - Self { - arrow_scale: None, - arrow_color: None, - tip_length: None, - enabled: false, - } - } -} diff --git a/crates/yahs-ui/src/main.rs b/crates/yahs-ui/src/main.rs index b0e5598..1d799bb 100644 --- a/crates/yahs-ui/src/main.rs +++ b/crates/yahs-ui/src/main.rs @@ -11,9 +11,6 @@ use camera::CameraPlugin; use controls::ControlsPlugin; use scene::ScenePlugin; -#[cfg(feature = "dev")] -use dev_tools::DevToolsPlugin; - use bevy::{ prelude::*, asset::AssetMetaCheck, @@ -45,7 +42,7 @@ fn main() { ScenePlugin, CameraPlugin, #[cfg(feature = "dev")] - DevToolsPlugin, + dev_tools::DevToolsPlugin, )) .run(); } diff --git a/crates/yahs-ui/src/scene.rs b/crates/yahs-ui/src/scene.rs index e990914..c9d38be 100644 --- a/crates/yahs-ui/src/scene.rs +++ b/crates/yahs-ui/src/scene.rs @@ -9,10 +9,6 @@ pub struct ScenePlugin; impl Plugin for ScenePlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, (setup_lighting, spawn_balloon)); - app.add_systems(Update, animate_light_direction); - // app.add_systems(PostStartup, |mut commands: Commands| { - // commands.set_state(SimState::Running); - // }); } } @@ -65,12 +61,3 @@ fn setup_lighting(mut commands: Commands) { brightness: 1.0, }); } - -fn animate_light_direction( - time: Res