Skip to content

Commit

Permalink
add an example to access a specific component of RapierContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Oct 14, 2024
1 parent 83c795f commit 78dc64e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bevy_rapier2d/examples/debugdump2.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
File renamed without changes.
61 changes: 61 additions & 0 deletions bevy_rapier3d/examples/rapier_context_component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

fn main() {
App::new()
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::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<DefaultRapierContext>>,
mut exit: EventWriter<AppExit>,
) {
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()
});
}
10 changes: 6 additions & 4 deletions src/plugin/context/systemparams/rapier_context_systemparam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 78dc64e

Please sign in to comment.