From 9d9d379ee934b68cc90c12dd2ebc4253ccf9b701 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Mon, 19 Jun 2023 14:29:05 -0700 Subject: [PATCH 1/4] Allow customizing the schedule with FixedUpdate --- src/plugin/plugin.rs | 50 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/plugin/plugin.rs b/src/plugin/plugin.rs index 2b808a95..c9f21911 100644 --- a/src/plugin/plugin.rs +++ b/src/plugin/plugin.rs @@ -14,6 +14,7 @@ pub type NoUserData = (); /// This will automatically setup all the resources needed to run a physics simulation with the /// Rapier physics engine. pub struct RapierPhysicsPlugin { + schedule: Box, physics_scale: f32, default_system_setup: bool, _phantom: PhantomData, @@ -52,10 +53,16 @@ where Self { physics_scale: pixels_per_meter, default_system_setup: true, - _phantom: PhantomData, + ..default() } } + /// Adds the physics systems to the `FixedUpdate` schedule rather than main. + pub fn fixed(mut self) -> Self { + self.schedule = Box::new(CoreSchedule::FixedUpdate); + self + } + /// Provided for use when staging systems outside of this plugin using /// [`with_system_setup(false)`](Self::with_system_setup). /// See [`PhysicsSet`] for a description of these systems. @@ -121,6 +128,7 @@ pub struct RapierTransformPropagateSet; impl Default for RapierPhysicsPlugin { fn default() -> Self { Self { + schedule: Box::new(CoreSchedule::Main), physics_scale: 1.0, default_system_setup: true, _phantom: PhantomData, @@ -191,6 +199,7 @@ where // Add each set as necessary if self.default_system_setup { +<<<<<<< HEAD app.configure_sets( PostUpdate, ( @@ -213,6 +222,45 @@ where .in_set(PhysicsSet::StepSimulation), Self::get_systems(PhysicsSet::Writeback).in_set(PhysicsSet::Writeback), ), +======= + app.world + .resource_mut::() + .get_mut(&self.schedule.clone()) + .expect("Expected schedule to exist") + .configure_sets( + ( + PhysicsSet::SyncBackend, + PhysicsSet::SyncBackendFlush, + PhysicsSet::StepSimulation, + PhysicsSet::Writeback, + ) + .chain() + .after(CoreSet::UpdateFlush) + .before(CoreSet::PostUpdate), + ); + + app.add_system(systems::sync_removals.in_base_set(CoreSet::PostUpdate)); + + app.add_systems( + Self::get_systems(PhysicsSet::SyncBackend) + .in_base_set(PhysicsSet::SyncBackend) + .in_schedule(self.schedule.clone()), + ); + app.add_systems( + Self::get_systems(PhysicsSet::SyncBackendFlush) + .in_base_set(PhysicsSet::SyncBackendFlush) + .in_schedule(self.schedule.clone()), + ); + app.add_systems( + Self::get_systems(PhysicsSet::StepSimulation) + .in_base_set(PhysicsSet::StepSimulation) + .in_schedule(self.schedule.clone()), + ); + app.add_systems( + Self::get_systems(PhysicsSet::Writeback) + .in_base_set(PhysicsSet::Writeback) + .in_schedule(self.schedule.clone()), +>>>>>>> 7c99ef2... Allow customizing the schedule with FixedUpdate ); } } From 346f8ad8c3e55c1cf49be4b021fb1cb14cdd8209 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Wed, 26 Jul 2023 04:11:06 -0700 Subject: [PATCH 2/4] Wartn user on bad timestepmode --- src/plugin/plugin.rs | 77 +++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 47 deletions(-) diff --git a/src/plugin/plugin.rs b/src/plugin/plugin.rs index c9f21911..f470e361 100644 --- a/src/plugin/plugin.rs +++ b/src/plugin/plugin.rs @@ -2,7 +2,11 @@ use crate::pipeline::{CollisionEvent, ContactForceEvent}; use crate::plugin::configuration::SimulationToRenderTime; use crate::plugin::{systems, RapierConfiguration, RapierContext}; use crate::prelude::*; -use bevy::ecs::{event::Events, schedule::SystemConfigs, system::SystemParamItem}; +use bevy::ecs::{ + event::Events, + schedule::{ScheduleLabel, SystemConfigs}, + system::SystemParamItem, +}; use bevy::{prelude::*, transform::TransformSystem}; use std::marker::PhantomData; @@ -57,9 +61,14 @@ where } } - /// Adds the physics systems to the `FixedUpdate` schedule rather than main. - pub fn fixed(mut self) -> Self { - self.schedule = Box::new(CoreSchedule::FixedUpdate); + /// Adds the physics systems to the `FixedUpdate` schedule rather than `PostUpdate`. + pub fn fixed(self) -> Self { + self.in_schedule(FixedUpdate) + } + + /// Adds the physics systems to the provided schedule rather than `PostUpdate`. + pub fn in_schedule(mut self, schedule: impl ScheduleLabel) -> Self { + self.schedule = Box::new(schedule); self } @@ -128,7 +137,7 @@ pub struct RapierTransformPropagateSet; impl Default for RapierPhysicsPlugin { fn default() -> Self { Self { - schedule: Box::new(CoreSchedule::Main), + schedule: Box::new(PostUpdate), physics_scale: 1.0, default_system_setup: true, _phantom: PhantomData, @@ -199,9 +208,8 @@ where // Add each set as necessary if self.default_system_setup { -<<<<<<< HEAD app.configure_sets( - PostUpdate, + self.schedule.clone(), ( PhysicsSet::SyncBackend, PhysicsSet::SyncBackendFlush, @@ -212,8 +220,11 @@ where .before(TransformSystem::TransformPropagate), ); + // These *must* be in the main schedule currently so that they do not miss events. + app.add_systems(PostUpdate, (systems::sync_removals,)); + app.add_systems( - PostUpdate, + self.schedule.clone(), ( Self::get_systems(PhysicsSet::SyncBackend).in_set(PhysicsSet::SyncBackend), Self::get_systems(PhysicsSet::SyncBackendFlush) @@ -222,46 +233,18 @@ where .in_set(PhysicsSet::StepSimulation), Self::get_systems(PhysicsSet::Writeback).in_set(PhysicsSet::Writeback), ), -======= - app.world - .resource_mut::() - .get_mut(&self.schedule.clone()) - .expect("Expected schedule to exist") - .configure_sets( - ( - PhysicsSet::SyncBackend, - PhysicsSet::SyncBackendFlush, - PhysicsSet::StepSimulation, - PhysicsSet::Writeback, - ) - .chain() - .after(CoreSet::UpdateFlush) - .before(CoreSet::PostUpdate), - ); - - app.add_system(systems::sync_removals.in_base_set(CoreSet::PostUpdate)); - - app.add_systems( - Self::get_systems(PhysicsSet::SyncBackend) - .in_base_set(PhysicsSet::SyncBackend) - .in_schedule(self.schedule.clone()), - ); - app.add_systems( - Self::get_systems(PhysicsSet::SyncBackendFlush) - .in_base_set(PhysicsSet::SyncBackendFlush) - .in_schedule(self.schedule.clone()), - ); - app.add_systems( - Self::get_systems(PhysicsSet::StepSimulation) - .in_base_set(PhysicsSet::StepSimulation) - .in_schedule(self.schedule.clone()), - ); - app.add_systems( - Self::get_systems(PhysicsSet::Writeback) - .in_base_set(PhysicsSet::Writeback) - .in_schedule(self.schedule.clone()), ->>>>>>> 7c99ef2... Allow customizing the schedule with FixedUpdate ); + + // Warn user if the timestep mode isn't in Fixed + if self.schedule.as_dyn_eq().dyn_eq(FixedUpdate.as_dyn_eq()) { + let config = app.world.resource::(); + match config.timestep_mode { + TimestepMode::Fixed { .. } => {} + mode => { + warn!("TimestepMode is set to `{:?}`, it is recommended to use `TimestepMode::Fixed` if you have the physics in `FixedUpdate`", mode); + } + } + } } } } From 3cb7b9ff966b6477792ac8c055177f17e73c13eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 6 Aug 2023 13:56:42 +0200 Subject: [PATCH 3/4] Rename RapierPhysicsPlugin::fixed to ::in_fixed_schedule --- src/plugin/plugin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugin/plugin.rs b/src/plugin/plugin.rs index f470e361..9726dc6b 100644 --- a/src/plugin/plugin.rs +++ b/src/plugin/plugin.rs @@ -62,7 +62,7 @@ where } /// Adds the physics systems to the `FixedUpdate` schedule rather than `PostUpdate`. - pub fn fixed(self) -> Self { + pub fn in_fixed_schedule(self) -> Self { self.in_schedule(FixedUpdate) } From b3cf412e439c95513e50ecfe75b1cb4df4520447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 6 Aug 2023 14:09:44 +0200 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1204da4c..bb27d915 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ `TypedShape` or `SharedShape`. The `From` trait has also been implemented accordingly. - Implement `Copy` for `ColliderView` and all the other non-mut shape views. - Add `RapierContext::rigid_body_colliders` to retrieve all collider entities attached to this rigid-body. +- Add `RapierPhysicsPlugin::in_fixed_schedule`/`::in_schedude` to add rapier’s systems to a fixed/custom + schedule. ## 0.22.0 (10 July 2023) ### Modified