diff --git a/bevy_rapier3d/Cargo.toml b/bevy_rapier3d/Cargo.toml index 1082f501..efcba93f 100644 --- a/bevy_rapier3d/Cargo.toml +++ b/bevy_rapier3d/Cargo.toml @@ -7,7 +7,7 @@ documentation = "http://docs.rs/bevy_rapier3d" homepage = "http://rapier.rs" repository = "https://github.com/dimforge/bevy_rapier" readme = "../README.md" -keywords = [ "physics", "dynamics", "rigid", "real-time", "joints" ] +keywords = ["physics", "dynamics", "rigid", "real-time", "joints"] license = "Apache-2.0" edition = "2021" @@ -15,38 +15,50 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] path = "../src/lib.rs" -required-features = [ "dim3" ] +required-features = ["dim3"] [features] -default = [ "dim3", "async-collider", "debug-render-3d" ] +default = ["dim3", "async-collider", "debug-render-3d"] dim3 = [] -debug-render = [ "debug-render-3d" ] -debug-render-2d = [ "bevy/bevy_core_pipeline", "bevy/bevy_sprite", "bevy/bevy_gizmos", "rapier3d/debug-render", "bevy/bevy_asset" ] -debug-render-3d = [ "bevy/bevy_core_pipeline", "bevy/bevy_pbr", "bevy/bevy_gizmos", "rapier3d/debug-render", "bevy/bevy_asset" ] -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 = [ ] -async-collider = [ "bevy/bevy_asset", "bevy/bevy_scene" ] +debug-render = ["debug-render-3d"] +debug-render-2d = [ + "bevy/bevy_core_pipeline", + "bevy/bevy_sprite", + "bevy/bevy_gizmos", + "rapier3d/debug-render", + "bevy/bevy_asset", +] +debug-render-3d = [ + "bevy/bevy_core_pipeline", + "bevy/bevy_pbr", + "bevy/bevy_gizmos", + "rapier3d/debug-render", + "bevy/bevy_asset", +] +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 = [] +async-collider = ["bevy/bevy_asset", "bevy/bevy_scene"] [dependencies] bevy = { version = "0.11", default-features = false } -nalgebra = { version = "0.32.3", features = [ "convert-glam024" ] } +nalgebra = { version = "0.32.3", features = ["convert-glam024"] } # Don't enable the default features because we don't need the ColliderSet/RigidBodySet rapier3d = "0.17.0" bitflags = "1" #bevy_prototype_debug_lines = { version = "0.6", features = ["3d"], optional = true } log = "0.4" -serde = { version = "1", features = [ "derive" ], optional = true} +serde = { version = "1", features = ["derive"], optional = true } [dev-dependencies] -bevy = { version = "0.11", default-features = false, features = ["x11"]} +bevy = { version = "0.11", default-features = false, features = ["x11"] } approx = "0.5.1" -glam = { version = "0.24", features = [ "approx" ] } +glam = { version = "0.24", features = ["approx"] } [package.metadata.docs.rs] # Enable all the features when building the docs on docs.rs -features = [ "debug-render-3d", "serde-serialize" ] +features = ["debug-render-3d", "serde-serialize"] diff --git a/bevy_rapier3d/examples/boxes3.rs b/bevy_rapier3d/examples/boxes3.rs index 3d510720..b440dc58 100644 --- a/bevy_rapier3d/examples/boxes3.rs +++ b/bevy_rapier3d/examples/boxes3.rs @@ -17,6 +17,155 @@ fn main() { .run(); } +fn setup_compound(mut commands: Commands, mut meshes: ResMut>) { + // Normal compound + // - Scaling works as expected + commands + .spawn(TransformBundle::default()) + .insert(Name::new("Compound")) + .insert(RigidBody::Dynamic) + .insert(ToggleCollider) + .insert(ToggleScale) + .insert(Collider::compound(vec![ + (Vec3::ZERO, Quat::IDENTITY, Collider::cuboid(0.5, 0.5, 0.5)), + ( + Vec3::new(0.0, 1.0, 0.0), + Quat::IDENTITY, + Collider::cuboid(0.5, 0.5, 0.5), + ), + ( + Vec3::new(-1.0, 1.0, 0.0), + Quat::IDENTITY, + Collider::cuboid(0.5, 0.5, 0.5), + ), + ])); + + let mesh = meshes.add(Mesh::from(shape::UVSphere { + radius: 0.2, + ..default() + })); + + commands + .spawn(PbrBundle { + mesh: mesh.clone(), + ..default() + }) + .insert(TransformBundle::from_transform(Transform { + translation: Vec3::new(-4.0, 0.0, 0.0), + ..default() + })) + .insert(Name::new("Standalone collider")) + .insert(ToggleScale) + .insert(ToggleCollider) + .insert(Collider::cuboid(0.5, 0.5, 0.5)); + + commands + .spawn(PbrBundle { + mesh: mesh.clone(), + ..default() + }) + .insert(TransformBundle::from_transform(Transform { + translation: Vec3::new(4.0, 0.0, 0.0), + ..default() + })) + .insert(Name::new("Compound via children")) + .insert(RigidBody::Dynamic) + .insert(ToggleScale) + //.insert(ToggleCollider) + .insert(Collider::cuboid(0.5, 0.5, 0.5)) + .with_children(|children| { + children + .spawn(PbrBundle { + mesh: mesh.clone(), + ..default() + }) + .insert(TransformBundle::from_transform(Transform { + translation: Vec3::new(0.0, 1.0, 0.0), + ..default() + })) + .insert(Name::new("Child collider")) + .insert(Collider::cuboid(0.5, 0.5, 0.5)); + + children + .spawn(PbrBundle { + mesh: mesh.clone(), + ..default() + }) + .insert(TransformBundle::from_transform(Transform { + translation: Vec3::new(-1.0, 1.0, 0.0), + ..default() + })) + .insert(Name::new("Child collider")) + .insert(Collider::cuboid(0.5, 0.5, 0.5)); + }); +} + +#[derive(Component)] +pub struct ToggleCollider; +#[derive(Component)] +pub struct ToggleScale; + +fn toggle_compound( + keycode: Res>, + mut commands: Commands, + mut collider: Query<(Entity, &mut Collider), With>, + mut scale: Query<(Entity, &mut Transform), With>, +) { + let new_collider = if keycode.just_pressed(KeyCode::Key1) { + Some(Collider::compound(vec![ + (Vec3::ZERO, Quat::IDENTITY, Collider::cuboid(0.5, 0.5, 0.5)), + ( + Vec3::new(0.0, 1.0, 0.0), + Quat::IDENTITY, + Collider::cuboid(0.5, 0.5, 0.5), + ), + ( + Vec3::new(-1.0, 1.0, 0.0), + Quat::IDENTITY, + Collider::cuboid(0.5, 0.5, 0.5), + ), + ])) + } else if keycode.just_pressed(KeyCode::Key2) { + Some(Collider::compound(vec![ + (Vec3::ZERO, Quat::IDENTITY, Collider::ball(0.5)), + ( + Vec3::new(0.0, 1.0, 0.0), + Quat::IDENTITY, + Collider::ball(0.5), + ), + ( + Vec3::new(-1.0, 1.0, 0.0), + Quat::IDENTITY, + Collider::ball(0.5), + ), + ])) + } else { + None + }; + + let new_scale_ratio = if keycode.just_pressed(KeyCode::T) { + Some(1.1) + } else if keycode.just_pressed(KeyCode::R) { + Some(0.9) + } else { + None + }; + + if let Some(new_scale_ratio) = new_scale_ratio { + for (entity, mut transform) in &mut scale { + transform.scale *= new_scale_ratio; + transform.scale = transform.scale.max(Vec3::new(0.1, 0.1, 0.1)); + transform.scale = transform.scale.min(Vec3::new(5.0, 5.0, 5.0)); + } + } + + if let Some(new_collider) = new_collider { + for (entity, mut collider) in &mut collider { + *collider = new_collider.clone(); + } + } +} + fn setup_graphics(mut commands: Commands) { commands.spawn(Camera3dBundle { transform: Transform::from_xyz(-30.0, 30.0, 100.0) @@ -40,7 +189,7 @@ pub fn setup_physics(mut commands: Commands) { /* * Create the cubes */ - let num = 8; + let num = 0; let rad = 1.0; let shift = rad * 2.0 + rad; @@ -56,7 +205,7 @@ pub fn setup_physics(mut commands: Commands) { Color::hsl(260.0, 1.0, 0.7), ]; - for j in 0usize..20 { + for j in 0usize..num { for i in 0..num { for k in 0usize..num { let x = i as f32 * shift - centerx + offset;