From b9f3a6f4958d0ba3fe0d8780db74cd5018dad8e3 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Thu, 8 Aug 2024 16:37:04 +0200 Subject: [PATCH 1/6] add example to output a debugdump of postupdate --- bevy_rapier2d/Cargo.toml | 1 + bevy_rapier2d/examples/debugdump2.rs | 61 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 bevy_rapier2d/examples/debugdump2.rs diff --git a/bevy_rapier2d/Cargo.toml b/bevy_rapier2d/Cargo.toml index b15d611be..69c64c14d 100644 --- a/bevy_rapier2d/Cargo.toml +++ b/bevy_rapier2d/Cargo.toml @@ -64,6 +64,7 @@ approx = "0.5.1" glam = { version = "0.27", features = ["approx"] } bevy-inspector-egui = "0.25.1" bevy_egui = "0.28.0" +bevy_mod_debugdump = "0.11" [package.metadata.docs.rs] # Enable all the features when building the docs on docs.rs diff --git a/bevy_rapier2d/examples/debugdump2.rs b/bevy_rapier2d/examples/debugdump2.rs new file mode 100644 index 000000000..e3b6d687a --- /dev/null +++ b/bevy_rapier2d/examples/debugdump2.rs @@ -0,0 +1,61 @@ +use bevy::prelude::*; +use bevy_rapier2d::prelude::*; + +fn main() { + let mut app = App::new(); + app.insert_resource(ClearColor(Color::srgb( + 0xF9 as f32 / 255.0, + 0xF9 as f32 / 255.0, + 0xFF as f32 / 255.0, + ))) + .add_plugins(( + DefaultPlugins, + RapierPhysicsPlugin::::pixels_per_meter(100.0), + RapierDebugRenderPlugin::default(), + )) + .add_systems(Startup, (setup_graphics, setup_physics)) + .add_systems(PostUpdate, display_events); + + bevy_mod_debugdump::print_schedule_graph(&mut app, PostUpdate); +} + +pub fn setup_graphics(mut commands: Commands) { + commands.spawn(Camera2dBundle::default()); +} + +pub fn display_events( + mut collision_events: EventReader, + mut contact_force_events: EventReader, +) { + for collision_event in collision_events.read() { + println!("Received collision event: {collision_event:?}"); + } + + for contact_force_event in contact_force_events.read() { + println!("Received contact force event: {contact_force_event:?}"); + } +} + +pub fn setup_physics(mut commands: Commands) { + /* + * Ground + */ + commands.spawn(( + TransformBundle::from(Transform::from_xyz(0.0, -24.0, 0.0)), + Collider::cuboid(80.0, 20.0), + )); + + commands.spawn(( + TransformBundle::from(Transform::from_xyz(0.0, 100.0, 0.0)), + Collider::cuboid(80.0, 30.0), + Sensor, + )); + + commands.spawn(( + TransformBundle::from(Transform::from_xyz(0.0, 260.0, 0.0)), + RigidBody::Dynamic, + Collider::cuboid(10.0, 10.0), + ActiveEvents::COLLISION_EVENTS, + ContactForceEventThreshold(10.0), + )); +} From ff031809a8725b5f25ba0e3e3f1ef49820dd148b Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Fri, 6 Sep 2024 10:07:45 +0200 Subject: [PATCH 2/6] better multiple sets for propagate transforms --- src/plugin/plugin.rs | 52 ++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/plugin/plugin.rs b/src/plugin/plugin.rs index 3a5d6b7df..4542ffaea 100644 --- a/src/plugin/plugin.rs +++ b/src/plugin/plugin.rs @@ -80,7 +80,7 @@ where match set { PhysicsSet::SyncBackend => ( // Run the character controller before the manual transform propagation. - systems::update_character_controls, + systems::update_character_controls.in_set(PhysicsSet::SyncBackend), // Run Bevy transform propagation additionally to sync [`GlobalTransform`] ( bevy::transform::systems::sync_simple_transforms, @@ -88,30 +88,37 @@ where ) .chain() .in_set(RapierTransformPropagateSet), - #[cfg(all(feature = "dim3", feature = "async-collider"))] - systems::init_async_scene_colliders, - #[cfg(all(feature = "dim3", feature = "async-collider"))] - systems::init_async_colliders, - systems::init_rigid_bodies, - systems::init_colliders, - systems::init_joints, - systems::sync_removals, - // Run this here so the following systems do not have a 1 frame delay. - apply_deferred, - systems::apply_scale, - systems::apply_collider_user_changes, - systems::apply_rigid_body_user_changes, - systems::apply_joint_user_changes, - systems::apply_initial_rigid_body_impulses, + ( + #[cfg(all(feature = "dim3", feature = "async-collider"))] + systems::init_async_scene_colliders, + #[cfg(all(feature = "dim3", feature = "async-collider"))] + systems::init_async_colliders, + systems::init_rigid_bodies, + systems::init_colliders, + systems::init_joints, + systems::sync_removals, + // Run this here so the following systems do not have a 1 frame delay. + apply_deferred, + systems::apply_scale, + systems::apply_collider_user_changes, + systems::apply_rigid_body_user_changes, + systems::apply_joint_user_changes, + systems::apply_initial_rigid_body_impulses, + ) + .chain() + .in_set(PhysicsSet::SyncBackend), ) .chain() .into_configs(), - PhysicsSet::StepSimulation => (systems::step_simulation::).into_configs(), + PhysicsSet::StepSimulation => (systems::step_simulation::) + .in_set(PhysicsSet::StepSimulation) + .into_configs(), PhysicsSet::Writeback => ( systems::update_colliding_entities, systems::writeback_rigid_bodies, systems::writeback_mass_properties, ) + .in_set(PhysicsSet::Writeback) .into_configs(), } } @@ -214,6 +221,10 @@ where .chain() .before(TransformSystem::TransformPropagate), ); + app.configure_sets( + self.schedule, + RapierTransformPropagateSet.in_set(PhysicsSet::SyncBackend), + ); // These *must* be in the main schedule currently so that they do not miss events. app.add_systems(PostUpdate, (systems::sync_removals,)); @@ -221,10 +232,9 @@ where app.add_systems( self.schedule, ( - Self::get_systems(PhysicsSet::SyncBackend).in_set(PhysicsSet::SyncBackend), - Self::get_systems(PhysicsSet::StepSimulation) - .in_set(PhysicsSet::StepSimulation), - Self::get_systems(PhysicsSet::Writeback).in_set(PhysicsSet::Writeback), + Self::get_systems(PhysicsSet::SyncBackend), + Self::get_systems(PhysicsSet::StepSimulation), + Self::get_systems(PhysicsSet::Writeback), ), ); From 16c3e3f26dc636686c6cbdd930a718340b531283 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Fri, 6 Sep 2024 10:52:10 +0200 Subject: [PATCH 3/6] allow system race for writeback --- src/plugin/plugin.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/plugin/plugin.rs b/src/plugin/plugin.rs index 4542ffaea..741d52127 100644 --- a/src/plugin/plugin.rs +++ b/src/plugin/plugin.rs @@ -116,7 +116,8 @@ where PhysicsSet::Writeback => ( systems::update_colliding_entities, systems::writeback_rigid_bodies, - systems::writeback_mass_properties, + // Each writeback write to different properties. + systems::writeback_mass_properties.ambiguous_with(systems::writeback_rigid_bodies), ) .in_set(PhysicsSet::Writeback) .into_configs(), From b669fe2da61f33c5da2a348725a740218c4b0135 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Fri, 6 Sep 2024 10:57:42 +0200 Subject: [PATCH 4/6] remove ambiguity from example --- bevy_rapier2d/examples/debugdump2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bevy_rapier2d/examples/debugdump2.rs b/bevy_rapier2d/examples/debugdump2.rs index e3b6d687a..ee8dd12ed 100644 --- a/bevy_rapier2d/examples/debugdump2.rs +++ b/bevy_rapier2d/examples/debugdump2.rs @@ -14,7 +14,7 @@ fn main() { RapierDebugRenderPlugin::default(), )) .add_systems(Startup, (setup_graphics, setup_physics)) - .add_systems(PostUpdate, display_events); + .add_systems(PostUpdate, display_events.after(PhysicsSet::StepSimulation)); bevy_mod_debugdump::print_schedule_graph(&mut app, PostUpdate); } From 310ab561f4f4968a976c663ecfd327f3c1e2c4b3 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 10 Sep 2024 10:05:54 +0200 Subject: [PATCH 5/6] filter out some less relevant systems --- bevy_rapier2d/examples/debugdump2.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/bevy_rapier2d/examples/debugdump2.rs b/bevy_rapier2d/examples/debugdump2.rs index ee8dd12ed..389d86b9f 100644 --- a/bevy_rapier2d/examples/debugdump2.rs +++ b/bevy_rapier2d/examples/debugdump2.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use bevy_mod_debugdump::{schedule_graph, schedule_graph_dot}; use bevy_rapier2d::prelude::*; fn main() { @@ -16,7 +17,22 @@ fn main() { .add_systems(Startup, (setup_graphics, setup_physics)) .add_systems(PostUpdate, display_events.after(PhysicsSet::StepSimulation)); - bevy_mod_debugdump::print_schedule_graph(&mut app, PostUpdate); + let mut debugdump_settings = schedule_graph::Settings::default(); + // Filter out some less relevant systems. + debugdump_settings.include_system = + Some(Box::new(|system: &(dyn System)| { + if system.name().starts_with("bevy_pbr") + || system.name().starts_with("bevy_render") + || system.name().starts_with("bevy_gizmos") + || system.name().starts_with("bevy_winit") + || system.name().starts_with("bevy_sprite") + { + return dbg!(false); + } + true + })); + let dot = schedule_graph_dot(&mut app, PostUpdate, &debugdump_settings); + println!("{dot}"); } pub fn setup_graphics(mut commands: Commands) { From e32c4a660a16936e9e58a96f097297d80fc4225a Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 10 Sep 2024 10:10:15 +0200 Subject: [PATCH 6/6] remove dbg --- bevy_rapier2d/examples/debugdump2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bevy_rapier2d/examples/debugdump2.rs b/bevy_rapier2d/examples/debugdump2.rs index 389d86b9f..ed294a1ed 100644 --- a/bevy_rapier2d/examples/debugdump2.rs +++ b/bevy_rapier2d/examples/debugdump2.rs @@ -27,7 +27,7 @@ fn main() { || system.name().starts_with("bevy_winit") || system.name().starts_with("bevy_sprite") { - return dbg!(false); + return false; } true }));