From 30cec1bf7b0083c7cdec357ff982744be9857046 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 22 Jan 2025 16:57:45 +0100 Subject: [PATCH 1/2] add ability to opt out of default PhysicsSet organization --- src/plugin/plugin.rs | 56 ++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/src/plugin/plugin.rs b/src/plugin/plugin.rs index f16ef7bf..9c43d88e 100644 --- a/src/plugin/plugin.rs +++ b/src/plugin/plugin.rs @@ -5,6 +5,7 @@ use bevy::ecs::{ schedule::{ScheduleLabel, SystemConfigs}, system::SystemParamItem, }; +use bevy::utils::HashSet; use bevy::{prelude::*, transform::TransformSystem}; use rapier::dynamics::IntegrationParameters; use std::marker::PhantomData; @@ -28,6 +29,7 @@ pub struct RapierPhysicsPlugin { /// to help initializing [`RapierContextInitialization`] resource. /// This will be ignored if that resource already exists. default_world_setup: RapierContextInitialization, + enabled_physics_schedules: HashSet, _phantom: PhantomData, } @@ -82,6 +84,21 @@ where } } + /// Controls whether given `PhysicsSets` systems are injected into the scheduler. + /// + /// This is useful to opt out of default plugin behaviour, for example if you need to reorganize + /// the systems in different schedules. + /// + /// If passing an empty set, the plugin will still add the Physics Sets to the plugin schedule, + /// but no systems will be added automatically. + pub fn set_physics_sets_to_initialize( + mut self, + enabled_physics_schedules: HashSet, + ) -> Self { + self.enabled_physics_schedules = enabled_physics_schedules; + self + } + /// Adds the physics systems to the `FixedUpdate` schedule rather than `PostUpdate`. pub fn in_fixed_schedule(self) -> Self { self.in_schedule(FixedUpdate) @@ -99,8 +116,16 @@ where pub fn get_systems(set: PhysicsSet) -> SystemConfigs { match set { PhysicsSet::SyncBackend => ( - // Run the character controller before the manual transform propagation. - systems::update_character_controls.in_set(PhysicsSet::SyncBackend), + ( + // Initialize the rapier configuration. + // A good candidate for required component or hook components. + // The configuration is needed for following systems, so it should be chained. + setup_rapier_configuration, + // Run the character controller before the manual transform propagation. + systems::update_character_controls, + ) + .chain() + .in_set(PhysicsSet::SyncBackend), // Run Bevy transform propagation additionally to sync [`GlobalTransform`] ( bevy::transform::systems::sync_simple_transforms, @@ -170,6 +195,12 @@ impl Default for RapierPhysicsPlugin(); // Warn user if the timestep mode isn't in Fixed From 817a2183766a840e660b84467e2b6c85f78bc5c7 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Fri, 31 Jan 2025 12:17:23 +0100 Subject: [PATCH 2/2] address feedback --- src/plugin/plugin.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/plugin/plugin.rs b/src/plugin/plugin.rs index 9c43d88e..283f5b4b 100644 --- a/src/plugin/plugin.rs +++ b/src/plugin/plugin.rs @@ -29,6 +29,13 @@ pub struct RapierPhysicsPlugin { /// to help initializing [`RapierContextInitialization`] resource. /// This will be ignored if that resource already exists. default_world_setup: RapierContextInitialization, + /// Controls whether given `PhysicsSets` systems are injected into the scheduler. + /// + /// This is useful to opt out of default plugin behaviour, for example if you need to reorganize + /// the systems in different schedules. + /// + /// If passing an empty set, the plugin will still add the Physics Sets to the plugin schedule, + /// but no systems will be added automatically. enabled_physics_schedules: HashSet, _phantom: PhantomData, } @@ -91,7 +98,7 @@ where /// /// If passing an empty set, the plugin will still add the Physics Sets to the plugin schedule, /// but no systems will be added automatically. - pub fn set_physics_sets_to_initialize( + pub fn with_physics_sets_systems( mut self, enabled_physics_schedules: HashSet, ) -> Self {