From 76f046ced0351b906d66c9ffa2d6107617a334c5 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 14 Jan 2025 16:15:43 +0100 Subject: [PATCH 1/5] update rapier to 0.23 --- .github/workflows/main.yml | 4 ++-- bevy_rapier2d/Cargo.toml | 1 - bevy_rapier3d/Cargo.toml | 3 +-- bevy_rapier_benches3d/Cargo.toml | 2 +- src/geometry/collider_impl.rs | 19 ++++++++++++------- src/plugin/systems/joint.rs | 2 +- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f966b503..cf14f105 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -72,6 +72,6 @@ jobs: with: prefix-key: ${{ env.RUST_CACHE_KEY }} - name: Clippy bevy_rapier2d - run: cd bevy_rapier2d && cargo clippy --verbose --features wasm-bindgen,bevy/webgl2 --target wasm32-unknown-unknown + run: cd bevy_rapier2d && cargo clippy --verbose --features bevy/webgl2 --target wasm32-unknown-unknown - name: Clippy bevy_rapier3d - run: cd bevy_rapier3d && cargo clippy --verbose --features wasm-bindgen,bevy/webgl2 --target wasm32-unknown-unknown + run: cd bevy_rapier3d && cargo clippy --verbose --features bevy/webgl2 --target wasm32-unknown-unknown diff --git a/bevy_rapier2d/Cargo.toml b/bevy_rapier2d/Cargo.toml index 4a626709..e7a20c5b 100644 --- a/bevy_rapier2d/Cargo.toml +++ b/bevy_rapier2d/Cargo.toml @@ -45,7 +45,6 @@ rapier-debug-render = ["rapier2d/debug-render"] parallel = ["rapier2d/parallel"] simd-stable = ["rapier2d/simd-stable"] simd-nightly = ["rapier2d/simd-nightly"] -wasm-bindgen = ["rapier2d/wasm-bindgen"] serde-serialize = ["rapier2d/serde-serialize", "bevy/serialize", "serde"] enhanced-determinism = ["rapier2d/enhanced-determinism"] headless = [] diff --git a/bevy_rapier3d/Cargo.toml b/bevy_rapier3d/Cargo.toml index b5d18c47..aa980fd6 100644 --- a/bevy_rapier3d/Cargo.toml +++ b/bevy_rapier3d/Cargo.toml @@ -46,7 +46,6 @@ rapier-debug-render = ["rapier3d/debug-render"] parallel = ["rapier3d/parallel"] simd-stable = ["rapier3d/simd-stable"] simd-nightly = ["rapier3d/simd-nightly"] -wasm-bindgen = ["rapier3d/wasm-bindgen"] serde-serialize = ["rapier3d/serde-serialize", "bevy/serialize", "serde"] enhanced-determinism = ["rapier3d/enhanced-determinism"] headless = [] @@ -55,7 +54,7 @@ async-collider = ["bevy/bevy_asset", "bevy/bevy_scene", "bevy/bevy_render"] [dependencies] bevy = { version = "0.15", default-features = false } nalgebra = { version = "0.33", features = ["convert-glam029"] } -rapier3d = "0.22" +rapier3d = "0.23" bitflags = "2.4" log = "0.4" serde = { version = "1", features = ["derive"], optional = true } diff --git a/bevy_rapier_benches3d/Cargo.toml b/bevy_rapier_benches3d/Cargo.toml index 80c325df..6b4689b4 100644 --- a/bevy_rapier_benches3d/Cargo.toml +++ b/bevy_rapier_benches3d/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rapier3d = { features = ["profiler"], version = "0.22" } +rapier3d = { features = ["profiler"], version = "0.23" } bevy_rapier3d = { version = "0.28", path = "../bevy_rapier3d" } bevy = { version = "0.15", default-features = false } diff --git a/src/geometry/collider_impl.rs b/src/geometry/collider_impl.rs index 88ead96e..c8892249 100644 --- a/src/geometry/collider_impl.rs +++ b/src/geometry/collider_impl.rs @@ -151,9 +151,12 @@ impl Collider { } /// Initializes a collider with a triangle mesh shape defined by its vertex and index buffers. - pub fn trimesh(vertices: Vec, indices: Vec<[u32; 3]>) -> Self { + pub fn trimesh( + vertices: Vec, + indices: Vec<[u32; 3]>, + ) -> Result { let vertices = vertices.into_iter().map(|v| v.into()).collect(); - SharedShape::trimesh(vertices, indices).into() + Ok(SharedShape::trimesh(vertices, indices)?.into()) } /// Initializes a collider with a triangle mesh shape defined by its vertex and index buffers, and flags @@ -162,9 +165,9 @@ impl Collider { vertices: Vec, indices: Vec<[u32; 3]>, flags: TriMeshFlags, - ) -> Self { + ) -> Result { let vertices = vertices.into_iter().map(|v| v.into()).collect(); - SharedShape::trimesh_with_flags(vertices, indices, flags).into() + Ok(SharedShape::trimesh_with_flags(vertices, indices, flags)?.into()) } /// Initializes a collider with a Bevy Mesh. @@ -175,9 +178,11 @@ impl Collider { let (vtx, idx) = extract_mesh_vertices_indices(mesh)?; match collider_shape { - ComputedColliderShape::TriMesh(flags) => { - Some(SharedShape::trimesh_with_flags(vtx, idx, *flags).into()) - } + ComputedColliderShape::TriMesh(flags) => Some( + SharedShape::trimesh_with_flags(vtx, idx, *flags) + .ok()? + .into(), + ), ComputedColliderShape::ConvexHull => { SharedShape::convex_hull(&vtx).map(|shape| shape.into()) } diff --git a/src/plugin/systems/joint.rs b/src/plugin/systems/joint.rs index c62b7774..9bd364b5 100644 --- a/src/plugin/systems/joint.rs +++ b/src/plugin/systems/joint.rs @@ -138,7 +138,7 @@ pub fn apply_joint_user_changes( // Re-parenting the joint isn’t supported yet. for (link, handle, changed_joint) in changed_impulse_joints.iter() { let mut context = context.get_mut(link.0).expect(RAPIER_CONTEXT_EXPECT_ERROR); - if let Some(joint) = context.impulse_joints.get_mut(handle.0) { + if let Some(joint) = context.impulse_joints.get_mut(handle.0, false) { joint.data = changed_joint.data.as_ref().into_rapier(); } } From 62d6033e48517ffae0260eb2a8bc8767792ea79b Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 15 Jan 2025 10:25:19 +0100 Subject: [PATCH 2/5] fix tests + add unit tests without default features in CI --- .github/workflows/main.yml | 4 +++ bevy_rapier2d/Cargo.toml | 9 +++++-- bevy_rapier3d/Cargo.toml | 7 ++++- bevy_rapier3d/examples/static_trimesh3.rs | 4 +-- bevy_rapier_benches3d/src/lib.rs | 8 ++++-- src/plugin/systems/mod.rs | 31 ++++++----------------- 6 files changed, 33 insertions(+), 30 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cf14f105..16723ac3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -58,6 +58,10 @@ jobs: run: cargo test --verbose -p bevy_rapier2d - name: Test for bevy_rapier3d run: cargo test --verbose -p bevy_rapier3d + - name: Unit Tests for bevy_rapier3d without default features + run: cargo test -p bevy_rapier3d --no-default-features --features dim3 --lib + - name: Unit Tests for bevy_rapier3d without default features + run: cargo test -p bevy_rapier3d --no-default-features --features dim3 --lib test-wasm: runs-on: ubuntu-latest env: diff --git a/bevy_rapier2d/Cargo.toml b/bevy_rapier2d/Cargo.toml index e7a20c5b..b4d1610e 100644 --- a/bevy_rapier2d/Cargo.toml +++ b/bevy_rapier2d/Cargo.toml @@ -48,12 +48,17 @@ simd-nightly = ["rapier2d/simd-nightly"] serde-serialize = ["rapier2d/serde-serialize", "bevy/serialize", "serde"] enhanced-determinism = ["rapier2d/enhanced-determinism"] headless = [] -async-collider = ["bevy/bevy_asset", "bevy/bevy_scene", "bevy/bevy_render"] +async-collider = [ + "bevy/bevy_asset", + "bevy/bevy_scene", + "bevy/bevy_render", + "bevy/bevy_image", +] [dependencies] bevy = { version = "0.15", default-features = false } nalgebra = { version = "0.33", features = ["convert-glam029"] } -rapier2d = "0.22" +rapier2d = "0.23" bitflags = "2.4" log = "0.4" serde = { version = "1", features = ["derive"], optional = true } diff --git a/bevy_rapier3d/Cargo.toml b/bevy_rapier3d/Cargo.toml index aa980fd6..1f0aa2ef 100644 --- a/bevy_rapier3d/Cargo.toml +++ b/bevy_rapier3d/Cargo.toml @@ -49,7 +49,12 @@ simd-nightly = ["rapier3d/simd-nightly"] serde-serialize = ["rapier3d/serde-serialize", "bevy/serialize", "serde"] enhanced-determinism = ["rapier3d/enhanced-determinism"] headless = [] -async-collider = ["bevy/bevy_asset", "bevy/bevy_scene", "bevy/bevy_render"] +async-collider = [ + "bevy/bevy_asset", + "bevy/bevy_scene", + "bevy/bevy_render", + "bevy/bevy_image", +] [dependencies] bevy = { version = "0.15", default-features = false } diff --git a/bevy_rapier3d/examples/static_trimesh3.rs b/bevy_rapier3d/examples/static_trimesh3.rs index 27fae6b5..5bc41382 100644 --- a/bevy_rapier3d/examples/static_trimesh3.rs +++ b/bevy_rapier3d/examples/static_trimesh3.rs @@ -55,7 +55,7 @@ pub fn setup_physics(mut commands: Commands, mut ball_state: ResMut) indices.push([2 * i + 2, 2 * i + 1, 2 * i + 3]); } - commands.spawn(Collider::trimesh(vertices, indices)); + commands.spawn(Collider::trimesh(vertices, indices).unwrap()); // Create a bowl with a cosine cross-section, // so that we can join the end of the ramp smoothly @@ -97,7 +97,7 @@ pub fn setup_physics(mut commands: Commands, mut ball_state: ResMut) -bowl_size.y / 2.0, bowl_size.z / 2.0 - ramp_size.z / 2.0, ), - Collider::trimesh(vertices, indices), + Collider::trimesh(vertices, indices).unwrap(), )); } diff --git a/bevy_rapier_benches3d/src/lib.rs b/bevy_rapier_benches3d/src/lib.rs index bd6b16b8..02709e02 100644 --- a/bevy_rapier_benches3d/src/lib.rs +++ b/bevy_rapier_benches3d/src/lib.rs @@ -25,7 +25,7 @@ pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) { timer_full_update.start(); app.update(); timer_full_update.pause(); - let elapsed_time = timer_full_update.time() as f32; + let elapsed_time = timer_full_update.time().as_millis(); let rc = app .world_mut() .query::<&RapierContextSimulation>() @@ -34,7 +34,11 @@ pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) { total_update_times.push(elapsed_time); } timer_total.pause(); - let average_total = total_update_times.iter().sum::() / total_update_times.len() as f32; + let average_total = total_update_times + .iter() + .map(|time| time.as_millis() as f32) + .sum::() + / total_update_times.len() as f32; println!("average total time: {} ms", average_total); let average_rapier_step = rapier_step_times.iter().sum::() / rapier_step_times.len() as f32; diff --git a/src/plugin/systems/mod.rs b/src/plugin/systems/mod.rs index 420876f4..4b5c7d7c 100644 --- a/src/plugin/systems/mod.rs +++ b/src/plugin/systems/mod.rs @@ -93,16 +93,7 @@ pub fn step_simulation( #[cfg(test)] #[allow(missing_docs)] pub mod tests { - use bevy::{ - asset::AssetPlugin, - ecs::event::Events, - render::{ - settings::{RenderCreation, WgpuSettings}, - RenderPlugin, - }, - scene::ScenePlugin, - time::TimePlugin, - }; + use bevy::{ecs::event::Events, time::TimePlugin}; use rapier::geometry::CollisionEventFlags; use std::f32::consts::PI; @@ -347,19 +338,13 @@ pub mod tests { pub struct HeadlessRenderPlugin; impl Plugin for HeadlessRenderPlugin { - fn build(&self, app: &mut App) { - app.add_plugins(( - AssetPlugin::default(), - ScenePlugin, - RenderPlugin { - render_creation: RenderCreation::Automatic(WgpuSettings { - backends: None, - ..Default::default() - }), - ..Default::default() - }, - ImagePlugin::default(), - )); + fn build(&self, _app: &mut App) { + #[cfg(feature = "async-collider")] + { + use bevy::{asset::AssetPlugin, render::mesh::MeshPlugin, scene::ScenePlugin}; + + _app.add_plugins((AssetPlugin::default(), MeshPlugin, ScenePlugin)); + } } } } From 71a05334588c2f3880d2f2f26d2b9640c2d27a08 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 15 Jan 2025 10:32:10 +0100 Subject: [PATCH 3/5] fix cargo doc ; speed up ci doc to run without deps --- .github/workflows/main.yml | 2 +- bevy_rapier_benches3d/src/lib.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 16723ac3..350d00b9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -32,7 +32,7 @@ jobs: with: prefix-key: ${{ env.RUST_CACHE_KEY }} - name: Cargo doc - run: cargo doc + run: cargo doc --no-deps test: runs-on: ubuntu-latest env: diff --git a/bevy_rapier_benches3d/src/lib.rs b/bevy_rapier_benches3d/src/lib.rs index 02709e02..ba24b105 100644 --- a/bevy_rapier_benches3d/src/lib.rs +++ b/bevy_rapier_benches3d/src/lib.rs @@ -30,13 +30,13 @@ pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) { .world_mut() .query::<&RapierContextSimulation>() .single(app.world()); - rapier_step_times.push(rc.pipeline.counters.step_time.time() as f32); + rapier_step_times.push(rc.pipeline.counters.step_time.time().as_millis() as f32); total_update_times.push(elapsed_time); } timer_total.pause(); let average_total = total_update_times .iter() - .map(|time| time.as_millis() as f32) + .map(|time| *time as f32) .sum::() / total_update_times.len() as f32; println!("average total time: {} ms", average_total); @@ -45,5 +45,5 @@ pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) { println!("average rapier step time: {} ms", average_rapier_step); let average_rapier_overhead = average_total - average_rapier_step; println!("average bevy overhead: {} ms", average_rapier_overhead); - println!("total time: {} ms", timer_total.time()); + println!("total time: {} ms", timer_total.time().as_millis()); } From acf6a504ad7986c57495873dafdd5443d405caac Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 15 Jan 2025 10:46:55 +0100 Subject: [PATCH 4/5] removed --lib tests from ci because now part of #626 --- .github/workflows/main.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 350d00b9..d9b2364f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -58,10 +58,6 @@ jobs: run: cargo test --verbose -p bevy_rapier2d - name: Test for bevy_rapier3d run: cargo test --verbose -p bevy_rapier3d - - name: Unit Tests for bevy_rapier3d without default features - run: cargo test -p bevy_rapier3d --no-default-features --features dim3 --lib - - name: Unit Tests for bevy_rapier3d without default features - run: cargo test -p bevy_rapier3d --no-default-features --features dim3 --lib test-wasm: runs-on: ubuntu-latest env: From 41083164bee4df280de68fdc5c4bd95111364210 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 15 Jan 2025 11:33:25 +0100 Subject: [PATCH 5/5] example from rapier issue 748 --- Cargo.toml | 3 + bevy_rapier3d/Cargo.toml | 2 + bevy_rapier3d/examples/issue_rapier_748.rs | 142 +++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 bevy_rapier3d/examples/issue_rapier_748.rs diff --git a/Cargo.toml b/Cargo.toml index a297a9b5..7bc0dd1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,6 @@ codegen-units = 1 #parry3d = { git = "https://github.com/dimforge/parry", branch = "master" } #rapier2d = { git = "https://github.com/dimforge/rapier", branch = "character-controller" } #rapier3d = { git = "https://github.com/dimforge/rapier", branch = "character-controller" } + +# Fix for exammple issue_rapier_748 +parry3d = { git = "https://github.com/Vrixyz/parry", branch = "incorrect-shapecast" } diff --git a/bevy_rapier3d/Cargo.toml b/bevy_rapier3d/Cargo.toml index 1f0aa2ef..874666c6 100644 --- a/bevy_rapier3d/Cargo.toml +++ b/bevy_rapier3d/Cargo.toml @@ -76,6 +76,8 @@ approx = "0.5.1" glam = { version = "0.29", features = ["approx"] } bevy-inspector-egui = "0.28" bevy_egui = "0.31" +# Used for example issue_rapier_748 +fastrand = "2.3" [package.metadata.docs.rs] # Enable all the features when building the docs on docs.rs diff --git a/bevy_rapier3d/examples/issue_rapier_748.rs b/bevy_rapier3d/examples/issue_rapier_748.rs new file mode 100644 index 00000000..bedee2b1 --- /dev/null +++ b/bevy_rapier3d/examples/issue_rapier_748.rs @@ -0,0 +1,142 @@ +use bevy::prelude::*; +use bevy_rapier3d::prelude::*; + +const START_POSITION: Vec3 = Vec3::new(0., 1.5, 0.); + +fn main() { + let mut app = App::new(); + app.add_plugins(( + DefaultPlugins, + RapierPhysicsPlugin::::default(), + RapierDebugRenderPlugin::default(), + )) + .insert_resource(ClearColor(Color::srgb(0.2, 0.2, 0.2))) + .add_systems(Startup, (spawn_basic_scene, spawn_shapes)) + .add_systems(FixedUpdate, update_cubes) + .run(); +} + +#[derive(Component)] +struct Velocity(Vec3); +impl Velocity { + pub fn random() -> Self { + Velocity( + // truly random velocity -> i found no matching vel that way + // Vec3::new( + // fastrand::f32() * 2. - 1., + // -fastrand::f32(), + // fastrand::f32() * 2. - 1., + // ) + // .normalize(), + + // this velocity always works + //Vec3::new(1.0, -5.0, -1.0).normalize(), + // integer velocities + Vec3::new( + fastrand::i32(-6..=6) as f32, + fastrand::i32(-6..0) as f32, + fastrand::i32(-6..=6) as f32, + ) + .normalize(), + // even a slight deviation from an integer velocity makes it very unlikely + // (Vec3::new( + // fastrand::i32(-6..=6) as f32, + // fastrand::i32(-6..0) as f32, + // fastrand::i32(-6..=6) as f32, + // ) + // .normalize() + // + Vec3::new( + // fastrand::f32() * 2. - 1., + // -fastrand::f32(), + // fastrand::f32() * 2. - 1., + // ) + // .normalize() + // * 0.00001) + // .normalize(), + ) + } +} + +fn spawn_shapes(mut commands: Commands) { + let group = CollisionGroups::new(Group::GROUP_1, Group::ALL ^ Group::GROUP_1); + + // every shape except cuboid works + for shape in [ + Collider::cuboid(0.5, 0.5, 0.5), + Collider::ball(0.5), + Collider::capsule_y(0.5, 0.2), + Collider::cylinder(0.5, 0.5), + ] { + // i spawned many shapes to get a better random sampling, but it also works with just one + for _ in 0..100 { + commands.spawn(( + group, + Velocity::random(), + RigidBody::KinematicPositionBased, + shape.clone(), + Transform::from_translation(START_POSITION), + KinematicCharacterController { + filter_groups: Some(group), + slide: true, // sliding seems to make no difference + ..KinematicCharacterController::default() + }, + )); + } + } +} + +fn spawn_basic_scene( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + // spawn light + commands.spawn((PointLight::default(), Transform::from_xyz(0., 5., 0.))); + + // spawn ground plane + commands.spawn(( + // the size of the cuboid seems very important + Collider::cuboid(10., 0.1, 10.), + Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(10.)).mesh())), + MeshMaterial3d(materials.add(StandardMaterial { + base_color: Color::srgb(0.2, 0.9, 0.2), + ..default() + })), + )); + + // add camera + commands.spawn(( + Camera3d::default(), + Transform::from_xyz(10., 1.8, 0.).looking_at(Vec3::ZERO, Vec3::Y), + )); +} + +fn update_cubes( + time: Res