From 78dc64e05890a8888c711b6f8d84c26865a42cc5 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Mon, 14 Oct 2024 17:20:28 +0200 Subject: [PATCH] add an example to access a specific component of RapierContext --- bevy_rapier2d/examples/debugdump2.rs | 2 +- .../{multi_world3.rs => multi_contexts3.rs} | 0 .../examples/rapier_context_component.rs | 61 +++++++++++++++++++ .../rapier_context_systemparam.rs | 10 +-- 4 files changed, 68 insertions(+), 5 deletions(-) rename bevy_rapier3d/examples/{multi_world3.rs => multi_contexts3.rs} (100%) create mode 100644 bevy_rapier3d/examples/rapier_context_component.rs diff --git a/bevy_rapier2d/examples/debugdump2.rs b/bevy_rapier2d/examples/debugdump2.rs index 190a5079..4d133d91 100644 --- a/bevy_rapier2d/examples/debugdump2.rs +++ b/bevy_rapier2d/examples/debugdump2.rs @@ -1,6 +1,6 @@ //! Example using bevy_mod_debugdump to output a graph of systems execution order. //! run with: -//! `cargo run --example debugdump2 > dump.dot && dot -Tsvg dump.dot > dump.svg` +//! `cargo run --example debugdump2 > dump.dot && dot -Tsvg dump.dot > dump.svg` use bevy::prelude::*; use bevy_mod_debugdump::{schedule_graph, schedule_graph_dot}; diff --git a/bevy_rapier3d/examples/multi_world3.rs b/bevy_rapier3d/examples/multi_contexts3.rs similarity index 100% rename from bevy_rapier3d/examples/multi_world3.rs rename to bevy_rapier3d/examples/multi_contexts3.rs diff --git a/bevy_rapier3d/examples/rapier_context_component.rs b/bevy_rapier3d/examples/rapier_context_component.rs new file mode 100644 index 00000000..156793a2 --- /dev/null +++ b/bevy_rapier3d/examples/rapier_context_component.rs @@ -0,0 +1,61 @@ +use bevy::prelude::*; +use bevy_rapier3d::prelude::*; + +fn main() { + App::new() + .add_plugins(( + DefaultPlugins, + RapierPhysicsPlugin::::default(), + RapierDebugRenderPlugin::default(), + )) + .add_systems(Startup, (setup_physics, setup_graphics)) + .add_systems(PostUpdate, display_nb_colliders) + .run(); +} + +/// Demonstrates how to access a more specific component of [`RapierContext`] +fn display_nb_colliders( + query_context: Query<&RapierContextColliders, With>, + mut exit: EventWriter, +) { + let nb_colliders = query_context.single().colliders.len(); + println!("There are {nb_colliders} colliders."); + if nb_colliders > 0 { + exit.send(AppExit::Success); + } +} + +pub fn setup_physics(mut commands: Commands) { + /* + * Ground + */ + let ground_size = 5.1; + let ground_height = 0.1; + + let starting_y = -0.5 - ground_height; + + commands.spawn(( + TransformBundle::from(Transform::from_xyz(0.0, starting_y, 0.0)), + Collider::cuboid(ground_size, ground_height, ground_size), + )); + + for _ in 0..3 { + /* + * Create the cubes + */ + + commands.spawn(( + TransformBundle::default(), + RigidBody::Dynamic, + Collider::cuboid(0.5, 0.5, 0.5), + )); + } +} + +fn setup_graphics(mut commands: Commands) { + commands.spawn(Camera3dBundle { + transform: Transform::from_xyz(0.0, 3.0, -10.0) + .looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y), + ..Default::default() + }); +} diff --git a/src/plugin/context/systemparams/rapier_context_systemparam.rs b/src/plugin/context/systemparams/rapier_context_systemparam.rs index 5386f4cd..91adcc3c 100644 --- a/src/plugin/context/systemparams/rapier_context_systemparam.rs +++ b/src/plugin/context/systemparams/rapier_context_systemparam.rs @@ -4,16 +4,18 @@ use bevy::prelude::*; use rapier::prelude::Real; pub(crate) const RAPIER_CONTEXT_EXPECT_ERROR: &str = - "RapierContextEntityLink.0 refers to an entity without RapierContext."; + "RapierContextEntityLink.0 refers to an entity missing components from RapierContextBundle."; use crate::{ plugin::context::{ - RapierContextColliders, RapierContextJoints, RapierQueryPipeline, RapierRigidBodySet, + DefaultRapierContext, RapierContextColliders, RapierContextJoints, RapierContextSimulation, + RapierQueryPipeline, RapierRigidBodySet, }, prelude::QueryFilter, }; -use super::super::{DefaultRapierContext, RapierContextSimulation}; +#[cfg(doc)] +use crate::prelude::RapierContextBundle; /// Utility [`SystemParam`] to easily access every required components of a [`RapierContext`] immutably. /// @@ -58,7 +60,7 @@ impl<'w, 's, T: query::QueryFilter + 'static> ReadRapierContext<'w, 's, T> { /// A helper struct to avoid passing too many parameters to most rapier functions. /// This helps with reducing boilerplate, at the (small) price of maybe getting too much information from the ECS. /// -/// Note: This is not a component, refer to [`ReadRapierContext`] or [`WriteRapierContext`] +/// Note: This is not a component, refer to [`ReadRapierContext`], [`WriteRapierContext`], or [`RapierContextBundle`] #[derive(query::QueryData)] pub struct RapierContext<'a> { /// The Rapier context, containing all the state of the physics engine.