From 7135148ea54e2078e070f80219a1d261bf905a0a Mon Sep 17 00:00:00 2001 From: devil-ira Date: Mon, 6 Feb 2023 23:18:47 +0100 Subject: [PATCH 01/18] Update to Bevy 0.10 --- bevy_rapier2d/Cargo.toml | 5 +- bevy_rapier2d/examples/events2.rs | 2 +- bevy_rapier2d/examples/player_movement2.rs | 11 +- bevy_rapier3d/Cargo.toml | 5 +- .../examples/custom_system_setup3.rs | 25 +-- bevy_rapier3d/examples/events3.rs | 2 +- bevy_rapier3d/examples/ray_casting3.rs | 39 +--- src/pipeline/events.rs | 4 +- src/pipeline/physics_hooks.rs | 4 +- src/plugin/mod.rs | 2 +- src/plugin/plugin.rs | 189 ++++++++---------- src/plugin/systems.rs | 43 ++-- src/render/lines/mod.rs | 48 ++--- src/render/lines/render_dim.rs | 83 ++++---- src/render/mod.rs | 9 +- 15 files changed, 202 insertions(+), 269 deletions(-) diff --git a/bevy_rapier2d/Cargo.toml b/bevy_rapier2d/Cargo.toml index 2bff9313..4807f643 100644 --- a/bevy_rapier2d/Cargo.toml +++ b/bevy_rapier2d/Cargo.toml @@ -37,7 +37,8 @@ async-collider = [ ] [dependencies] -bevy = { version = "0.9.0", default-features = false, features = ["bevy_asset", "bevy_scene"] } +bevy = { path = "../../bevy", default-features = false, features = ["bevy_asset", "bevy_scene"] } +# bevy = { version = "0.9.0", default-features = false, features = ["bevy_asset", "bevy_scene"] } nalgebra = { version = "0.32.0", features = [ "convert-glam022" ] } # Don't enable the default features because we don't need the ColliderSet/RigidBodySet rapier2d = "0.17.0" @@ -47,7 +48,7 @@ log = "0.4" serde = { version = "1", features = [ "derive" ], optional = true} [dev-dependencies] -bevy = { version = "0.9.0", default-features = false, features = ["x11"]} +bevy = { path = "../../bevy", default-features = false, features = ["x11"]} oorandom = "11" approx = "0.5.1" glam = { version = "0.22", features = [ "approx" ] } diff --git a/bevy_rapier2d/examples/events2.rs b/bevy_rapier2d/examples/events2.rs index 77c60f65..9263baf4 100644 --- a/bevy_rapier2d/examples/events2.rs +++ b/bevy_rapier2d/examples/events2.rs @@ -13,7 +13,7 @@ fn main() { .add_plugin(RapierDebugRenderPlugin::default()) .add_startup_system(setup_graphics) .add_startup_system(setup_physics) - .add_system_to_stage(CoreStage::PostUpdate, display_events) + .add_system(display_events.in_base_set(CoreSet::PostUpdate)) .run(); } diff --git a/bevy_rapier2d/examples/player_movement2.rs b/bevy_rapier2d/examples/player_movement2.rs index 898b9557..bf273bd7 100644 --- a/bevy_rapier2d/examples/player_movement2.rs +++ b/bevy_rapier2d/examples/player_movement2.rs @@ -1,15 +1,14 @@ -use bevy::prelude::*; +use bevy::{prelude::*, window::WindowResolution}; use bevy_rapier2d::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { - window: WindowDescriptor { + primary_window: Some(Window { + resolution: WindowResolution::new(1000., 1000.), title: "Player Movement Example".to_string(), - width: 1000.0, - height: 1000.0, - ..Default::default() - }, + ..default() + }), ..default() })) .add_startup_system(spawn_player) diff --git a/bevy_rapier3d/Cargo.toml b/bevy_rapier3d/Cargo.toml index 8cc93187..20078aa9 100644 --- a/bevy_rapier3d/Cargo.toml +++ b/bevy_rapier3d/Cargo.toml @@ -38,7 +38,8 @@ headless = [ ] async-collider = [ ] [dependencies] -bevy = { version = "0.9.0", default-features = false, features = ["bevy_asset", "bevy_scene"] } +bevy = { path = "../../bevy", default-features = false, features = ["bevy_asset", "bevy_scene"] } +# bevy = { version = "0.9.0", default-features = false, features = ["bevy_asset", "bevy_scene"] } nalgebra = { version = "^0.32.0", features = [ "convert-glam022" ] } # Don't enable the default features because we don't need the ColliderSet/RigidBodySet rapier3d = "0.17.0" @@ -48,7 +49,7 @@ log = "0.4" serde = { version = "1", features = [ "derive" ], optional = true} [dev-dependencies] -bevy = { version = "0.9.0", default-features = false, features = ["x11"]} +bevy = { path = "../../bevy", default-features = false, features = ["x11"]} approx = "0.5.1" glam = { version = "0.22", features = [ "approx" ] } diff --git a/bevy_rapier3d/examples/custom_system_setup3.rs b/bevy_rapier3d/examples/custom_system_setup3.rs index ddf315fe..3b2cdf1b 100644 --- a/bevy_rapier3d/examples/custom_system_setup3.rs +++ b/bevy_rapier3d/examples/custom_system_setup3.rs @@ -55,39 +55,30 @@ fn main() { SpecialStagingPlugin::new( Schedule::default() .with_stage( - PhysicsStages::SyncBackend, + PhysicsSet::SyncBackend, SystemStage::parallel().with_system_set( - RapierPhysicsPlugin::::get_systems(PhysicsStages::SyncBackend), + RapierPhysicsPlugin::::get_systems(PhysicsSet::SyncBackend), ), ) .with_stage_after( - PhysicsStages::SyncBackend, - PhysicsStages::StepSimulation, + PhysicsSet::SyncBackend, + PhysicsSet::StepSimulation, SystemStage::parallel() .with_system(despawn_one_box) // We can add a special despawn to determine cleanup later .with_system_set(RapierPhysicsPlugin::::get_systems( - PhysicsStages::StepSimulation, + PhysicsSet::StepSimulation, )), ) .with_stage_after( - PhysicsStages::StepSimulation, - PhysicsStages::Writeback, + PhysicsSet::StepSimulation, + PhysicsSet::Writeback, SystemStage::parallel().with_system_set( - RapierPhysicsPlugin::::get_systems(PhysicsStages::Writeback), + RapierPhysicsPlugin::::get_systems(PhysicsSet::Writeback), ), ), ) .build(&mut app); - // Be sure to setup all four stages - app.add_stage_before( - CoreStage::Last, - PhysicsStages::DetectDespawn, - SystemStage::parallel().with_system_set(RapierPhysicsPlugin::::get_systems( - PhysicsStages::DetectDespawn, - )), - ); - app.add_plugin(RapierPhysicsPlugin::::default().with_default_system_setup(false)); app.run(); diff --git a/bevy_rapier3d/examples/events3.rs b/bevy_rapier3d/examples/events3.rs index fb2b1660..43c30fab 100644 --- a/bevy_rapier3d/examples/events3.rs +++ b/bevy_rapier3d/examples/events3.rs @@ -13,7 +13,7 @@ fn main() { .add_plugin(RapierDebugRenderPlugin::default()) .add_startup_system(setup_graphics) .add_startup_system(setup_physics) - .add_system_to_stage(CoreStage::PostUpdate, display_events) + .add_system(display_events.in_base_set(CoreSet::PostUpdate)) .run(); } diff --git a/bevy_rapier3d/examples/ray_casting3.rs b/bevy_rapier3d/examples/ray_casting3.rs index 5490bc72..da8c4957 100644 --- a/bevy_rapier3d/examples/ray_casting3.rs +++ b/bevy_rapier3d/examples/ray_casting3.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{prelude::*, window::PrimaryWindow}; use bevy_rapier3d::prelude::*; fn main() { @@ -72,20 +72,23 @@ pub fn setup_physics(mut commands: Commands) { fn cast_ray( mut commands: Commands, - windows: Res, + windows: Query<&Window>, rapier_context: Res, cameras: Query<(&Camera, &GlobalTransform)>, ) { + let window = windows.single(); + + let Some(cursor_position) = window.cursor_position() else { return; }; + // We will color in read the colliders hovered by the mouse. - for (camera, camera_transform) in cameras.iter() { + for (camera, camera_transform) in &cameras { // First, compute a ray from the mouse position. - let (ray_pos, ray_dir) = - ray_from_mouse_position(windows.get_primary().unwrap(), camera, camera_transform); + let Some(ray) = camera.viewport_to_world(camera_transform, cursor_position) else { return; }; // Then cast the ray. let hit = rapier_context.cast_ray( - ray_pos, - ray_dir, + ray.origin, + ray.direction, f32::MAX, true, QueryFilter::only_dynamic(), @@ -100,25 +103,3 @@ fn cast_ray( } } } - -// Credit to @doomy on discord. -fn ray_from_mouse_position( - window: &Window, - camera: &Camera, - camera_transform: &GlobalTransform, -) -> (Vec3, Vec3) { - let mouse_position = window.cursor_position().unwrap_or(Vec2::new(0.0, 0.0)); - - let x = 2.0 * (mouse_position.x / window.width() as f32) - 1.0; - let y = 2.0 * (mouse_position.y / window.height() as f32) - 1.0; - - let camera_inverse_matrix = - camera_transform.compute_matrix() * camera.projection_matrix().inverse(); - let near = camera_inverse_matrix * Vec3::new(x, y, -1.0).extend(1.0); - let far = camera_inverse_matrix * Vec3::new(x, y, 1.0).extend(1.0); - - let near = near.truncate() / near.w; - let far = far.truncate() / far.w; - let dir: Vec3 = far - near; - (near, dir) -} diff --git a/src/pipeline/events.rs b/src/pipeline/events.rs index 61678579..9293a13c 100644 --- a/src/pipeline/events.rs +++ b/src/pipeline/events.rs @@ -49,8 +49,8 @@ pub(crate) struct EventQueue<'a> { // Used ot retrieve the entity of colliders that have been removed from the simulation // since the last physics step. pub deleted_colliders: &'a HashMap, - pub collision_events: RwLock>, - pub contact_force_events: RwLock>, + pub collision_events: RwLock>, + pub contact_force_events: RwLock>, } impl<'a> EventQueue<'a> { diff --git a/src/pipeline/physics_hooks.rs b/src/pipeline/physics_hooks.rs index 2ce9e30b..c3fd9556 100644 --- a/src/pipeline/physics_hooks.rs +++ b/src/pipeline/physics_hooks.rs @@ -1,5 +1,5 @@ use bevy::{ - ecs::system::{SystemParam, SystemParamFetch, SystemParamItem}, + ecs::system::{SystemParam, SystemParamItem}, prelude::*, }; use rapier::{ @@ -168,7 +168,7 @@ pub trait BevyPhysicsHooks: SystemParam + Send + Sync { impl BevyPhysicsHooks for T where T: 'static + PhysicsHooks + SystemParam + Send + Sync, - for<'w, 's> T::Fetch: SystemParamFetch<'w, 's, Item = T>, + for<'w, 's> T: SystemParam = T>, { fn filter_contact_pair(&self, context: PairFilterContextView) -> Option { PhysicsHooks::filter_contact_pair(self, context.raw) diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index d832673b..d0ca7d45 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -1,6 +1,6 @@ pub use self::configuration::{RapierConfiguration, SimulationToRenderTime, TimestepMode}; pub use self::context::RapierContext; -pub use self::plugin::{NoUserData, PhysicsStages, RapierPhysicsPlugin}; +pub use self::plugin::{NoUserData, PhysicsSet, RapierPhysicsPlugin}; #[allow(clippy::type_complexity)] #[allow(clippy::too_many_arguments)] diff --git a/src/plugin/plugin.rs b/src/plugin/plugin.rs index ffd761dd..2db2e648 100644 --- a/src/plugin/plugin.rs +++ b/src/plugin/plugin.rs @@ -2,8 +2,7 @@ use crate::pipeline::{CollisionEvent, ContactForceEvent}; use crate::plugin::configuration::SimulationToRenderTime; use crate::plugin::{systems, RapierConfiguration, RapierContext}; use crate::prelude::*; -use bevy::ecs::event::Events; -use bevy::ecs::system::SystemParamItem; +use bevy::ecs::{event::Events, schedule_v3::SystemConfigs, system::SystemParam}; use bevy::prelude::*; use std::marker::PhantomData; @@ -23,7 +22,7 @@ pub struct RapierPhysicsPlugin { impl RapierPhysicsPlugin where PhysicsHooks: 'static + BevyPhysicsHooks, - for<'w, 's> SystemParamItem<'w, 's, PhysicsHooks>: BevyPhysicsHooks, + for<'w, 's> PhysicsHooks: SystemParam = PhysicsHooks>, { /// Specifies a scale ratio between the physics world and the bevy transforms. /// @@ -59,77 +58,52 @@ where /// Provided for use when staging systems outside of this plugin using /// [`with_system_setup(false)`](Self::with_system_setup). - /// See [`PhysicsStages`] for a description of these systems. - pub fn get_systems(stage: PhysicsStages) -> SystemSet { - match stage { - PhysicsStages::SyncBackend => { - let systems = SystemSet::new() - .with_system(systems::update_character_controls) // Run the character controller befor ethe manual transform propagation. - .with_system( - bevy::transform::transform_propagate_system - .after(systems::update_character_controls), - ) // Run Bevy transform propagation additionally to sync [`GlobalTransform`] - .with_system( - systems::init_async_colliders - .after(bevy::transform::transform_propagate_system), - ) - .with_system(systems::apply_scale.after(systems::init_async_colliders)) - .with_system(systems::apply_collider_user_changes.after(systems::apply_scale)) - .with_system( - systems::apply_rigid_body_user_changes - .after(systems::apply_collider_user_changes), - ) - .with_system( - systems::apply_joint_user_changes - .after(systems::apply_rigid_body_user_changes), - ) - .with_system( - systems::init_rigid_bodies.after(systems::apply_joint_user_changes), - ) - .with_system( - systems::init_colliders - .after(systems::init_rigid_bodies) - .after(systems::init_async_colliders), - ) - .with_system(systems::init_joints.after(systems::init_colliders)) - .with_system( - systems::apply_initial_rigid_body_impulses.after(systems::init_colliders), - ) - .with_system( - systems::sync_removals - .after(systems::init_joints) - .after(systems::apply_initial_rigid_body_impulses), - ); - + /// See [`PhysicsSet`] for a description of these systems. + pub fn get_systems(set: PhysicsSet) -> SystemConfigs { + #[derive(SystemSet, Hash, Debug, PartialEq, Eq, Clone)] + struct Label; + match set { + PhysicsSet::SyncBackend => ( + systems::update_character_controls, + bevy::transform::systems::sync_simple_transforms + .in_set(Label::<1>) + .after(systems::update_character_controls), + bevy::transform::systems::propagate_transforms + .after(systems::update_character_controls) + .after(Label::<1>) + .in_set(Label::<2>), + systems::init_async_colliders.after(Label::<2>), + systems::apply_scale.after(systems::init_async_colliders), + systems::apply_collider_user_changes.after(systems::apply_scale), + systems::apply_rigid_body_user_changes.after(systems::apply_collider_user_changes), + systems::apply_joint_user_changes.after(systems::apply_rigid_body_user_changes), + systems::init_rigid_bodies.after(systems::apply_joint_user_changes), + systems::init_colliders + .after(systems::init_rigid_bodies) + .after(systems::init_async_colliders), + systems::init_joints.after(systems::init_colliders), + systems::apply_initial_rigid_body_impulses.after(systems::init_colliders), + systems::sync_removals + .after(systems::init_joints) + .after(systems::apply_initial_rigid_body_impulses), #[cfg(all(feature = "dim3", feature = "async-collider"))] - { - systems.with_system( - systems::init_async_scene_colliders.before(systems::init_async_colliders), - ) - } - #[cfg(not(feature = "dim3"))] - { - systems - } - #[cfg(feature = "headless")] - { - systems - } - } - PhysicsStages::StepSimulation => SystemSet::new() - .with_system(systems::step_simulation::) - .with_system( - Events::::update_system - .before(systems::step_simulation::), - ) - .with_system( - Events::::update_system - .before(systems::step_simulation::), - ), - PhysicsStages::Writeback => SystemSet::new() - .with_system(systems::update_colliding_entities) - .with_system(systems::writeback_rigid_bodies), - PhysicsStages::DetectDespawn => SystemSet::new().with_system(systems::sync_removals), + systems::init_async_scene_colliders.before(systems::init_async_colliders), + ) + .into_configs(), + PhysicsSet::SyncBackendFlush => (apply_system_buffers,).into_configs(), + PhysicsSet::StepSimulation => ( + systems::step_simulation::, + Events::::update_system + .before(systems::step_simulation::), + Events::::update_system + .before(systems::step_simulation::), + ) + .into_configs(), + PhysicsSet::Writeback => ( + systems::update_colliding_entities, + systems::writeback_rigid_bodies, + ) + .into_configs(), } } } @@ -144,34 +118,32 @@ impl Default for RapierPhysicsPlugin Plugin for RapierPhysicsPlugin where PhysicsHooks: 'static + BevyPhysicsHooks, - for<'w, 's> SystemParamItem<'w, 's, PhysicsHooks>: BevyPhysicsHooks, + for<'w, 's> PhysicsHooks: SystemParam = PhysicsHooks>, { fn build(&self, app: &mut App) { // Register components as reflectable. @@ -198,9 +170,7 @@ where // Insert all of our required resources. Don’t overwrite // the `RapierConfiguration` if it already exists. - if app.world.get_resource::().is_none() { - app.insert_resource(RapierConfiguration::default()); - } + app.init_resource::(); app.insert_resource(SimulationToRenderTime::default()) .insert_resource(RapierContext { @@ -210,33 +180,30 @@ where .insert_resource(Events::::default()) .insert_resource(Events::::default()); - // Add each stage as necessary + // Add each set as necessary if self.default_system_setup { - app.add_stage_after( - CoreStage::Update, - PhysicsStages::SyncBackend, - SystemStage::parallel() - .with_system_set(Self::get_systems(PhysicsStages::SyncBackend)), + app.configure_sets( + ( + PhysicsSet::SyncBackend, + PhysicsSet::SyncBackendFlush, + PhysicsSet::StepSimulation, + PhysicsSet::Writeback, + ) + .chain() + .after(CoreSet::UpdateFlush) + .before(CoreSet::PostUpdate), ); - app.add_stage_after( - PhysicsStages::SyncBackend, - PhysicsStages::StepSimulation, - SystemStage::parallel() - .with_system_set(Self::get_systems(PhysicsStages::StepSimulation)), + + app.add_systems( + Self::get_systems(PhysicsSet::SyncBackend).in_base_set(PhysicsSet::SyncBackend), ); - app.add_stage_after( - PhysicsStages::StepSimulation, - PhysicsStages::Writeback, - SystemStage::parallel() - .with_system_set(Self::get_systems(PhysicsStages::Writeback)), + app.add_system(apply_system_buffers.in_base_set(PhysicsSet::SyncBackendFlush)); + app.add_systems( + Self::get_systems(PhysicsSet::StepSimulation) + .in_base_set(PhysicsSet::StepSimulation), ); - - // NOTE: we run sync_removals at the end of the frame, too, in order to make sure we don’t miss any `RemovedComponents`. - app.add_stage_before( - CoreStage::Last, - PhysicsStages::DetectDespawn, - SystemStage::parallel() - .with_system_set(Self::get_systems(PhysicsStages::DetectDespawn)), + app.add_systems( + Self::get_systems(PhysicsSet::Writeback).in_base_set(PhysicsSet::Writeback), ); } } diff --git a/src/plugin/systems.rs b/src/plugin/systems.rs index c11ba7e7..f2127416 100644 --- a/src/plugin/systems.rs +++ b/src/plugin/systems.rs @@ -19,7 +19,7 @@ use crate::prelude::{ KinematicCharacterControllerOutput, RigidBodyDisabled, }; use crate::utils; -use bevy::ecs::system::SystemParamItem; +use bevy::ecs::system::{SystemParam, SystemParamItem}; use bevy::prelude::*; use rapier::prelude::*; use std::collections::HashMap; @@ -632,13 +632,14 @@ pub fn step_simulation( mut context: ResMut, config: Res, hooks: SystemParamItem, - (time, mut sim_to_render_time): (Res