From 88063a3fc9156af7fe3d8e626d6a6f138501d05b Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 30 Dec 2020 19:59:07 +0100 Subject: [PATCH] very basic (and broken) physics based movement --- Cargo.lock | 293 ++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + src/main.rs | 23 ++-- src/player.rs | 26 +++-- 4 files changed, 315 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 272b7c2..e852da3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -270,7 +270,7 @@ dependencies = [ "bevy_reflect", "bevy_tasks", "bevy_utils", - "crossbeam-channel", + "crossbeam-channel 0.4.4", "downcast-rs", "js-sys", "ndk-glue", @@ -363,7 +363,7 @@ dependencies = [ "bevy_utils", "bitflags", "downcast-rs", - "fixedbitset", + "fixedbitset 0.3.1", "lazy_static", "parking_lot", "rand", @@ -507,6 +507,18 @@ dependencies = [ "bevy_window", ] +[[package]] +name = "bevy_rapier3d" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c11b4b13050eb907f824c92b598e418e75f710c50a3c35a2cd3f97f6f5afc0e1" +dependencies = [ + "bevy", + "concurrent-queue", + "nalgebra", + "rapier3d", +] + [[package]] name = "bevy_reflect" version = "0.4.0" @@ -714,8 +726,8 @@ dependencies = [ "bevy_utils", "bevy_window", "bevy_winit", - "crossbeam-channel", - "crossbeam-utils", + "crossbeam-channel 0.4.4", + "crossbeam-utils 0.7.2", "futures-lite", "parking_lot", "wgpu", @@ -1171,16 +1183,75 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "crossbeam" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd01a6eb3daaafa260f6fc94c3a6c36390abc2080e38e3e34ced87393fb77d80" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-channel 0.5.0", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils 0.8.1", +] + [[package]] name = "crossbeam-channel" version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b153fe7cbef478c567df0f972e02e6d736db11affe43dfc9c56a9374d1adfb87" dependencies = [ - "crossbeam-utils", + "crossbeam-utils 0.7.2", "maybe-uninit", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils 0.8.1", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-epoch", + "crossbeam-utils 0.8.1", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1aaa739f95311c2c7887a76863f500026092fb1dce0161dab577e559ef3569d" +dependencies = [ + "cfg-if 1.0.0", + "const_fn", + "crossbeam-utils 0.8.1", + "lazy_static", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-queue" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f6cb3c7f5b8e51bc3ebb73a2327ad4abdbd119dc13223f14f961d2f38486756" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils 0.8.1", +] + [[package]] name = "crossbeam-utils" version = "0.7.2" @@ -1192,6 +1263,17 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crossbeam-utils" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02d96d1e189ef58269ebe5b97953da3274d83a93af647c2ddd6f9dab28cedb8d" +dependencies = [ + "autocfg", + "cfg-if 1.0.0", + "lazy_static", +] + [[package]] name = "d3d12" version = "0.3.2" @@ -1361,13 +1443,19 @@ dependencies = [ [[package]] name = "find-crate" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "057a1d48e8ff33649ee2d7c510b79ecf1f8a52b684d446a72de600ad7e2823b6" +checksum = "e82d9a3770cc1839f610f61f90bb1167c37581e97ed3400cfb6cf66fbd69a639" dependencies = [ "toml", ] +[[package]] +name = "fixedbitset" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d" + [[package]] name = "fixedbitset" version = "0.3.1" @@ -1559,6 +1647,15 @@ dependencies = [ "byteorder", ] +[[package]] +name = "generational-arena" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d3b771574f62d0548cee0ad9057857e9fc25d7a3335f140c84f6acd0bf601" +dependencies = [ + "cfg-if 0.1.10", +] + [[package]] name = "generator" version = "0.6.23" @@ -1572,6 +1669,16 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.1.15" @@ -1854,6 +1961,12 @@ dependencies = [ "svg_fmt", ] +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" + [[package]] name = "hermit-abi" version = "0.1.17" @@ -1912,6 +2025,16 @@ dependencies = [ "scoped_threadpool", ] +[[package]] +name = "indexmap" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "inflections" version = "1.1.1" @@ -1952,6 +2075,7 @@ checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" dependencies = [ "cfg-if 1.0.0", "js-sys", + "time 0.2.23", "wasm-bindgen", "web-sys", ] @@ -2095,6 +2219,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a" +[[package]] +name = "libm" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" + [[package]] name = "libudev-sys" version = "0.1.4" @@ -2178,6 +2308,15 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" +[[package]] +name = "matrixmultiply" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "916806ba0031cd542105d916a97c8572e1fa6dd79c9c51e7eb43a09ec2dd84c1" +dependencies = [ + "rawpointer", +] + [[package]] name = "maybe-uninit" version = "2.0.0" @@ -2190,6 +2329,15 @@ version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +[[package]] +name = "memoffset" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" +dependencies = [ + "autocfg", +] + [[package]] name = "metal" version = "0.20.0" @@ -2299,6 +2447,43 @@ dependencies = [ "thiserror", ] +[[package]] +name = "nalgebra" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9bd52e9c9922d5702d5c2a105ceacc3c1206e781d3d130f4d2b7ad5f43c144b" +dependencies = [ + "approx", + "generic-array", + "matrixmultiply", + "num-complex", + "num-rational", + "num-traits", + "rand", + "rand_distr", + "simba", + "typenum", +] + +[[package]] +name = "ncollide3d" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fa86028ca1d3ac2ff5946c64e3f82fc6e9cdfaef2bef716f9fbf7e1559298ec" +dependencies = [ + "approx", + "bitflags", + "downcast-rs", + "either", + "nalgebra", + "num-traits", + "petgraph", + "simba", + "slab", + "slotmap", + "smallvec", +] + [[package]] name = "ndk" version = "0.2.1" @@ -2399,7 +2584,7 @@ checksum = "a8b946889dfdad884379cd56367d93b6d0ce8889cc027d26a69a3a31c0a03bb5" dependencies = [ "anymap", "bitflags", - "crossbeam-channel", + "crossbeam-channel 0.4.4", "filetime", "fsevent", "fsevent-sys", @@ -2411,6 +2596,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "num-complex" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "747d632c0c558b87dbabbe6a82f3b4ae03720d0646ac5b7b4dae89394be5f2c5" +dependencies = [ + "num-traits", +] + [[package]] name = "num-derive" version = "0.3.3" @@ -2461,6 +2655,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" dependencies = [ "autocfg", + "libm 0.2.1", ] [[package]] @@ -2589,6 +2784,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "paste" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5d65c4d95931acda4498f675e332fcbdc9a06705cd07086c510e9b6009cd1c1" + [[package]] name = "peeking_take_while" version = "0.1.2" @@ -2601,6 +2802,16 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +[[package]] +name = "petgraph" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "467d164a6de56270bd7c4d070df81d07beace25012d5103ced4e9ff08d6afdb7" +dependencies = [ + "fixedbitset 0.2.0", + "indexmap", +] + [[package]] name = "pin-project" version = "1.0.2" @@ -2723,6 +2934,7 @@ name = "rake" version = "0.1.0" dependencies = [ "bevy", + "bevy_rapier3d", ] [[package]] @@ -2757,6 +2969,16 @@ dependencies = [ "getrandom 0.1.15", ] +[[package]] +name = "rand_distr" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e9532ada3929fb8b2e9dbe28d1e06c9b2cc65813f074fcb6bd5fbefeff9d56" +dependencies = [ + "num-traits", + "rand", +] + [[package]] name = "rand_hc" version = "0.2.0" @@ -2772,6 +2994,29 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a871f1e45a3a3f0c73fb60343c811238bb5143a81642e27c2ac7aac27ff01a63" +[[package]] +name = "rapier3d" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f175c71f3c2b140f17fd4683097d1b166cd253b1d8dc4cdf3467bd6281b9ef" +dependencies = [ + "approx", + "arrayvec", + "bit-vec", + "bitflags", + "crossbeam", + "downcast-rs", + "generational-arena", + "instant", + "nalgebra", + "ncollide3d", + "num-derive", + "num-traits", + "rustc-hash", + "simba", + "vec_map", +] + [[package]] name = "raw-window-handle" version = "0.3.3" @@ -2781,6 +3026,12 @@ dependencies = [ "libc", ] +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + [[package]] name = "rectangle-pack" version = "0.2.0" @@ -3039,6 +3290,18 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" +[[package]] +name = "simba" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17bfe642b1728a6e89137ad428ef5d4738eca4efaba9590f9e110b8944028621" +dependencies = [ + "approx", + "num-complex", + "num-traits", + "paste", +] + [[package]] name = "slab" version = "0.4.2" @@ -3056,6 +3319,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "slotmap" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c46a3482db8f247956e464d783693ece164ca056e6e67563ee5505bdb86452cd" + [[package]] name = "smallvec" version = "1.5.1" @@ -3188,7 +3457,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b0dc6d20ce137f302edf90f9cd3d278866fd7fb139efca6f246161222ad6d87" dependencies = [ "lazy_static", - "libm", + "libm 0.1.4", ] [[package]] @@ -3432,6 +3701,12 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae" +[[package]] +name = "typenum" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" + [[package]] name = "unicode-bidi" version = "0.3.4" diff --git a/Cargo.toml b/Cargo.toml index e14b7e3..8457bc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2018" [dependencies] bevy = "0.4" +bevy_rapier3d = "0.7.0" diff --git a/src/main.rs b/src/main.rs index 107a603..8161a04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,9 @@ use bevy::prelude::*; use rake::player::{Player, PlayerPlugin}; +use bevy_rapier3d::rapier::dynamics::RigidBodyBuilder; +use bevy_rapier3d::rapier::geometry::ColliderBuilder; +use bevy_rapier3d::physics::RapierPhysicsPlugin; +use bevy::input::system::exit_on_esc_system; fn main() { App::build() @@ -11,11 +15,12 @@ fn main() { ..Default::default() }) .add_plugins(DefaultPlugins) + .add_plugin(RapierPhysicsPlugin) // note: debug renderer doesnt work for cylinder //.add_plugin(RapierRenderPlugin) .add_plugin(PlayerPlugin) .add_startup_system(init.system()) - .add_system(exit_game_system.system()) + .add_system(exit_on_esc_system.system()) .run(); } @@ -34,7 +39,9 @@ fn init( transform: Transform::from_translation(Vec3::new(0.0, 3.0, 3.0)), ..Default::default() }) - .with(player); + .with(player) + .with(RigidBodyBuilder::new_dynamic().translation(0.0, 3.0, 0.0)) + .with(ColliderBuilder::cylinder(1.0, 0.5).friction(0.0)); let box_mesh = meshes.add(Mesh::from(shape::Cube { size: 0.25 })); let box_material = materials.add(Color::rgb(1.0, 0.2, 0.3).into()); @@ -54,6 +61,9 @@ fn init( } // create a floor + let rigid_body1 = RigidBodyBuilder::new_static(); + let collider1 = ColliderBuilder::cuboid(10.0, 1.0, 10.0); + commands.spawn((rigid_body1, collider1)); commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::Plane { size: 10. })), material: materials.add(Color::rgb(0.5, 0.9, 0.9).into()), @@ -61,12 +71,3 @@ fn init( ..Default::default() }); } - -fn exit_game_system( - keyboard_input: Res>, - mut app_exit_events: ResMut>, -) { - if keyboard_input.pressed(KeyCode::Escape) { - app_exit_events.send(bevy::app::AppExit); - } -} diff --git a/src/player.rs b/src/player.rs index 3c42699..03d6d67 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,7 @@ use bevy::{input::mouse::MouseMotion, math::clamp, prelude::*}; +use bevy_rapier3d::rapier::dynamics::{RigidBody, RigidBodySet}; +use bevy_rapier3d::physics::RigidBodyHandleComponent; +use bevy_rapier3d::na::{Matrix3x1}; pub struct Player { /// The speed the FlyCamera moves at. Defaults to `1.0` @@ -18,7 +21,7 @@ pub struct Player { impl Player { pub fn create() -> Self { Self { - speed: 1.5, + speed: 15., sensitivity: 6.0, friction: 1.0, pitch: 0.0, @@ -54,10 +57,12 @@ fn movement_axis(input: &Res>, plus: KeyCode, minus: KeyCode) -> fn camera_movement_system( time: Res