Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing the schedule with FixedUpdate #385

Merged
merged 4 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 35 additions & 4 deletions src/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,6 +18,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<PhysicsHooks = ()> {
schedule: Box<dyn ScheduleLabel>,
physics_scale: f32,
default_system_setup: bool,
_phantom: PhantomData<PhysicsHooks>,
Expand Down Expand Up @@ -52,10 +57,21 @@ where
Self {
physics_scale: pixels_per_meter,
default_system_setup: true,
_phantom: PhantomData,
..default()
}
}

/// Adds the physics systems to the `FixedUpdate` schedule rather than `PostUpdate`.
pub fn in_fixed_schedule(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
}

/// 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.
Expand Down Expand Up @@ -121,6 +137,7 @@ pub struct RapierTransformPropagateSet;
impl<PhysicsHooksSystemParam> Default for RapierPhysicsPlugin<PhysicsHooksSystemParam> {
fn default() -> Self {
Self {
schedule: Box::new(PostUpdate),
physics_scale: 1.0,
default_system_setup: true,
_phantom: PhantomData,
Expand Down Expand Up @@ -192,7 +209,7 @@ where
// Add each set as necessary
if self.default_system_setup {
app.configure_sets(
PostUpdate,
self.schedule.clone(),
(
PhysicsSet::SyncBackend,
PhysicsSet::SyncBackendFlush,
Expand All @@ -203,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)
Expand All @@ -214,6 +234,17 @@ where
Self::get_systems(PhysicsSet::Writeback).in_set(PhysicsSet::Writeback),
),
);

// 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::<RapierConfiguration>();
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);
}
}
}
Comment on lines +238 to +247
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we automatically set it to fixed here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m fine with the warning for now.

}
}
}