Skip to content

Commit

Permalink
bevy: reintroduce bevy trait query on 0.15 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
philiplinden committed Nov 24, 2024
1 parent e08cf94 commit f6a49a9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dev_native = [
[dependencies]
bevy = "0.15.0-rc.3"
avian3d = { git = "https://github.com/Jondolf/avian.git", branch = "bevy-0.15", features = ["debug-plugin"] }
bevy-trait-query = { git = "https://github.com/JoJoJet/bevy-trait-query.git", branch = "bevy-0.15-rc" }
thiserror = "2.0.3"

# -----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion docs/devlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ update them or remove them.

- [x] `avian3d` -> branch `bevy-0.15`
- [ ] ~~`bevy_heavy`~~ remove for now
- [ ] ~~`bevy-trait-query`~~ remove for now
- [x] `bevy-trait-query` -> branch `bevy-0.15-rc`
- [ ] ~~`bevy_common_assets`~~ remove for now
- [ ] ~~`bevy_panorbit_camera`~~ remove for now
- [ ] ~~`iyes_perf_ui`~~ remove for now
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use bevy::{asset::AssetMetaCheck, prelude::*};
/// Re-export for convenience
pub use crate::app3d::{
{ScenePlugin, InterfacePlugins},
controls::{KeyBindingsConfig, CameraControls, DebugControls, TimeControls}
};

pub struct YahsPlugin;
Expand Down
2 changes: 1 addition & 1 deletion src/simulator/balloon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Properties, attributes and functions related to the balloon.
use avian3d::{math::PI, prelude::*};
use avian3d::math::PI;
use bevy::prelude::*;

use super::{
Expand Down
3 changes: 3 additions & 0 deletions src/simulator/forces/aero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use avian3d::{math::PI, prelude::*};
use bevy::prelude::*;
use bevy_trait_query::{self, RegisterExt};

use super::{Atmosphere, Density, ForceUpdateOrder, Force, SimulatedBody};

Expand All @@ -10,6 +11,8 @@ pub struct AeroForcesPlugin;
impl Plugin for AeroForcesPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Drag>();
app.register_component_as::<dyn Force, Drag>();

app.add_systems(Update, update_drag_parameters.in_set(ForceUpdateOrder::Prepare));
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/simulator/forces/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use avian3d::prelude::*;
use bevy::prelude::*;
use bevy_trait_query::{self, RegisterExt};

use super::{Atmosphere, Density, Force, ForceUpdateOrder, Mass, SimulatedBody, Volume};
use crate::simulator::properties::{EARTH_RADIUS_M, STANDARD_G};
Expand All @@ -13,6 +14,9 @@ impl Plugin for BodyForcesPlugin {
app.register_type::<Weight>();
app.register_type::<Buoyancy>();

app.register_component_as::<dyn Force, Weight>();
app.register_component_as::<dyn Force, Buoyancy>();

app.add_systems(
Update,
(update_weight_parameters, update_buoyant_parameters).in_set(ForceUpdateOrder::Prepare),
Expand Down
54 changes: 28 additions & 26 deletions src/simulator/forces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod body;

use avian3d::prelude::*;
use bevy::prelude::*;
use bevy_trait_query;

// Re-expert common forces
#[allow(unused_imports)]
Expand Down Expand Up @@ -34,10 +35,10 @@ impl Plugin for ForcesPlugin {
Update,
on_simulated_body_added.in_set(ForceUpdateOrder::First),
);
// app.add_systems(
// Update,
// update_total_external_force.in_set(ForceUpdateOrder::Apply),
// );
app.add_systems(
Update,
update_total_external_force.in_set(ForceUpdateOrder::Apply),
);

app.add_plugins((aero::AeroForcesPlugin, body::BodyForcesPlugin));
}
Expand Down Expand Up @@ -69,6 +70,7 @@ fn on_simulated_body_added(mut commands: Commands, query: Query<Entity, Added<Si
/// collected and summed to determine the net force acting on a rigid body. All
/// forces assume a right-handed Y-up coordinate frame and are reported in
/// Newtons.
#[bevy_trait_query::queryable]
pub trait Force {
fn name(&self) -> String {
String::from("Force")
Expand All @@ -83,26 +85,26 @@ pub trait Force {
fn point_of_application(&self) -> Vec3;
}

// Set the `ExternalForce` to the sum of all forces in the `Forces` collection.
// This effectively applies all the calculated force vectors to the physics
// rigid body without regard to where the forces came from.
//
// TODO: preserve the position of the total force vector and apply it at that
// point instead of the center of mass.
// fn update_total_external_force(
// mut body_forces: Query<(&mut ExternalForce, &dyn Force, &RigidBody), With<SimulatedBody>>,
// ) {
// // Iterate over each entity that has force vector components.
// for (mut physics_force_component, acting_forces, rigid_body) in body_forces.iter_mut() {
// // Forces only act on dynamic bodies. Don't bother with other kinds.
// if rigid_body.is_dynamic() {
// let mut net_force = Vec3::ZERO; // reset the net force to zero
/// Set the `ExternalForce` to the sum of all forces in the `Forces` collection.
/// This effectively applies all the calculated force vectors to the physics
/// rigid body without regard to where the forces came from.
///
/// TODO: preserve the position of the total force vector and apply it at that
/// point instead of the center of mass.
fn update_total_external_force(
mut body_forces: Query<(&mut ExternalForce, &dyn Force, &RigidBody), With<SimulatedBody>>,
) {
// Iterate over each entity that has force vector components.
for (mut physics_force_component, acting_forces, rigid_body) in body_forces.iter_mut() {
// Forces only act on dynamic bodies. Don't bother with other kinds.
if rigid_body.is_dynamic() {
let mut net_force = Vec3::ZERO; // reset the net force to zero

// // Iterate over each force vector component and compute its value.
// for force in acting_forces.iter() {
// net_force += force.force();
// }
// physics_force_component.set_force(net_force);
// }
// }
// }
// Iterate over each force vector component and compute its value.
for force in acting_forces.iter() {
net_force += force.force();
}
physics_force_component.set_force(net_force);
}
}
}

0 comments on commit f6a49a9

Please sign in to comment.