Skip to content

Commit

Permalink
Merge pull request #613 from Vrixyz/picking_backend
Browse files Browse the repository at this point in the history
Picking backend for bevy 0.15
  • Loading branch information
Vrixyz authored Feb 3, 2025
2 parents 4c04cf7 + 371b12b commit a176d80
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ jobs:
- name: Clippy for bevy_rapier3d
run: cargo clippy --verbose -p bevy_rapier3d
- name: Clippy for bevy_rapier2d (debug-render, simd, serde)
run: cargo clippy --verbose -p bevy_rapier2d --features debug-render-2d,simd-stable,serde-serialize
run: cargo clippy --verbose -p bevy_rapier2d --features debug-render-2d,simd-stable,serde-serialize,picking-backend
- name: Clippy for bevy_rapier3d (debug-render, simd, serde)
run: cargo clippy --verbose -p bevy_rapier3d --features debug-render-3d,simd-stable,serde-serialize
run: cargo clippy --verbose -p bevy_rapier3d --features debug-render-3d,simd-stable,serde-serialize,picking-backend
- name: Test for bevy_rapier2d
run: cargo test --verbose -p bevy_rapier2d
- name: Test for bevy_rapier3d
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Added

- Added optional feature `picking-backend` to support bevy_picking.
- See `picking_backend` module documentation for more details.

### Modified

- Update from rapier `0.22` to rapier `0.23`,
Expand Down
3 changes: 2 additions & 1 deletion bevy_rapier2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rust.unexpected_cfgs = { level = "warn", check-cfg = [
clippy = { needless_lifetimes = "allow" }

[features]
default = ["dim2", "async-collider", "debug-render-2d"]
default = ["dim2", "async-collider", "debug-render-2d", "picking-backend"]
dim2 = []
debug-render-2d = [
"bevy/bevy_core_pipeline",
Expand All @@ -48,6 +48,7 @@ simd-nightly = ["rapier2d/simd-nightly"]
serde-serialize = ["rapier2d/serde-serialize", "bevy/serialize", "serde"]
enhanced-determinism = ["rapier2d/enhanced-determinism"]
headless = []
picking-backend = ["bevy/bevy_picking"]
async-collider = [
"bevy/bevy_asset",
"bevy/bevy_scene",
Expand Down
15 changes: 13 additions & 2 deletions bevy_rapier3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rust.unexpected_cfgs = { level = "warn", check-cfg = [
clippy = { needless_lifetimes = "allow" }

[features]
default = ["dim3", "async-collider", "debug-render-3d"]
default = ["dim3", "async-collider", "debug-render-3d", "picking-backend"]
dim3 = []
debug-render = ["debug-render-3d"]
debug-render-2d = [
Expand All @@ -49,6 +49,7 @@ simd-nightly = ["rapier3d/simd-nightly"]
serde-serialize = ["rapier3d/serde-serialize", "bevy/serialize", "serde"]
enhanced-determinism = ["rapier3d/enhanced-determinism"]
headless = []
picking-backend = ["bevy/bevy_picking"]
async-collider = [
"bevy/bevy_asset",
"bevy/bevy_scene",
Expand All @@ -71,6 +72,9 @@ bevy = { version = "0.15", default-features = false, features = [
"tonemapping_luts",
"bevy_state",
"bevy_debug_stepping",
"bevy_text",
"bevy_ui",
"default_font",
] }
approx = "0.5.1"
glam = { version = "0.29", features = ["approx"] }
Expand All @@ -79,4 +83,11 @@ bevy_egui = "0.31"

[package.metadata.docs.rs]
# Enable all the features when building the docs on docs.rs
features = ["debug-render-3d", "serde-serialize"]

[[example]]
name = "picking3"
required-features = ["picking-backend"]

[[example]]
name = "testbed3"
required-features = ["picking-backend"]
100 changes: 100 additions & 0 deletions bevy_rapier3d/examples/picking3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! A simple scene to demonstrate picking events for rapier [`Collider`] entities.
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

fn main() {
App::new()
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
RapierPickingPlugin::default(),
))
.insert_resource(RapierPickingSettings {
// Optional: only needed when you want fine-grained control
// over which cameras and entities should be used with the rapier picking backend.
// This is disabled by default, and no marker components are required on cameras or colliders.
// This resource is inserted by default,
// you only need to add it if you want to override the default settings.
require_markers: true,
..Default::default()
})
.add_systems(Startup, (setup_graphics, setup_physics))
.run();
}

pub fn setup_graphics(mut commands: Commands) {
// Camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
RapierPickable,
));
}

pub fn setup_physics(mut commands: Commands) {
commands
.spawn((
Text::new("Click Me to get a box\nDrag cubes to rotate"),
Node {
position_type: PositionType::Absolute,
top: Val::Percent(12.0),
left: Val::Percent(12.0),
..default()
},
))
.observe(on_click_spawn_cube)
.observe(
|out: Trigger<Pointer<Out>>, mut texts: Query<&mut TextColor>| {
let mut text_color = texts.get_mut(out.entity()).unwrap();
text_color.0 = Color::WHITE;
},
)
.observe(
|over: Trigger<Pointer<Over>>, mut texts: Query<&mut TextColor>| {
let mut color = texts.get_mut(over.entity()).unwrap();
color.0 = bevy::color::palettes::tailwind::CYAN_400.into();
},
);
// Base
let ground_size = 3.1;
let ground_height = 0.1;
commands.spawn((
Transform::from_xyz(0.0, -ground_height / 2.0, 0.0),
Collider::cuboid(ground_size, ground_height, ground_size),
ColliderDebugColor(Hsla::BLACK),
));
}

fn on_click_spawn_cube(
_click: Trigger<Pointer<Click>>,
mut commands: Commands,
mut num: Local<usize>,
) {
let rad = 0.25;
let colors = [
Hsla::hsl(220.0, 1.0, 0.3),
Hsla::hsl(180.0, 1.0, 0.3),
Hsla::hsl(260.0, 1.0, 0.7),
];
commands
.spawn((
Transform::from_xyz(0.0, 0.25 + 0.55 * *num as f32, 0.0),
Visibility::default(),
RigidBody::Dynamic,
Collider::cuboid(rad, rad, rad),
ColliderDebugColor(colors[*num % 3]),
RapierPickable,
))
// With the RapierPickingPlugin added, you can add pointer event observers to colliders:
.observe(on_drag_rotate);
*num += 1;
}

fn on_drag_rotate(drag: Trigger<Pointer<Drag>>, mut transforms: Query<&mut Transform>) {
if let Ok(mut transform) = transforms.get_mut(drag.entity()) {
transform.rotate_y(drag.delta.x * 0.02);
transform.rotate_x(drag.delta.y * 0.02);
}
}
11 changes: 11 additions & 0 deletions bevy_rapier3d/examples/testbed3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod joints3;
mod joints_despawn3;
mod locked_rotations3;
mod multiple_colliders3;
mod picking3;
mod ray_casting3;
mod static_trimesh3;

Expand All @@ -28,6 +29,7 @@ pub enum Examples {
JointsDespawn3,
LockedRotations3,
MultipleColliders3,
Picking3,
Raycasting3,
StaticTrimesh3,
}
Expand Down Expand Up @@ -64,6 +66,7 @@ fn main() {
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
WorldInspectorPlugin::new(),
RapierPickingPlugin::default(),
))
.register_type::<Examples>()
.register_type::<ExamplesRes>()
Expand All @@ -78,6 +81,7 @@ fn main() {
(Examples::JointsDespawn3, "JointsDespawn3").into(),
(Examples::LockedRotations3, "LockedRotations3").into(),
(Examples::MultipleColliders3, "MultipleColliders3").into(),
(Examples::Picking3, "Picking3").into(),
(Examples::Raycasting3, "Raycasting3").into(),
(Examples::StaticTrimesh3, "StaticTrimesh3").into(),
]))
Expand Down Expand Up @@ -173,6 +177,13 @@ fn main() {
)
.add_systems(OnExit(Examples::MultipleColliders3), cleanup)
//
// picking
.add_systems(
OnEnter(Examples::Picking3),
(picking3::setup_graphics, picking3::setup_physics),
)
.add_systems(OnExit(Examples::Picking3), cleanup)
//
// raycasting
.add_systems(
OnEnter(Examples::Raycasting3),
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! Rapier is a set of two Rust crates `rapier2d` and `rapier3d` for efficient cross-platform
//! physics simulation. Its target application include video games, animation, robotics, etc.
//!
//! The `bevy_rapier` projects implements two other crates `bevy_rapier2d` and `bevy_rapier3d` which
//! The `bevy_rapier` project implements two crates `bevy_rapier2d` and `bevy_rapier3d` which
//! define physics plugins for the Bevy game engine.
//!
//! User documentation for `bevy_rapier` is on [the official Rapier site](https://rapier.rs/docs/).
Expand Down Expand Up @@ -57,6 +57,9 @@ pub mod pipeline;
/// The physics plugin and systems.
pub mod plugin;

#[cfg(feature = "picking-backend")]
pub mod picking_backend;

/// Components related to character control.
pub mod control;
/// The debug-renderer.
Expand All @@ -71,6 +74,8 @@ pub mod prelude {
pub use crate::dynamics::*;
pub use crate::geometry::*;
pub use crate::math::*;
#[cfg(feature = "picking-backend")]
pub use crate::picking_backend::*;
pub use crate::pipeline::*;
pub use crate::plugin::context::systemparams::*;
pub use crate::plugin::context::*;
Expand Down
Loading

0 comments on commit a176d80

Please sign in to comment.