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

Improve system schedules #584

Merged
merged 10 commits into from
Sep 20, 2024
1 change: 1 addition & 0 deletions bevy_rapier2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ approx = "0.5.1"
glam = { version = "0.27", features = ["approx"] }
bevy-inspector-egui = "0.25.1"
bevy_egui = "0.28.0"
bevy_mod_debugdump = "0.11"

[package.metadata.docs.rs]
# Enable all the features when building the docs on docs.rs
Expand Down
61 changes: 61 additions & 0 deletions bevy_rapier2d/examples/debugdump2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

fn main() {
let mut app = App::new();
app.insert_resource(ClearColor(Color::srgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics))
.add_systems(PostUpdate, display_events);

bevy_mod_debugdump::print_schedule_graph(&mut app, PostUpdate);
}

pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}

pub fn display_events(
mut collision_events: EventReader<CollisionEvent>,
mut contact_force_events: EventReader<ContactForceEvent>,
) {
for collision_event in collision_events.read() {
println!("Received collision event: {collision_event:?}");
}

for contact_force_event in contact_force_events.read() {
println!("Received contact force event: {contact_force_event:?}");
}
}

pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -24.0, 0.0)),
Collider::cuboid(80.0, 20.0),
));

commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 100.0, 0.0)),
Collider::cuboid(80.0, 30.0),
Sensor,
));

commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 260.0, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(10.0, 10.0),
ActiveEvents::COLLISION_EVENTS,
ContactForceEventThreshold(10.0),
));
}
52 changes: 31 additions & 21 deletions src/plugin/plugin.rs
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,45 @@ where
match set {
PhysicsSet::SyncBackend => (
// Run the character controller before the manual transform propagation.
systems::update_character_controls,
systems::update_character_controls.in_set(PhysicsSet::SyncBackend),
// Run Bevy transform propagation additionally to sync [`GlobalTransform`]
(
bevy::transform::systems::sync_simple_transforms,
bevy::transform::systems::propagate_transforms,
)
.chain()
.in_set(RapierTransformPropagateSet),
#[cfg(all(feature = "dim3", feature = "async-collider"))]
systems::init_async_scene_colliders,
#[cfg(all(feature = "dim3", feature = "async-collider"))]
systems::init_async_colliders,
systems::init_rigid_bodies,
systems::init_colliders,
systems::init_joints,
systems::sync_removals,
// Run this here so the following systems do not have a 1 frame delay.
apply_deferred,
systems::apply_scale,
systems::apply_collider_user_changes,
systems::apply_rigid_body_user_changes,
systems::apply_joint_user_changes,
systems::apply_initial_rigid_body_impulses,
(
#[cfg(all(feature = "dim3", feature = "async-collider"))]
systems::init_async_scene_colliders,
#[cfg(all(feature = "dim3", feature = "async-collider"))]
systems::init_async_colliders,
systems::init_rigid_bodies,
systems::init_colliders,
systems::init_joints,
systems::sync_removals,
// Run this here so the following systems do not have a 1 frame delay.
apply_deferred,
systems::apply_scale,
systems::apply_collider_user_changes,
systems::apply_rigid_body_user_changes,
systems::apply_joint_user_changes,
systems::apply_initial_rigid_body_impulses,
)
.chain()
.in_set(PhysicsSet::SyncBackend),
)
.chain()
.into_configs(),
PhysicsSet::StepSimulation => (systems::step_simulation::<PhysicsHooks>).into_configs(),
PhysicsSet::StepSimulation => (systems::step_simulation::<PhysicsHooks>)
.in_set(PhysicsSet::StepSimulation)
.into_configs(),
PhysicsSet::Writeback => (
systems::update_colliding_entities,
systems::writeback_rigid_bodies,
systems::writeback_mass_properties,
)
.in_set(PhysicsSet::Writeback)
.into_configs(),
}
}
Expand Down Expand Up @@ -214,17 +221,20 @@ where
.chain()
.before(TransformSystem::TransformPropagate),
);
app.configure_sets(
self.schedule,
RapierTransformPropagateSet.in_set(PhysicsSet::SyncBackend),
);

// 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(
self.schedule,
(
Self::get_systems(PhysicsSet::SyncBackend).in_set(PhysicsSet::SyncBackend),
Self::get_systems(PhysicsSet::StepSimulation)
.in_set(PhysicsSet::StepSimulation),
Self::get_systems(PhysicsSet::Writeback).in_set(PhysicsSet::Writeback),
Self::get_systems(PhysicsSet::SyncBackend),
Self::get_systems(PhysicsSet::StepSimulation),
Self::get_systems(PhysicsSet::Writeback),
),
);

Expand Down