Skip to content

Commit

Permalink
Allow customizing the schedule with FixedUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri committed Jun 19, 2023
1 parent 38e1b79 commit 7c99ef2
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 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::*;
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,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.
Expand Down Expand Up @@ -124,6 +135,7 @@ pub struct RapierTransformPropagateSet;
impl<PhysicsHooksSystemParam> Default for RapierPhysicsPlugin<PhysicsHooksSystemParam> {
fn default() -> Self {
Self {
schedule: Box::new(CoreSchedule::Main),
physics_scale: 1.0,
default_system_setup: true,
_phantom: PhantomData,
Expand Down Expand Up @@ -195,31 +207,43 @@ where

// Add each set as necessary
if self.default_system_setup {
app.configure_sets(
(
PhysicsSet::SyncBackend,
PhysicsSet::SyncBackendFlush,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
)
.chain()
.after(CoreSet::UpdateFlush)
.before(CoreSet::PostUpdate),
);
app.world
.resource_mut::<Schedules>()
.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),
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_base_set(PhysicsSet::SyncBackendFlush)
.in_schedule(self.schedule.clone()),
);
app.add_systems(
Self::get_systems(PhysicsSet::StepSimulation)
.in_base_set(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),
Self::get_systems(PhysicsSet::Writeback)
.in_base_set(PhysicsSet::Writeback)
.in_schedule(self.schedule.clone()),
);
}
}
Expand Down

0 comments on commit 7c99ef2

Please sign in to comment.