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

Update to Bevy 0.10 #324

Merged
merged 19 commits into from Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions bevy_rapier2d/Cargo.toml
Expand Up @@ -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"] }
irate-devil marked this conversation as resolved.
Show resolved Hide resolved
# 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"
Expand All @@ -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" ] }
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier2d/examples/events2.rs
Expand Up @@ -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();
}

Expand Down
11 changes: 5 additions & 6 deletions 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)
Expand Down
5 changes: 3 additions & 2 deletions bevy_rapier3d/Cargo.toml
Expand Up @@ -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"
Expand All @@ -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" ] }

Expand Down
25 changes: 8 additions & 17 deletions bevy_rapier3d/examples/custom_system_setup3.rs
Expand Up @@ -55,39 +55,30 @@ fn main() {
SpecialStagingPlugin::new(
Schedule::default()
.with_stage(
PhysicsStages::SyncBackend,
PhysicsSet::SyncBackend,
SystemStage::parallel().with_system_set(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsStages::SyncBackend),
RapierPhysicsPlugin::<NoUserData>::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::<NoUserData>::get_systems(
PhysicsStages::StepSimulation,
PhysicsSet::StepSimulation,
)),
)
.with_stage_after(
PhysicsStages::StepSimulation,
PhysicsStages::Writeback,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
SystemStage::parallel().with_system_set(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsStages::Writeback),
RapierPhysicsPlugin::<NoUserData>::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::<NoUserData>::get_systems(
PhysicsStages::DetectDespawn,
)),
);

app.add_plugin(RapierPhysicsPlugin::<NoUserData>::default().with_default_system_setup(false));

app.run();
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier3d/examples/events3.rs
Expand Up @@ -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();
}

Expand Down
39 changes: 10 additions & 29 deletions bevy_rapier3d/examples/ray_casting3.rs
@@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_rapier3d::prelude::*;

fn main() {
Expand Down Expand Up @@ -72,20 +72,23 @@ pub fn setup_physics(mut commands: Commands) {

fn cast_ray(
mut commands: Commands,
windows: Res<Windows>,
windows: Query<&Window>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you add With<PrimaryWindow> filter here to keep the previous behavior? I see you were calling get_primary() to get the window.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I merged right before seeing your comment. I think this is a good point. I’ll make the change with the release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example already didn't support multiple windows so it's effectively the same.
I just followed what Bevy's examples do, but I'll leave it up to seb

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries! I'm anxious for this new release tbh. 😆

Thanks everyone!

rapier_context: Res<RapierContext>,
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(),
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions src/pipeline/events.rs
Expand Up @@ -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<ColliderHandle, Entity>,
pub collision_events: RwLock<EventWriter<'a, 'a, CollisionEvent>>,
pub contact_force_events: RwLock<EventWriter<'a, 'a, ContactForceEvent>>,
pub collision_events: RwLock<EventWriter<'a, CollisionEvent>>,
pub contact_force_events: RwLock<EventWriter<'a, ContactForceEvent>>,
}

impl<'a> EventQueue<'a> {
Expand Down
4 changes: 2 additions & 2 deletions src/pipeline/physics_hooks.rs
@@ -1,5 +1,5 @@
use bevy::{
ecs::system::{SystemParam, SystemParamFetch, SystemParamItem},
ecs::system::{SystemParam, SystemParamItem},
prelude::*,
};
use rapier::{
Expand Down Expand Up @@ -168,7 +168,7 @@ pub trait BevyPhysicsHooks: SystemParam + Send + Sync {
impl<T> 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<Item<'w, 's> = T>,
{
fn filter_contact_pair(&self, context: PairFilterContextView) -> Option<SolverFlags> {
PhysicsHooks::filter_contact_pair(self, context.raw)
Expand Down
2 changes: 1 addition & 1 deletion 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)]
Expand Down