avian3d/schedule/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
//! Sets up the default scheduling, system set configuration, and time resources for physics.
//!
//! See [`PhysicsSchedulePlugin`].
mod time;
use dynamics::solver::schedule::SubstepCount;
pub use time::*;
use std::time::Duration;
// For doc links
#[allow(unused_imports)]
use crate::prelude::*;
use bevy::{
ecs::intern::Interned,
ecs::schedule::{ExecutorKind, LogLevel, ScheduleBuildSettings, ScheduleLabel},
prelude::*,
transform::TransformSystem,
};
/// Sets up the default scheduling, system set configuration, and time resources for physics.
///
/// # Schedules and Sets
///
/// This plugin initializes and configures the following schedules and system sets:
///
/// - [`PhysicsSet`]: High-level system sets for the main phases of the physics engine.
/// You can use these to schedule your own systems before or after physics is run without
/// having to worry about implementation details.
/// - [`PhysicsSchedule`]: Responsible for advancing the simulation in [`PhysicsSet::StepSimulation`].
/// - [`PhysicsStepSet`]: System sets for the steps of the actual physics simulation loop.
pub struct PhysicsSchedulePlugin {
schedule: Interned<dyn ScheduleLabel>,
}
impl PhysicsSchedulePlugin {
/// Creates a [`PhysicsSchedulePlugin`] using the given schedule for running the [`PhysicsSchedule`].
///
/// The default schedule is `FixedPostUpdate`.
pub fn new(schedule: impl ScheduleLabel) -> Self {
Self {
schedule: schedule.intern(),
}
}
}
impl Default for PhysicsSchedulePlugin {
fn default() -> Self {
Self::new(FixedPostUpdate)
}
}
impl Plugin for PhysicsSchedulePlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Time<Physics>>()
.insert_resource(Time::new_with(Substeps))
.init_resource::<SubstepCount>();
// TODO: Where should this be initialized?
app.init_resource::<PhysicsLengthUnit>();
// Configure higher level system sets for the given schedule
let schedule = self.schedule;
app.configure_sets(
schedule,
(
PhysicsSet::Prepare,
PhysicsSet::StepSimulation,
PhysicsSet::Sync,
)
.chain()
.before(TransformSystem::TransformPropagate),
);
// Set up the physics schedule, the schedule that advances the physics simulation
app.edit_schedule(PhysicsSchedule, |schedule| {
schedule
.set_executor_kind(ExecutorKind::SingleThreaded)
.set_build_settings(ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});
schedule.configure_sets(
(
PhysicsStepSet::First,
PhysicsStepSet::BroadPhase,
PhysicsStepSet::NarrowPhase,
PhysicsStepSet::Solver,
PhysicsStepSet::ReportContacts,
PhysicsStepSet::Sleeping,
PhysicsStepSet::SpatialQuery,
PhysicsStepSet::Last,
)
.chain(),
);
});
app.add_systems(
schedule,
run_physics_schedule.in_set(PhysicsSet::StepSimulation),
);
}
}
/// True if a system is running for the first time.
struct IsFirstRun(bool);
impl Default for IsFirstRun {
fn default() -> Self {
Self(true)
}
}
/// Responsible for advancing the physics simulation. This is run in [`PhysicsSet::StepSimulation`].
///
/// See [`PhysicsStepSet`] for the system sets that are run in this schedule.
#[derive(Debug, Hash, PartialEq, Eq, Clone, ScheduleLabel)]
pub struct PhysicsSchedule;
/// A schedule where you can add systems to filter or modify collisions
/// using the [`Collisions`] resource.
///
/// The schedule is empty by default and runs in
/// [`NarrowPhaseSet::PostProcess`](collision::narrow_phase::NarrowPhaseSet::PostProcess).
///
/// # Example
///
/// Below is an example of how you could add a system that filters collisions.
///
/// ```no_run
#[cfg_attr(feature = "2d", doc = "use avian2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use avian3d::prelude::*;")]
/// use bevy::prelude::*;
///
/// #[derive(Component)]
/// struct Invulnerable;
///
/// fn main() {
/// App::new()
/// .add_plugins((DefaultPlugins, PhysicsPlugins::default()))
/// .add_systems(PostProcessCollisions, filter_collisions)
/// .run();
/// }
///
/// fn filter_collisions(mut collisions: ResMut<Collisions>, query: Query<(), With<Invulnerable>>) {
/// // Remove collisions where one of the colliders has an `Invulnerable` component.
/// // In a real project, this could be done more efficiently with collision layers.
/// collisions.retain(|contacts| {
/// !query.contains(contacts.entity1) && !query.contains(contacts.entity2)
/// });
/// }
/// ```
#[derive(Debug, Hash, PartialEq, Eq, Clone, ScheduleLabel)]
pub struct PostProcessCollisions;
/// High-level system sets for the main phases of the physics engine.
/// You can use these to schedule your own systems before or after physics is run without
/// having to worry about implementation details.
///
/// 1. `Prepare`: Responsible for initializing [rigid bodies](RigidBody) and [colliders](Collider) and
/// updating several components.
/// 2. `StepSimulation`: Responsible for advancing the simulation by running the steps in [`PhysicsStepSet`].
/// 3. `Sync`: Responsible for synchronizing physics components with other data, like keeping [`Position`]
/// and [`Rotation`] in sync with `Transform`.
///
/// # See Also
///
/// - [`PhysicsSchedule`]: Responsible for advancing the simulation in [`PhysicsSet::StepSimulation`].
/// - [`PhysicsStepSet`]: System sets for the steps of the actual physics simulation loop, like
/// the broad phase and the substepping loop.
/// - [`SubstepSchedule`]: Responsible for running the substepping loop in [`PhysicsStepSet::Solver`].
/// - [`PostProcessCollisions`]: Responsible for running the post-process collisions group in
/// [`NarrowPhaseSet::PostProcess`](collision::narrow_phase::NarrowPhaseSet::PostProcess).
/// Empty by default.
#[derive(SystemSet, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PhysicsSet {
/// Responsible for initializing [rigid bodies](RigidBody) and [colliders](Collider) and
/// updating several components.
///
/// See [`PreparePlugin`].
Prepare,
/// Responsible for advancing the simulation by running the steps in [`PhysicsStepSet`].
/// Systems in this set are run in the [`PhysicsSchedule`].
StepSimulation,
/// Responsible for synchronizing physics components with other data, like keeping [`Position`]
/// and [`Rotation`] in sync with `Transform`.
///
/// See [`SyncPlugin`].
Sync,
}
/// System sets for the main steps in the physics simulation loop. These are typically run in the [`PhysicsSchedule`].
///
/// 1. First (empty by default)
/// 2. Broad phase
/// 3. Narrow phase
/// 4. Solver
/// 5. Report contacts (send collision events)
/// 6. Sleeping
/// 7. Spatial queries
/// 8. Last (empty by default)
#[derive(SystemSet, Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PhysicsStepSet {
/// Runs at the start of the [`PhysicsSchedule`]. Empty by default.
First,
/// Responsible for collecting pairs of potentially colliding entities into [`BroadCollisionPairs`] using
/// [AABB](ColliderAabb) intersection tests.
///
/// See [`BroadPhasePlugin`].
BroadPhase,
/// Responsible for computing contacts between entities and sending collision events.
///
/// See [`NarrowPhasePlugin`].
NarrowPhase,
/// Responsible for running the solver and its substepping loop.
///
/// See [`SolverPlugin`] and [`SubstepSchedule`].
Solver,
/// Responsible for sending collision events and updating [`CollidingEntities`].
///
/// See [`ContactReportingPlugin`].
ReportContacts,
/// Responsible for controlling when bodies should be deactivated and marked as [`Sleeping`].
///
/// See [`SleepingPlugin`].
Sleeping,
/// Responsible for spatial queries like [raycasting](`RayCaster`) and shapecasting.
///
/// See [`SpatialQueryPlugin`].
SpatialQuery,
/// Runs at the end of the [`PhysicsSchedule`]. Empty by default.
Last,
}
/// Runs the [`PhysicsSchedule`].
fn run_physics_schedule(world: &mut World, mut is_first_run: Local<IsFirstRun>) {
let _ = world.try_schedule_scope(PhysicsSchedule, |world, schedule| {
let is_paused = world.resource::<Time<Physics>>().is_paused();
let old_clock = world.resource::<Time>().as_generic();
let physics_clock = world.resource_mut::<Time<Physics>>();
// Get the scaled timestep delta time based on the timestep mode.
let timestep = old_clock
.delta()
.mul_f64(physics_clock.relative_speed_f64());
// Advance physics clock by timestep if not paused.
if !is_paused {
world.resource_mut::<Time<Physics>>().advance_by(timestep);
// Advance substep clock already so that systems running before the substepping loop have the right delta.
let SubstepCount(substeps) = *world.resource::<SubstepCount>();
let sub_delta = timestep.div_f64(substeps as f64);
world.resource_mut::<Time<Substeps>>().advance_by(sub_delta);
}
// Set generic `Time` resource to `Time<Physics>`.
*world.resource_mut::<Time>() = world.resource::<Time<Physics>>().as_generic();
// Advance simulation.
trace!("running PhysicsSchedule");
schedule.run(world);
// If physics is paused, reset delta time to stop simulation
// unless users manually advance `Time<Physics>`.
if is_paused {
world
.resource_mut::<Time<Physics>>()
.advance_by(Duration::ZERO);
}
// Set generic `Time` resource back to the clock that was active before physics.
*world.resource_mut::<Time>() = old_clock;
});
is_first_run.0 = false;
}