Skip to content

Commit

Permalink
Don't overwrite schedules when adding plugin (#236)
Browse files Browse the repository at this point in the history
If user systems were added to any of the physics schedules (e.g.
`PostProcessCollisions`) before `PhysicsPlugins` are added, the
schedules would be replaced when adding `PhysicsPlugins`, effectively
ignoring the user's system.

```rust
app
    .add_system(PostProcessColllisions, filter_collisions)
    .add_plugins(PhysicsPlugins);
```

Solve it by using `edit_schedule` instead of `add_schedule`.
  • Loading branch information
johanhelsing authored Nov 10, 2023
1 parent 776a785 commit 2eb3492
Showing 1 changed file with 52 additions and 58 deletions.
110 changes: 52 additions & 58 deletions src/plugins/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,77 +129,71 @@ impl Plugin for PhysicsSetupPlugin {
.before(TransformSystem::TransformPropagate),
);

// Create physics schedule, the schedule that advances the physics simulation
let mut physics_schedule = Schedule::new(PhysicsSchedule);

physics_schedule
.set_executor_kind(ExecutorKind::SingleThreaded)
.set_build_settings(ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});

physics_schedule.configure_sets(
(
PhysicsStepSet::BroadPhase,
PhysicsStepSet::Substeps,
PhysicsStepSet::ReportContacts,
PhysicsStepSet::Sleeping,
PhysicsStepSet::SpatialQuery,
)
.chain(),
);

app.add_schedule(physics_schedule);
// 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::BroadPhase,
PhysicsStepSet::Substeps,
PhysicsStepSet::ReportContacts,
PhysicsStepSet::Sleeping,
PhysicsStepSet::SpatialQuery,
)
.chain(),
);
});

app.add_systems(
schedule,
run_physics_schedule.in_set(PhysicsSet::StepSimulation),
);

// Create substep schedule, the schedule that runs the inner substepping loop
let mut substep_schedule = Schedule::new(SubstepSchedule);

substep_schedule
.set_executor_kind(ExecutorKind::SingleThreaded)
.set_build_settings(ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});

substep_schedule.configure_sets(
(
SubstepSet::Integrate,
SubstepSet::NarrowPhase,
SubstepSet::PostProcessCollisions,
SubstepSet::SolveConstraints,
SubstepSet::SolveUserConstraints,
SubstepSet::UpdateVelocities,
SubstepSet::SolveVelocities,
SubstepSet::ApplyTranslation,
)
.chain(),
);

app.add_schedule(substep_schedule);
// Set up the substep schedule, the schedule that runs the inner substepping loop
app.edit_schedule(SubstepSchedule, |schedule| {
schedule
.set_executor_kind(ExecutorKind::SingleThreaded)
.set_build_settings(ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});

schedule.configure_sets(
(
SubstepSet::Integrate,
SubstepSet::NarrowPhase,
SubstepSet::PostProcessCollisions,
SubstepSet::SolveConstraints,
SubstepSet::SolveUserConstraints,
SubstepSet::UpdateVelocities,
SubstepSet::SolveVelocities,
SubstepSet::ApplyTranslation,
)
.chain(),
);
});

app.add_systems(
PhysicsSchedule,
run_substep_schedule.in_set(PhysicsStepSet::Substeps),
);

// Create the PostProcessCollisions schedule for user-defined systems
// Set up the PostProcessCollisions schedule for user-defined systems
// that filter and modify collisions.
let mut post_process_collisions_schedule = Schedule::new(PostProcessCollisions);

post_process_collisions_schedule
.set_executor_kind(ExecutorKind::SingleThreaded)
.set_build_settings(ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});

app.add_schedule(post_process_collisions_schedule);
app.edit_schedule(PostProcessCollisions, |schedule| {
schedule
.set_executor_kind(ExecutorKind::SingleThreaded)
.set_build_settings(ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});
});

app.add_systems(
SubstepSchedule,
Expand Down

0 comments on commit 2eb3492

Please sign in to comment.