From c3e061e62fed03450a3db7108d41f1f6681d6917 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Sun, 29 Aug 2021 00:29:41 -0700 Subject: [PATCH] Update to Bevy 0.6 main branch. The biggest change here is that doing the same query twice, one mutably and one immutably, is no longer needed, once bevyengine/bevy#2305 lands. Before that lands, however, this upgrade is actually impossible to do safely, since the `q0`/`q0_mut` distinction is gone and therefore it's impossible to implement the Rapier component traits on Bevy queries. (This is filed upstream as bevyengine/bevy#2744.) Therefore, this upgrade has to wait until that Bevy PR lands. Other minor changes: * `Light` has been renamed to `PointLight`. * `App::build()` is now `App::new()`, and `AppBuilder` is now `App`. * `glam` has been upgraded upstream. --- bevy_rapier2d/examples/boxes2.rs | 6 +- bevy_rapier2d/examples/contact_filter2.rs | 6 +- bevy_rapier2d/examples/debug_despawn2.rs | 7 +- bevy_rapier2d/examples/despawn2.rs | 6 +- bevy_rapier2d/examples/events2.rs | 6 +- bevy_rapier2d/examples/joints2.rs | 6 +- bevy_rapier2d/examples/joints_despawn2.rs | 6 +- bevy_rapier2d/examples/locked_rotations2.rs | 6 +- bevy_rapier2d/examples/multiple_colliders2.rs | 6 +- bevy_rapier2d/examples/player_movement2.rs | 2 +- bevy_rapier3d/examples/boxes3.rs | 6 +- bevy_rapier3d/examples/contact_filter3.rs | 6 +- bevy_rapier3d/examples/despawn3.rs | 6 +- bevy_rapier3d/examples/events3.rs | 6 +- bevy_rapier3d/examples/joints3.rs | 6 +- bevy_rapier3d/examples/joints_despawn3.rs | 6 +- bevy_rapier3d/examples/locked_rotations3.rs | 6 +- bevy_rapier3d/examples/multiple_colliders3.rs | 6 +- bevy_rapier3d/examples/ray_casting3.rs | 6 +- bevy_rapier3d/examples/static_trimesh3.rs | 6 +- src/physics/collider_component_set.rs | 133 ++++++++-------- src/physics/mod.rs | 75 +++------ src/physics/plugins.rs | 10 +- src/physics/resources.rs | 47 +++--- src/physics/rigid_body_component_set.rs | 148 ++++++++---------- src/physics/systems.rs | 41 +++-- src/render/plugins.rs | 2 +- src/render/systems.rs | 2 +- src_debug_ui/plugins.rs | 2 +- 29 files changed, 261 insertions(+), 316 deletions(-) diff --git a/bevy_rapier2d/examples/boxes2.rs b/bevy_rapier2d/examples/boxes2.rs index 9309f797..61b9a24f 100644 --- a/bevy_rapier2d/examples/boxes2.rs +++ b/bevy_rapier2d/examples/boxes2.rs @@ -12,7 +12,7 @@ use ui::DebugUiPlugin; mod ui; fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -40,9 +40,9 @@ fn setup_graphics(mut commands: Commands, mut configuration: ResMut PhysicsHooksWithQuery<&'a CustomFilterTag> for SameUserDataFilter { } fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -77,9 +77,9 @@ fn setup_graphics(mut commands: Commands, mut configuration: ResMut() .insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0))) .insert_resource(Msaa::default()) @@ -187,8 +188,8 @@ fn spawn_cube(commands: &mut Commands, game: &mut Game) { let x_dir = coords[*j].0 as f32 - coords[*i].0 as f32; let y_dir = coords[*j].1 as f32 - coords[*i].1 as f32; - let anchor_1 = Vec2::new(x_dir * 0.5, y_dir * 0.5).into(); - let anchor_2 = Vec2::new(x_dir * -0.5, y_dir * -0.5).into(); + let anchor_1 = Point2::new(x_dir * 0.5, y_dir * 0.5); + let anchor_2 = Point2::new(x_dir * -0.5, y_dir * -0.5); commands .spawn() diff --git a/bevy_rapier2d/examples/despawn2.rs b/bevy_rapier2d/examples/despawn2.rs index 20f1d6a7..ffbeaa34 100644 --- a/bevy_rapier2d/examples/despawn2.rs +++ b/bevy_rapier2d/examples/despawn2.rs @@ -22,7 +22,7 @@ pub struct ResizeResource { } fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -54,9 +54,9 @@ fn setup_graphics(mut commands: Commands, mut configuration: ResMut) { fn setup_graphics(mut commands: Commands, mut configuration: ResMut) { configuration.scale = 15.0; - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(1000.0, 10.0, 2000.0)), - light: Light { + point_light: PointLight { intensity: 100_000_000_.0, range: 6000.0, ..Default::default() diff --git a/bevy_rapier2d/examples/joints2.rs b/bevy_rapier2d/examples/joints2.rs index 38f61cc5..4669359d 100644 --- a/bevy_rapier2d/examples/joints2.rs +++ b/bevy_rapier2d/examples/joints2.rs @@ -13,7 +13,7 @@ use ui::DebugUiPlugin; mod ui; fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -41,9 +41,9 @@ fn setup_graphics(mut commands: Commands, mut configuration: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/bevy_rapier3d/examples/contact_filter3.rs b/bevy_rapier3d/examples/contact_filter3.rs index 6e953a7c..99fd2462 100644 --- a/bevy_rapier3d/examples/contact_filter3.rs +++ b/bevy_rapier3d/examples/contact_filter3.rs @@ -49,7 +49,7 @@ impl<'a> PhysicsHooksWithQuery<&'a CustomFilterTag> for SameUserDataFilter { } fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -73,9 +73,9 @@ fn enable_physics_profiling(mut pipeline: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/bevy_rapier3d/examples/despawn3.rs b/bevy_rapier3d/examples/despawn3.rs index 8f9b645d..032db0c6 100644 --- a/bevy_rapier3d/examples/despawn3.rs +++ b/bevy_rapier3d/examples/despawn3.rs @@ -16,7 +16,7 @@ pub struct DespawnResource { } fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -42,9 +42,9 @@ fn enable_physics_profiling(mut pipeline: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/bevy_rapier3d/examples/events3.rs b/bevy_rapier3d/examples/events3.rs index b889d861..46681adc 100644 --- a/bevy_rapier3d/examples/events3.rs +++ b/bevy_rapier3d/examples/events3.rs @@ -11,7 +11,7 @@ use ui::DebugUiPlugin; mod ui; fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -36,9 +36,9 @@ fn enable_physics_profiling(mut pipeline: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/bevy_rapier3d/examples/joints3.rs b/bevy_rapier3d/examples/joints3.rs index 7b73586f..65b548ec 100644 --- a/bevy_rapier3d/examples/joints3.rs +++ b/bevy_rapier3d/examples/joints3.rs @@ -14,7 +14,7 @@ use ui::DebugUiPlugin; mod ui; fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -38,9 +38,9 @@ fn enable_physics_profiling(mut pipeline: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/bevy_rapier3d/examples/joints_despawn3.rs b/bevy_rapier3d/examples/joints_despawn3.rs index 9dc6a8cc..996cecc2 100644 --- a/bevy_rapier3d/examples/joints_despawn3.rs +++ b/bevy_rapier3d/examples/joints_despawn3.rs @@ -19,7 +19,7 @@ pub struct DespawnResource { } fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -45,9 +45,9 @@ fn enable_physics_profiling(mut pipeline: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/bevy_rapier3d/examples/locked_rotations3.rs b/bevy_rapier3d/examples/locked_rotations3.rs index 6e273df1..2a4f79df 100644 --- a/bevy_rapier3d/examples/locked_rotations3.rs +++ b/bevy_rapier3d/examples/locked_rotations3.rs @@ -13,7 +13,7 @@ use ui::DebugUiPlugin; mod ui; fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -37,9 +37,9 @@ fn enable_physics_profiling(mut pipeline: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/bevy_rapier3d/examples/multiple_colliders3.rs b/bevy_rapier3d/examples/multiple_colliders3.rs index a4d5bc2e..1e836772 100644 --- a/bevy_rapier3d/examples/multiple_colliders3.rs +++ b/bevy_rapier3d/examples/multiple_colliders3.rs @@ -12,7 +12,7 @@ use ui::DebugUiPlugin; mod ui; fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -36,9 +36,9 @@ fn enable_physics_profiling(mut pipeline: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/bevy_rapier3d/examples/ray_casting3.rs b/bevy_rapier3d/examples/ray_casting3.rs index 1244e943..31c4a6ce 100644 --- a/bevy_rapier3d/examples/ray_casting3.rs +++ b/bevy_rapier3d/examples/ray_casting3.rs @@ -13,7 +13,7 @@ use ui::DebugUiPlugin; mod ui; fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -38,9 +38,9 @@ fn enable_physics_profiling(mut pipeline: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/bevy_rapier3d/examples/static_trimesh3.rs b/bevy_rapier3d/examples/static_trimesh3.rs index 544e2d61..50b347c1 100644 --- a/bevy_rapier3d/examples/static_trimesh3.rs +++ b/bevy_rapier3d/examples/static_trimesh3.rs @@ -15,7 +15,7 @@ use ui::DebugUiPlugin; mod ui; fn main() { - App::build() + App::new() .insert_resource(ClearColor(Color::rgb( 0xF9 as f32 / 255.0, 0xF9 as f32 / 255.0, @@ -41,9 +41,9 @@ fn enable_physics_profiling(mut pipeline: ResMut) { } fn setup_graphics(mut commands: Commands) { - commands.spawn_bundle(LightBundle { + commands.spawn_bundle(PointLightBundle { transform: Transform::from_translation(Vec3::new(100.0, 10.0, 200.0)), - light: Light { + point_light: PointLight { intensity: 100_000.0, range: 3000.0, ..Default::default() diff --git a/src/physics/collider_component_set.rs b/src/physics/collider_component_set.rs index ffc550fc..eacf374a 100644 --- a/src/physics/collider_component_set.rs +++ b/src/physics/collider_component_set.rs @@ -17,93 +17,92 @@ impl IntoEntity for ColliderHandle { } } -pub type QueryPipelineColliderComponentsQuery<'a, 'b> = Query< - 'a, +pub type QueryPipelineColliderComponentsQuery<'world, 'state, 'a> = Query< + 'world, + 'state, ( Entity, - &'b ColliderPosition, - &'b ColliderShape, - &'b ColliderFlags, + &'a ColliderPosition, + &'a ColliderShape, + &'a ColliderFlags, ), >; -pub struct QueryPipelineColliderComponentsSet<'a, 'b, 'c>( - pub &'c QueryPipelineColliderComponentsQuery<'a, 'b>, +pub struct QueryPipelineColliderComponentsSet<'world, 'state, 'a, 'c>( + pub &'c QueryPipelineColliderComponentsQuery<'world, 'state, 'a>, ); -impl_component_set_wo_query_set!( +impl_component_set!( QueryPipelineColliderComponentsSet, ColliderPosition, |data| data.1 ); -impl_component_set_wo_query_set!(QueryPipelineColliderComponentsSet, ColliderShape, |data| { +impl_component_set!(QueryPipelineColliderComponentsSet, ColliderShape, |data| { data.2 }); -impl_component_set_wo_query_set!(QueryPipelineColliderComponentsSet, ColliderFlags, |data| { +impl_component_set!(QueryPipelineColliderComponentsSet, ColliderFlags, |data| { data.3 }); -pub struct ColliderComponentsSet<'a, 'b, 'c>(pub ColliderComponentsQuery<'a, 'b, 'c>); +pub struct ColliderComponentsSet<'world, 'state, 'a>( + pub Query<'world, 'state, ColliderComponentsQueryPayload<'a>>, +); + +pub type ColliderComponentsQueryPayload<'a> = ( + Entity, + &'a mut ColliderChanges, + &'a mut ColliderPosition, + &'a mut ColliderBroadPhaseData, + &'a mut ColliderShape, + &'a mut ColliderType, + &'a mut ColliderMaterial, + &'a mut ColliderFlags, + Option<&'a ColliderParent>, +); -pub type ColliderComponentsQuery<'a, 'b, 'c> = QuerySet<( - Query< - 'a, - ( - Entity, - &'b ColliderChanges, - &'b ColliderPosition, - &'b ColliderBroadPhaseData, - &'b ColliderShape, - &'b ColliderType, - &'b ColliderMaterial, - &'b ColliderFlags, - Option<&'b ColliderParent>, - ), - >, - Query< - 'a, - ( - Entity, - &'c mut ColliderChanges, - &'c mut ColliderPosition, - &'c mut ColliderBroadPhaseData, - ), - >, - Query< - 'a, - ( - Entity, - &'c mut ColliderChanges, - Or<(Changed, Added)>, - Or<(Changed, Added)>, - Or<(Changed, Added)>, - Or<(Changed, Added)>, - Option, Added)>>, - ), - Or<( - Changed, - Added, - Changed, - Added, - Changed, - Added, - Changed, - Added, - Changed, - Added, - )>, - >, -)>; +pub type ColliderChangesQueryPayload<'a> = ( + Entity, + &'a mut ColliderChanges, + Or<(Changed, Added)>, + Or<(Changed, Added)>, + Or<(Changed, Added)>, + Or<(Changed, Added)>, + Option, Added)>>, +); -impl_component_set_mut!(ColliderComponentsSet, ColliderChanges, |data| data.1); -impl_component_set_mut!(ColliderComponentsSet, ColliderPosition, |data| data.2); -impl_component_set_mut!(ColliderComponentsSet, ColliderBroadPhaseData, |d| d.3); +pub type ColliderChangesQueryFilter = ( + Or<( + Changed, + Added, + Changed, + Added, + Changed, + Added, + Changed, + Added, + Changed, + Added, + )>, +); -impl_component_set!(ColliderComponentsSet, ColliderShape, |data| data.4); -impl_component_set!(ColliderComponentsSet, ColliderType, |data| data.5); -impl_component_set!(ColliderComponentsSet, ColliderMaterial, |data| data.6); -impl_component_set!(ColliderComponentsSet, ColliderFlags, |data| data.7); +pub type ColliderComponentsQuerySet<'world, 'state, 'a> = QuerySet< + 'world, + 'state, + ( + // Components query + QueryState>, + // Changes query + QueryState, ColliderChangesQueryFilter>, + ), +>; +impl_component_set_mut!(ColliderComponentsSet, ColliderChanges, |data| &*data.1); +impl_component_set_mut!(ColliderComponentsSet, ColliderPosition, |data| &*data.2); +impl_component_set_mut!(ColliderComponentsSet, ColliderBroadPhaseData, |d| &*d.3); +impl_component_set_mut!(ColliderComponentsSet, ColliderShape, |data| &*data.4); +impl_component_set_mut!(ColliderComponentsSet, ColliderType, |data| &*data.5); +impl_component_set_mut!(ColliderComponentsSet, ColliderMaterial, |data| &*data.6); +impl_component_set_mut!(ColliderComponentsSet, ColliderFlags, |data| &*data.7); impl_component_set_option!(ColliderComponentsSet, ColliderParent); #[derive(Bundle)] diff --git a/src/physics/mod.rs b/src/physics/mod.rs index 2f2e82ec..ee4e1a6a 100644 --- a/src/physics/mod.rs +++ b/src/physics/mod.rs @@ -7,7 +7,7 @@ pub use self::systems::*; use crate::rapier::data::{ComponentSet, ComponentSetMut, ComponentSetOption, Index}; use crate::rapier::prelude::*; -use bevy::prelude::{Entity, Query, QuerySet}; +use bevy::prelude::{Entity, Query}; pub trait IntoHandle { fn handle(self) -> H; @@ -53,14 +53,14 @@ pub trait BundleBuilder { macro_rules! impl_component_set_mut( ($ComponentsSet: ident, $T: ty, |$data: ident| $data_expr: expr) => { - impl<'a, 'b, 'c> ComponentSetOption<$T> for $ComponentsSet<'a, 'b, 'c> { + impl<'world, 'state, 'a> ComponentSetOption<$T> for $ComponentsSet<'world, 'state, 'a> { #[inline(always)] fn get(&self, handle: Index) -> Option<&$T> { - self.0.q0().get_component(handle.entity()).ok() + self.0.get_component(handle.entity()).ok() } } - impl<'a, 'b, 'c> ComponentSet<$T> for $ComponentsSet<'a, 'b, 'c> { + impl<'world, 'state, 'a> ComponentSet<$T> for $ComponentsSet<'world, 'state, 'a> { #[inline(always)] fn size_hint(&self) -> usize { 0 @@ -68,14 +68,14 @@ macro_rules! impl_component_set_mut( #[inline(always)] fn for_each(&self, mut f: impl FnMut(Index, &$T)) { - self.0.q0().for_each(|$data| f($data.0.handle(), $data_expr)) + self.0.iter().for_each(|$data| f($data.0.handle(), $data_expr)) } } - impl<'a, 'b, 'c> ComponentSetMut<$T> for $ComponentsSet<'a, 'b, 'c> { + impl<'world, 'state, 'a> ComponentSetMut<$T> for $ComponentsSet<'world, 'state, 'a> { #[inline(always)] fn set_internal(&mut self, handle: Index, val: $T) { - let _ = self.0.q1_mut().get_component_mut(handle.entity()).map(|mut data| *data = val); + let _ = self.0.get_component_mut(handle.entity()).map(|mut data| *data = val); } #[inline(always)] @@ -84,7 +84,7 @@ macro_rules! impl_component_set_mut( handle: Index, f: impl FnOnce(&mut $T) -> Result, ) -> Option { - self.0.q1_mut() + self.0 .get_component_mut(handle.entity()) .map(|mut data| f(&mut data)) .ok() @@ -93,16 +93,16 @@ macro_rules! impl_component_set_mut( } ); -macro_rules! impl_component_set_wo_query_set( +macro_rules! impl_component_set( ($ComponentsSet: ident, $T: ty, |$data: ident| $data_expr: expr) => { - impl<'a, 'b, 'c> ComponentSetOption<$T> for $ComponentsSet<'a, 'b, 'c> { + impl<'a, 'w, 'b, 'c> ComponentSetOption<$T> for $ComponentsSet<'a, 'w, 'b, 'c> { #[inline(always)] fn get(&self, handle: Index) -> Option<&$T> { self.0.get_component(handle.entity()).ok() } } - impl<'a, 'b, 'c> ComponentSet<$T> for $ComponentsSet<'a, 'b, 'c> { + impl<'a, 'w, 'b, 'c> ComponentSet<$T> for $ComponentsSet<'a, 'w, 'b, 'c> { #[inline(always)] fn size_hint(&self) -> usize { 0 @@ -116,57 +116,36 @@ macro_rules! impl_component_set_wo_query_set( } ); -macro_rules! impl_component_set( - ($ComponentsSet: ident, $T: ty, |$data: ident| $data_expr: expr) => { - impl<'a, 'b, 'c> ComponentSetOption<$T> for $ComponentsSet<'a, 'b, 'c> { - #[inline(always)] - fn get(&self, handle: Index) -> Option<&$T> { - self.0.q0().get_component(handle.entity()).ok() - } - } - - impl<'a, 'b, 'c> ComponentSet<$T> for $ComponentsSet<'a, 'b, 'c> { - #[inline(always)] - fn size_hint(&self) -> usize { - 0 - } - - #[inline(always)] - fn for_each(&self, mut f: impl FnMut(Index, &$T)) { - self.0.q0().for_each(|$data| f($data.0.handle(), $data_expr)) - } - } - } -); - macro_rules! impl_component_set_option( ($ComponentsSet: ident, $T: ty) => { - impl<'a, 'b, 'c> ComponentSetOption<$T> for $ComponentsSet<'a, 'b, 'c> { + impl<'world, 'state, 'a> ComponentSetOption<$T> for $ComponentsSet<'world, 'state, 'a> { #[inline(always)] fn get(&self, handle: Index) -> Option<&$T> { - self.0.q0().get_component(handle.entity()).ok() + self.0.get_component(handle.entity()).ok() } } } ); -pub type ComponentSetQueryMut<'a, 'b, 'c, T> = - QuerySet<(Query<'a, (Entity, &'b T)>, Query<'a, (Entity, &'c mut T)>)>; +pub type ComponentSetQueryMut<'world, 'state, 'a, T> = + Query<'world, 'state, (Entity, &'a mut T)>; -pub struct QueryComponentSetMut<'a, 'b, 'c, T: 'static + Send + Sync>( - ComponentSetQueryMut<'a, 'b, 'c, T>, +pub struct QueryComponentSetMut<'world, 'state, 'a, T: 'static + Send + Sync>( + ComponentSetQueryMut<'world, 'state, 'a, T> ); -impl<'a, 'b, 'c, T: 'static + Send + Sync> ComponentSetOption - for QueryComponentSetMut<'a, 'b, 'c, T> +impl<'world, 'state, 'a, T: 'static + Send + Sync> ComponentSetOption + for QueryComponentSetMut<'world, 'state, 'a, T> { #[inline(always)] fn get(&self, handle: Index) -> Option<&T> { - self.0.q0().get_component(handle.entity()).ok() + self.0.get_component(handle.entity()).ok() } } -impl<'a, 'b, 'c, T: 'static + Send + Sync> ComponentSet for QueryComponentSetMut<'a, 'b, 'c, T> { +impl<'world, 'state, 'a, T: 'static + Send + Sync> ComponentSet + for QueryComponentSetMut<'world, 'state, 'a, T> +{ #[inline(always)] fn size_hint(&self) -> usize { 0 @@ -174,18 +153,17 @@ impl<'a, 'b, 'c, T: 'static + Send + Sync> ComponentSet for QueryComponentSet #[inline(always)] fn for_each(&self, mut f: impl FnMut(Index, &T)) { - self.0.q0().for_each(|data| f(data.0.handle(), &data.1)) + self.0.iter().for_each(|data| f(data.0.handle(), &data.1)) } } -impl<'a, 'b, 'c, T: 'static + Send + Sync> ComponentSetMut - for QueryComponentSetMut<'a, 'b, 'c, T> +impl<'world, 'state, 'a, T: 'static + Send + Sync> ComponentSetMut + for QueryComponentSetMut<'world, 'state, 'a, T> { #[inline(always)] fn set_internal(&mut self, handle: Index, val: T) { let _ = self .0 - .q1_mut() .get_mut(handle.entity()) .map(|mut data| *data.1 = val); } @@ -197,7 +175,6 @@ impl<'a, 'b, 'c, T: 'static + Send + Sync> ComponentSetMut f: impl FnOnce(&mut T) -> Result, ) -> Option { self.0 - .q1_mut() .get_component_mut(handle.entity()) .map(|mut data| f(&mut data)) .ok() diff --git a/src/physics/plugins.rs b/src/physics/plugins.rs index fd9bb8fb..dd944881 100644 --- a/src/physics/plugins.rs +++ b/src/physics/plugins.rs @@ -47,7 +47,7 @@ pub enum PhysicsStages { } impl Plugin for RapierPhysicsPlugin { - fn build(&self, app: &mut AppBuilder) { + fn build(&self, app: &mut App) { app.add_stage_before( CoreStage::PreUpdate, PhysicsStages::FinalizeCreations, @@ -75,41 +75,35 @@ impl Plugin for RapierPhysicsPlugi .add_system_to_stage( PhysicsStages::FinalizeCreations, physics::attach_bodies_and_colliders_system - .system() .label(physics::PhysicsSystems::AttachBodiesAndColliders), ) .add_system_to_stage( PhysicsStages::FinalizeCreations, physics::create_joints_system - .system() .label(physics::PhysicsSystems::CreateJoints), ) .add_system_to_stage( CoreStage::PreUpdate, physics::finalize_collider_attach_to_bodies - .system() .label(physics::PhysicsSystems::FinalizeColliderAttachToBodies), ) .add_system_to_stage( CoreStage::Update, physics::step_world_system:: - .system() .label(physics::PhysicsSystems::StepWorld), ) .add_system_to_stage( PhysicsStages::SyncTransforms, physics::sync_transforms - .system() .label(physics::PhysicsSystems::SyncTransforms), ) .add_system_to_stage( CoreStage::PostUpdate, physics::collect_removals - .system() .label(physics::PhysicsSystems::CollectRemovals), ); if app - .world() + .world .get_resource::>() .is_none() { diff --git a/src/physics/resources.rs b/src/physics/resources.rs index 197a0e58..c40a3179 100644 --- a/src/physics/resources.rs +++ b/src/physics/resources.rs @@ -1,6 +1,7 @@ use crate::physics::{ - ColliderBundle, ColliderComponentsQuery, ColliderComponentsSet, IntoEntity, IntoHandle, - JointHandleComponent, RigidBodyComponentsQuery, RigidBodyComponentsSet, + ColliderBundle, ColliderChangesQueryFilter, ColliderChangesQueryPayload, ColliderComponentsSet, + IntoEntity, IntoHandle, JointHandleComponent, RigidBodyChangesQueryFilter, + RigidBodyChangesQueryPayload, RigidBodyComponentsSet, }; use crate::rapier::prelude::*; use bevy::ecs::query::WorldQuery; @@ -61,9 +62,9 @@ impl Default for RapierConfiguration { /// A set of queues collecting events emitted by the physics engine. pub(crate) struct EventQueue<'a> { /// The unbounded contact event queue. - pub contact_events: RwLock>, + pub contact_events: RwLock>, /// The unbounded intersection event queue. - pub intersection_events: RwLock>, + pub intersection_events: RwLock>, } impl<'a> EventHandler for EventQueue<'a> { @@ -130,20 +131,12 @@ impl ModificationTracker { pub fn detect_modifications( &mut self, - bodies_query: &mut RigidBodyComponentsQuery, - colliders_query: &mut ColliderComponentsQuery, + mut bodies_query: Query, + mut colliders_query: Query, ) { // Detect modifications. - for ( - entity, - mut rb_changes, - mut rb_activation, - rb_pos, - _rb_vels, - _rb_forces, - rb_type, - rb_colliders, - ) in bodies_query.q2_mut().iter_mut() + for (entity, mut rb_activation, mut rb_changes, rb_pos, rb_type, rb_colliders) in + bodies_query.iter_mut() { if !rb_changes.contains(RigidBodyChanges::MODIFIED) { self.modified_bodies.push(entity.handle()); @@ -166,12 +159,8 @@ impl ModificationTracker { rb_activation.wake_up(true); } - for mut rb_changes in bodies_query.q3_mut().iter_mut() { - *rb_changes |= RigidBodyChanges::SLEEP; - } - for (entity, mut co_changes, co_pos, co_groups, co_shape, co_type, co_parent) in - colliders_query.q2_mut().iter_mut() + colliders_query.iter_mut() { if !co_changes.contains(ColliderChanges::MODIFIED) { self.modified_colliders.push(entity.handle()); @@ -313,9 +302,9 @@ pub trait PhysicsHooksWithQuery: Send + Sync { impl PhysicsHooksWithQuery for T where - T: for<'a, 'b, 'c, 'd, 'e, 'f> PhysicsHooks< - RigidBodyComponentsSet<'a, 'b, 'c>, - ColliderComponentsSet<'d, 'e, 'f>, + T: for<'world_a, 'state_a, 'a, 'world_b, 'state_b, 'b> PhysicsHooks< + RigidBodyComponentsSet<'world_a, 'state_a, 'a>, + ColliderComponentsSet<'world_b, 'state_b, 'b>, > + Send + Sync, UserData: WorldQuery, @@ -341,14 +330,14 @@ pub struct PhysicsHooksWithQueryObject( pub Box>, ); -pub(crate) struct PhysicsHooksWithQueryInstance<'a, 'b, UserData: WorldQuery> { - pub user_data: Query<'a, UserData>, +pub(crate) struct PhysicsHooksWithQueryInstance<'world, 'state, 'b, UserData: WorldQuery> { + pub user_data: Query<'world, 'state, UserData>, pub hooks: &'b dyn PhysicsHooksWithQuery, } -impl<'aa, 'bb, 'a, 'b, 'c, 'd, 'e, 'f, UserData: WorldQuery> - PhysicsHooks, ColliderComponentsSet<'d, 'e, 'f>> - for PhysicsHooksWithQueryInstance<'aa, 'bb, UserData> +impl + PhysicsHooks, ColliderComponentsSet<'_, '_, '_>> + for PhysicsHooksWithQueryInstance<'_, '_, '_, UserData> { fn filter_contact_pair( &self, diff --git a/src/physics/rigid_body_component_set.rs b/src/physics/rigid_body_component_set.rs index 8e1e2de6..ec51c87d 100644 --- a/src/physics/rigid_body_component_set.rs +++ b/src/physics/rigid_body_component_set.rs @@ -21,90 +21,76 @@ impl IntoEntity for RigidBodyHandle { } } -pub type RigidBodyComponentsQuery<'a, 'b, 'c> = QuerySet<( - Query< - 'a, - ( - Entity, - &'b RigidBodyPosition, - &'b RigidBodyVelocity, - &'b RigidBodyMassProps, - &'b RigidBodyIds, - &'b RigidBodyForces, - &'b RigidBodyActivation, - &'b RigidBodyChanges, - &'b RigidBodyCcd, - &'b RigidBodyColliders, - &'b RigidBodyDamping, - &'b RigidBodyDominance, - &'b RigidBodyType, - ), - >, - Query< - 'a, - ( - Entity, - &'c mut RigidBodyPosition, - &'c mut RigidBodyVelocity, - &'c mut RigidBodyMassProps, - &'c mut RigidBodyIds, - &'c mut RigidBodyForces, - &'c mut RigidBodyActivation, - &'c mut RigidBodyChanges, - &'c mut RigidBodyCcd, - // Need for handling collider removals. - &'c mut RigidBodyColliders, - ), - >, - Query< - 'a, - ( - Entity, - &'c mut RigidBodyChanges, - &'c mut RigidBodyActivation, - Or<(Changed, Added)>, - Or<(Changed, Added)>, - Or<(Changed, Added)>, - Or<(Changed, Added)>, - Or<(Changed, Added)>, - ), - Or<( - Changed, - Added, - Changed, - Added, - Changed, - Added, - Changed, - Added, - Changed, - Added, - Changed, - Added, - )>, - >, - Query< - 'a, - &'c mut RigidBodyChanges, - Or<(Changed, Added)>, - >, -)>; +pub type RigidBodyComponentsQueryPayload<'a> = ( + Entity, + &'a mut RigidBodyPosition, + &'a mut RigidBodyVelocity, + &'a mut RigidBodyMassProps, + &'a mut RigidBodyIds, + &'a mut RigidBodyForces, + &'a mut RigidBodyCcd, + &'a mut RigidBodyColliders, + &'a mut RigidBodyDamping, + &'a mut RigidBodyDominance, + &'a mut RigidBodyType, + &'a mut RigidBodyChanges, + &'a mut RigidBodyActivation, +); -pub struct RigidBodyComponentsSet<'a, 'b, 'c>(pub RigidBodyComponentsQuery<'a, 'b, 'c>); +pub type RigidBodyChangesQueryPayload<'a> = ( + Entity, + &'a mut RigidBodyActivation, + &'a mut RigidBodyChanges, + Or<(Changed, Added)>, + Or<(Changed, Added)>, + Or<(Changed, Added)>, +); -impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyPosition, |data| data.1); -impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyVelocity, |data| data.2); -impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyMassProps, |data| data.3); -impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyIds, |data| data.4); -impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyForces, |data| data.5); -impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyActivation, |data| data.6); -impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyChanges, |data| data.7); -impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyCcd, |data| data.8); -impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyColliders, |data| data.9); +pub type RigidBodyChangesQueryFilter = ( + Or<( + Changed, + Added, + Changed, + Added, + Changed, + Added, + Changed, + Added, + Changed, + Added, + Changed, + Added, + )>, +); -impl_component_set!(RigidBodyComponentsSet, RigidBodyDamping, |data| data.10); -impl_component_set!(RigidBodyComponentsSet, RigidBodyDominance, |data| data.11); -impl_component_set!(RigidBodyComponentsSet, RigidBodyType, |data| data.12); +pub type RigidBodyComponentsQuerySet<'world, 'state, 'a> = QuerySet< + 'world, + 'state, + ( + // Components query + QueryState>, + // Changes query + QueryState, RigidBodyChangesQueryFilter>, + ), +>; + +pub struct RigidBodyComponentsSet<'world, 'state, 'a>( + pub Query<'world, 'state, RigidBodyComponentsQueryPayload<'a>>, +); + +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyPosition, |data| &*data.1); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyVelocity, |data| &*data.2); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyMassProps, |data| &*data.3); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyIds, |data| &*data.4); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyForces, |data| &*data.5); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyCcd, |data| &*data.6); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyColliders, |data| &*data.7); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyDamping, |data| &*data.8); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyDominance, |data| &*data.9); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyType, |data| &*data.10); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyChanges, |data| &*data.11); +impl_component_set_mut!(RigidBodyComponentsSet, RigidBodyActivation, |data| &*data + .12); #[derive(Bundle)] pub struct RigidBodyBundle { diff --git a/src/physics/systems.rs b/src/physics/systems.rs index 13f35218..7533d7ea 100644 --- a/src/physics/systems.rs +++ b/src/physics/systems.rs @@ -1,9 +1,9 @@ use crate::physics::{ - ColliderComponentsQuery, ColliderComponentsSet, ColliderPositionSync, ComponentSetQueryMut, + ColliderComponentsQuerySet, ColliderComponentsSet, ColliderPositionSync, ComponentSetQueryMut, EventQueue, IntoEntity, IntoHandle, JointBuilderComponent, JointHandleComponent, JointsEntityMap, ModificationTracker, PhysicsHooksWithQueryInstance, PhysicsHooksWithQueryObject, QueryComponentSetMut, RapierConfiguration, - RigidBodyComponentsQuery, RigidBodyComponentsSet, RigidBodyPositionSync, + RigidBodyComponentsQuerySet, RigidBodyComponentsSet, RigidBodyPositionSync, SimulationToRenderTime, TimestepMode, }; @@ -18,7 +18,7 @@ use crate::rapier::geometry::{ ColliderShape, }; use crate::rapier::pipeline::QueryPipeline; -use bevy::ecs::query::WorldQuery; +use bevy::ecs::query::{QueryState, WorldQuery}; use bevy::prelude::*; use rapier::dynamics::{CCDSolver, IntegrationParameters, IslandManager, JointSet}; use rapier::geometry::{BroadPhase, NarrowPhase}; @@ -164,12 +164,10 @@ pub fn create_joints_system( // Make sure the rigid-bodies the joint it attached to exist. if bodies .0 - .q0() .get_component::(joint.entity1) .is_err() || bodies .0 - .q0() .get_component::(joint.entity2) .is_err() { @@ -221,8 +219,8 @@ pub fn step_world_system( ), user_data: Query, mut position_sync_query: Query<(Entity, &mut RigidBodyPositionSync)>, - bodies_query: RigidBodyComponentsQuery, - colliders_query: ColliderComponentsQuery, + mut bodies_query: RigidBodyComponentsQuerySet, + mut colliders_query: ColliderComponentsQuerySet, (removed_bodies, removed_colliders, removed_joints): ( RemovedComponents, RemovedComponents, @@ -235,14 +233,15 @@ pub fn step_world_system( intersection_events: RwLock::new(intersection_events), contact_events: RwLock::new(contact_events), }; - let mut rigid_body_components_set = RigidBodyComponentsSet(bodies_query); - let mut collider_components_set = ColliderComponentsSet(colliders_query); - modifs_tracker.detect_removals(removed_bodies, removed_colliders, removed_joints); modifs_tracker.detect_modifications( - &mut rigid_body_components_set.0, - &mut collider_components_set.0, + bodies_query.q1(), + colliders_query.q1(), ); + + let mut rigid_body_components_set = RigidBodyComponentsSet(bodies_query.q0()); + let mut collider_components_set = ColliderComponentsSet(colliders_query.q0()); + modifs_tracker.propagate_removals( &mut commands, &mut islands, @@ -351,14 +350,16 @@ pub(crate) fn sync_transform(pos: &Isometry, scale: f32, transform: &mut Tr // Do not touch the 'z' part of the translation, used in Bevy for 2d layering transform.translation.x = tra.x * scale; transform.translation.y = tra.y * scale; - transform.rotation = rot; + transform.rotation = Quat::from_xyzw(rot.x, rot.y, rot.z, rot.w); } #[cfg(feature = "dim3")] pub(crate) fn sync_transform(pos: &Isometry, scale: f32, transform: &mut Transform) { let (tra, rot) = (*pos).into(); - transform.translation = tra * scale; - transform.rotation = rot; + transform.translation.x = tra.x * scale; + transform.translation.y = tra.y * scale; + transform.translation.z = tra.z * scale; + transform.rotation = Quat::from_xyzw(rot.x, rot.y, rot.z, rot.w); } /// System responsible for writing the rigid-bodies positions into the Bevy translation and rotation components. @@ -370,14 +371,14 @@ pub fn sync_transforms( rigid_body_sync_mode: Query<&RigidBodyPositionSync>, // TODO: add some Changed filters to only sync when something moved? mut sync_query: QuerySet<( - Query<( + QueryState<( Entity, &RigidBodyPosition, &RigidBodyPositionSync, Option<&mut Transform>, Option<&mut GlobalTransform>, )>, - Query<( + QueryState<( Entity, &ColliderPosition, &ColliderPositionSync, @@ -392,9 +393,7 @@ pub fn sync_transforms( let alpha = dt / sim_dt; // Sync bodies. - for (entity, rb_pos, sync_mode, mut transform, global_transform) in - sync_query.q0_mut().iter_mut() - { + for (entity, rb_pos, sync_mode, mut transform, global_transform) in sync_query.q0().iter_mut() { let mut new_transform = transform .as_deref_mut() .map(|t| t.clone()) @@ -431,7 +430,7 @@ pub fn sync_transforms( // Sync colliders. for (entity, co_pos, _, co_parent, mut transform, mut global_transform) in - sync_query.q1_mut().iter_mut() + QuerySet::<(_, _)>::q1(&mut sync_query).iter_mut() { let mut new_transform = transform .as_deref_mut() diff --git a/src/render/plugins.rs b/src/render/plugins.rs index 4866ceac..46b56e9c 100644 --- a/src/render/plugins.rs +++ b/src/render/plugins.rs @@ -5,7 +5,7 @@ use bevy::prelude::*; pub struct RapierRenderPlugin; impl Plugin for RapierRenderPlugin { - fn build(&self, app: &mut AppBuilder) { + fn build(&self, app: &mut App) { app.add_system_to_stage( CoreStage::PreUpdate, systems::create_collider_renders_system diff --git a/src/render/systems.rs b/src/render/systems.rs index b307e08c..9a424c63 100644 --- a/src/render/systems.rs +++ b/src/render/systems.rs @@ -172,7 +172,7 @@ fn generate_collider_mesh(co_shape: &ColliderShape) -> Option<(Mesh, Vec3)> { #[cfg(feature = "dim3")] ShapeType::Cuboid => { let c = co_shape.as_cuboid().unwrap(); - Vec3::from_slice_unaligned(c.half_extents.as_slice()) + Vec3::new(c.half_extents.x, c.half_extents.y, c.half_extents.z) } ShapeType::Ball => { let b = co_shape.as_ball().unwrap(); diff --git a/src_debug_ui/plugins.rs b/src_debug_ui/plugins.rs index 73c173ae..ad38b962 100644 --- a/src_debug_ui/plugins.rs +++ b/src_debug_ui/plugins.rs @@ -4,7 +4,7 @@ use bevy::prelude::*; pub struct DebugUiPlugin; impl Plugin for DebugUiPlugin { - fn build(&self, app: &mut AppBuilder) { + fn build(&self, app: &mut App) { app.add_startup_system(systems::setup_ui.system()) .add_system_to_stage(CoreStage::Update, systems::text_update_system.system()); }