Merge branch 'rapier'

This commit is contained in:
Joe Ardent 2022-02-08 22:01:30 -08:00
commit 8dc2f91fcf
6 changed files with 224 additions and 295 deletions

255
Cargo.lock generated
View file

@ -134,9 +134,9 @@ checksum = "677d306121baf53310a3fd342d88dc0824f6bbeace68347593658525565abee8"
[[package]]
name = "autocfg"
version = "1.0.1"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "base-x"
@ -449,15 +449,26 @@ dependencies = [
[[package]]
name = "bevy_polyline"
version = "0.1.3"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c5c82e6ea20642e4d8aa120bcf0ae09d7cae067825d7f954228c246bb8c948b"
checksum = "0973d599ae862049333b74cb5216de1be07cf7db675b6dcc0e055006ab41be26"
dependencies = [
"bevy",
"bitflags",
"naga",
]
[[package]]
name = "bevy_rapier3d"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5186c735d7aa202e982f93c71b444d34bf15fed44aca942f9b5f2940e3d06764"
dependencies = [
"bevy",
"nalgebra",
"rapier3d",
]
[[package]]
name = "bevy_reflect"
version = "0.6.0"
@ -804,7 +815,7 @@ dependencies = [
"bitflags",
"block",
"cocoa-foundation",
"core-foundation 0.9.2",
"core-foundation 0.9.3",
"core-graphics 0.22.3",
"foreign-types",
"libc",
@ -819,7 +830,7 @@ checksum = "7ade49b65d560ca58c403a479bb396592b155c0185eada742ee323d1d68d6318"
dependencies = [
"bitflags",
"block",
"core-foundation 0.9.2",
"core-foundation 0.9.3",
"core-graphics-types",
"foreign-types",
"libc",
@ -889,9 +900,9 @@ dependencies = [
[[package]]
name = "core-foundation"
version = "0.9.2"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6888e10551bb93e424d8df1d07f1a8b4fceb0001a3a4b048bfc47554946f47b3"
checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146"
dependencies = [
"core-foundation-sys 0.8.3",
"libc",
@ -934,7 +945,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2581bbab3b8ffc6fcbd550bf46c355135d16e9ff2a6ea032ad6b9bf1d7efe4fb"
dependencies = [
"bitflags",
"core-foundation 0.9.2",
"core-foundation 0.9.3",
"core-graphics-types",
"foreign-types",
"libc",
@ -947,7 +958,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a68b68b3446082644c91ac778bf50cd4104bfb002b5a6a7c44cca5a2c70788b"
dependencies = [
"bitflags",
"core-foundation 0.9.2",
"core-foundation 0.9.3",
"foreign-types",
"libc",
]
@ -967,9 +978,9 @@ dependencies = [
[[package]]
name = "crc32fast"
version = "1.3.1"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2209c310e29876f7f0b2721e7e26b84aff178aa3da5d091f9bfbf47669e60e3"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if 1.0.0",
]
@ -1011,9 +1022,9 @@ dependencies = [
[[package]]
name = "crossbeam-epoch"
version = "0.9.6"
version = "0.9.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97242a70df9b89a65d0b6df3c4bf5b9ce03c5b7309019777fbde37e7537f8762"
checksum = "c00d6d2ea26e8b151d99093005cb442fb9a37aeaca582a03ec70946f49ab5ed9"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils",
@ -1024,9 +1035,9 @@ dependencies = [
[[package]]
name = "crossbeam-queue"
version = "0.3.3"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b979d76c9fcb84dffc80a73f7290da0f83e4c95773494674cb44b76d13a7a110"
checksum = "4dd435b205a4842da59efd07628f921c096bc1cc0a156835b4fa0bcb9a19bcce"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils",
@ -1034,9 +1045,9 @@ dependencies = [
[[package]]
name = "crossbeam-utils"
version = "0.8.6"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfcae03edb34f947e64acdb1c33ec169824e20657e9ecb61cef6c8c74dcb8120"
checksum = "b5e5bed1f1c269533fa816a0a5492b3545209a205ca1a54842be180eb63a16a6"
dependencies = [
"cfg-if 1.0.0",
"lazy_static",
@ -1054,7 +1065,7 @@ version = "0.1.0"
dependencies = [
"bevy",
"bevy_polyline",
"heron",
"bevy_rapier3d",
"rand",
]
@ -1132,16 +1143,6 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650"
[[package]]
name = "duplicate"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cdaf23abc9bcc47fda1ae3af68425a22b4dfbd745f9077be0eaad5320f75f52"
dependencies = [
"heck",
"proc-macro-error",
]
[[package]]
name = "either"
version = "1.6.1"
@ -1220,15 +1221,15 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "futures-core"
version = "0.3.19"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0c8ff0461b82559810cdccfde3215c3f373807f5e5232b71479bff7bb2583d7"
checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3"
[[package]]
name = "futures-io"
version = "0.3.19"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f9d34af5a1aac6fb380f735fe510746c38067c5bf16c7fd250280503c971b2"
checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b"
[[package]]
name = "futures-lite"
@ -1428,15 +1429,6 @@ dependencies = [
"ahash",
]
[[package]]
name = "heck"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -1446,54 +1438,6 @@ dependencies = [
"libc",
]
[[package]]
name = "heron"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54579e36436e33827e21f046ea6752b3a93dbd0b05884bffe982568af7b3bebd"
dependencies = [
"bevy",
"cfg_aliases",
"heron_core",
"heron_macros",
"heron_rapier",
]
[[package]]
name = "heron_core"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7099a07736abf70026b1e474fff8d74a8fa88a834cfd33e974541e6b056abb41"
dependencies = [
"bevy",
"cfg_aliases",
"duplicate",
]
[[package]]
name = "heron_macros"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a09c1e02655f04faf8d6b485f6c27422f3769343c30c39e51a2cd25f3f2869c3"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "heron_rapier"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bf94fface67dce71dfd0ab2c0a8b211cacf7b1d02532669671892350765b340"
dependencies = [
"bevy",
"cfg_aliases",
"crossbeam",
"fnv",
"heron_core",
"rapier3d",
]
[[package]]
name = "hex"
version = "0.4.3"
@ -1621,9 +1565,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.114"
version = "0.2.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0005d08a8f7b65fb8073cb697aa0b12b631ed251ce73d862ce50eeb52ce3b50"
checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c"
[[package]]
name = "libloading"
@ -1641,6 +1585,12 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a"
[[package]]
name = "libm"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33a33a362ce288760ec6a508b94caaec573ae7d3bbbd91b87aa0bad4456839db"
[[package]]
name = "libudev-sys"
version = "0.1.4"
@ -1653,9 +1603,9 @@ dependencies = [
[[package]]
name = "lock_api"
version = "0.4.5"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109"
checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b"
dependencies = [
"scopeguard",
]
@ -1745,9 +1695,9 @@ dependencies = [
[[package]]
name = "mint"
version = "0.5.8"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "162e591484b4b8fe9e1ca16ebf07ab584fdc3334508d76a788cd54d89cfc20dc"
checksum = "e53debba6bda7a793e5f99b8dacf19e626084f525f7829104ba9898f367d85ff"
[[package]]
name = "mio"
@ -1793,11 +1743,12 @@ dependencies = [
[[package]]
name = "nalgebra"
version = "0.29.0"
version = "0.30.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d506eb7e08d6329505faa8a3a00a5dcc6de9f76e0c77e4b75763ae3c770831ff"
checksum = "4fb2d0de08694bed883320212c18ee3008576bfe8c306f4c3c4a58b4876998be"
dependencies = [
"approx",
"glam",
"matrixmultiply",
"nalgebra-macros",
"num-complex",
@ -1957,6 +1908,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg",
"libm 0.2.2",
]
[[package]]
@ -2024,6 +1976,16 @@ dependencies = [
"ttf-parser",
]
[[package]]
name = "packed_simd_2"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "defdcfef86dcc44ad208f71d9ff4ce28df6537a4e0d6b0e8e845cb8ca10059a6"
dependencies = [
"cfg-if 1.0.0",
"libm 0.1.4",
]
[[package]]
name = "parking"
version = "2.0.0"
@ -2057,9 +2019,9 @@ dependencies = [
[[package]]
name = "parry3d"
version = "0.7.1"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "067fa44bf7f7cc4022756ef9796c5f8d67c7a63041df71e6eeb09ad08ab1ece8"
checksum = "c418a03e2ba297c931c530a690b305ce3b3dcc675caf16777f4f8565c8e84732"
dependencies = [
"approx",
"bitflags",
@ -2145,30 +2107,6 @@ dependencies = [
"toml",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.36"
@ -2241,9 +2179,9 @@ checksum = "63e935c45e09cc6dcf00d2f0b2d630a58f4095320223d47fc68918722f0538b6"
[[package]]
name = "rapier3d"
version = "0.11.1"
version = "0.12.0-alpha.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfc8dde2352efdbc0813938fcc41d12651b4bb05211e81d2b44ff6c09d39b2ac"
checksum = "3f0590e74514004c7313934d092ed071302146323124efde9d30d6aa517186bf"
dependencies = [
"approx",
"arrayvec",
@ -2256,8 +2194,10 @@ dependencies = [
"num-derive",
"num-traits",
"parry3d",
"rayon",
"rustc-hash",
"simba",
"vec_map",
]
[[package]]
@ -2275,6 +2215,31 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]]
name = "rayon"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90"
dependencies = [
"autocfg",
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"lazy_static",
"num_cpus",
]
[[package]]
name = "rectangle-pack"
version = "0.4.2"
@ -2458,13 +2423,14 @@ dependencies = [
[[package]]
name = "simba"
version = "0.6.0"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0b7840f121a46d63066ee7a99fc81dcabbc6105e437cae43528cea199b5a05f"
checksum = "13a2609e876d4f77f6ab7ff5254fc39b4f1927ba8e6db3d18be7c32534d3725e"
dependencies = [
"approx",
"num-complex",
"num-traits",
"packed_simd_2",
"paste",
"wide",
]
@ -2561,7 +2527,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b0dc6d20ce137f302edf90f9cd3d278866fd7fb139efca6f246161222ad6d87"
dependencies = [
"lazy_static",
"libm",
"libm 0.1.4",
]
[[package]]
@ -2637,9 +2603,9 @@ dependencies = [
[[package]]
name = "tracing"
version = "0.1.29"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105"
checksum = "2d8d93354fe2a8e50d5953f5ae2e47a3fc2ef03292e7ea46e3cc38f549525fb9"
dependencies = [
"cfg-if 1.0.0",
"pin-project-lite",
@ -2649,9 +2615,9 @@ dependencies = [
[[package]]
name = "tracing-attributes"
version = "0.1.18"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f480b8f81512e825f337ad51e94c1eb5d3bbdf2b363dcd01e2b19a9ffe3f8e"
checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716"
dependencies = [
"proc-macro2",
"quote",
@ -2660,11 +2626,12 @@ dependencies = [
[[package]]
name = "tracing-core"
version = "0.1.21"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4"
checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23"
dependencies = [
"lazy_static",
"valuable",
]
[[package]]
@ -2680,9 +2647,9 @@ dependencies = [
[[package]]
name = "tracing-subscriber"
version = "0.3.7"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5312f325fe3588e277415f5a6cca1f4ccad0f248c4cd5a4bd33032d7286abc22"
checksum = "74786ce43333fcf51efe947aed9718fbe46d5c7328ec3f1029e818083966d9aa"
dependencies = [
"ansi_term",
"lazy_static",
@ -2719,12 +2686,6 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "unicode-segmentation"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
[[package]]
name = "unicode-width"
version = "0.1.9"
@ -2747,6 +2708,12 @@ dependencies = [
"serde",
]
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "vec_map"
version = "0.8.2"
@ -2987,7 +2954,7 @@ checksum = "9b43cc931d58b99461188607efd7acb2a093e65fc621f54cad78517a6063e73a"
dependencies = [
"bitflags",
"cocoa",
"core-foundation 0.9.2",
"core-foundation 0.9.3",
"core-graphics 0.22.3",
"core-video-sys",
"dispatch",

View file

@ -19,9 +19,9 @@ features = [
"x11",
]
[dependencies.heron]
version = "1.1"
features = ["3d"]
[dependencies.bevy_rapier3d]
version = "0.12"
features = ["parallel", "simd-nightly"]
# Enable optimizations for dependencies (incl. Bevy), but not for our code:
[profile.dev.package."*"]

View file

@ -1,14 +1,15 @@
use bevy::prelude::*;
use heron::prelude::*;
use bevy_rapier3d::{na::Vector3, prelude::*};
use crate::{geometry::CyberBike, input::InputState};
use crate::{
geometry::{CyberBike, CyberSphere, PLANET_RADIUS},
input::InputState,
};
/// Mouse sensitivity and movement speed
pub struct MovementSettings {
pub sensitivity: f32,
pub accel: f32,
pub drag: f32,
pub gravity: f32,
}
impl Default for MovementSettings {
@ -16,151 +17,119 @@ impl Default for MovementSettings {
Self {
sensitivity: 1.0,
accel: 40.,
drag: 0.0005,
gravity: 10.0,
}
}
}
#[derive(Component, Default)]
pub(crate) struct CyberBikeState {
pub velocity: Vec3,
pub colliding: bool,
fn setup_colliders(
mut commands: Commands,
planet_query: Query<Entity, With<CyberSphere>>,
bike_query: Query<(Entity, &Transform), With<CyberBike>>,
) {
let planet = planet_query.single();
let pbody = RigidBodyBundle {
body_type: RigidBodyType::Static.into(),
..Default::default()
};
let pcollide = ColliderBundle {
shape: ColliderShape::ball(PLANET_RADIUS).into(),
material: ColliderMaterial {
friction: 0.0,
restitution: 0.3,
..Default::default()
}
.into(),
..Default::default()
};
commands
.entity(planet)
.insert_bundle(pbody)
.insert_bundle(pcollide);
let (bike, xform) = bike_query.single();
let mut bbody = RigidBodyBundle::default();
bbody.damping.angular_damping = 0.8;
bbody.damping.linear_damping = 0.5;
let isometry = Isometry::from_parts(xform.translation.into(), xform.rotation.into());
bbody.position = isometry.into();
// collider
let shape = ColliderShape::capsule(
Vec3::new(0.0, 0.0, -1.25).into(),
Vec3::new(0.0, 0.0, 1.2).into(),
0.4,
);
let bcollide = ColliderBundle {
shape: shape.into(),
mass_properties: ColliderMassProps::Density(0.3).into(),
material: ColliderMaterial {
friction: 0.0,
restitution: 0.3,
..Default::default()
}
.into(),
..Default::default()
};
commands
.entity(bike)
.insert_bundle(bbody)
.insert_bundle(bcollide)
.insert(ColliderPositionSync::Discrete);
}
fn falling_cat(time: Res<Time>, mut bike_query: Query<&mut Transform, With<CyberBike>>) {
let dt = time.delta_seconds();
fn gravity(xform: Query<&Transform, With<CyberBike>>, mut config: ResMut<RapierConfiguration>) {
let gravity = xform.single().translation.normalize() * -8.0;
config.gravity = gravity.into();
}
let mut bike_xform = bike_query.single_mut();
fn falling_cat(
mut bike_query: Query<(&Transform, &mut RigidBodyForcesComponent), With<CyberBike>>,
) {
let (bike_xform, mut forces) = bike_query.single_mut();
let up = bike_xform.translation.normalize();
let cam_up = bike_xform.up();
let cos = up.dot(cam_up);
let theta = cos.acos();
let rate = if !theta.is_normal() {
0.0
} else if theta.is_sign_negative() {
-0.4
} else {
0.4
} * dt;
let angle = if rate.is_sign_negative() {
rate.max(theta)
} else {
rate.min(theta)
};
let force_mag = if !theta.is_normal() { 0.0 } else { theta * 5.0 };
let torque = cam_up.cross(up).normalize() * force_mag;
let rot = Quat::from_axis_angle(cam_up.cross(up).normalize(), angle);
if rot.is_finite() && theta.abs() > 1.0f32.to_radians() {
bike_xform.rotate(rot);
}
forces.torque = torque.into();
}
fn update_velocity(
time: Res<Time>,
fn update_forces(
settings: Res<MovementSettings>,
input: Res<InputState>,
mut query: Query<(&Transform, &mut CyberBikeState)>,
mut query: Query<
(
&Transform,
&mut RigidBodyForcesComponent,
&mut ColliderMaterialComponent,
),
With<CyberBike>,
>,
) {
let dt = time.delta_seconds();
let (xform, mut state) = query.single_mut();
let (xform, mut forces, mut cmat) = query.single_mut();
// first gravity
let down = -xform.translation.normalize();
let dvel = down * settings.gravity * dt;
let mut vel = if state.velocity.is_finite() {
state.velocity + dvel
} else {
dvel
};
// thrust or brake
vel += xform.forward() * input.throttle * dt * settings.accel;
// thrust
let thrust: Vector3<f32> = (xform.forward() * input.throttle * settings.accel).into();
forces.force += thrust;
// brake
if input.brake {
let s = vel.length_squared();
if s < 0.05 {
vel = Vec3::ZERO;
} else {
vel -= vel.normalize() * settings.accel * dt;
}
}
cmat.friction = if input.brake { 2.0 } else { 0.0 };
// drag
let v2 = vel.length_squared().min(100_000.0);
let drag = vel * settings.drag * v2 * dt;
vel -= drag;
if vel.length_squared() < 0.0001 {
vel = Vec3::ZERO;
}
state.velocity = vel;
// steering
let torque: Vector3<f32> = (xform.up() * input.yaw * settings.sensitivity).into();
forces.torque += torque;
}
fn apply_velocity(time: Res<Time>, mut bike_query: Query<(&mut Transform, &CyberBikeState)>) {
let dt = time.delta_seconds();
let (mut bike_xform, state) = bike_query.single_mut();
if state.velocity.is_finite() {
bike_xform.translation += state.velocity * dt;
}
}
fn steer_cyberbike(
settings: Res<MovementSettings>,
windows: Res<Windows>,
time: Res<Time>,
istate: Res<InputState>,
mut query: Query<&mut Transform, With<CyberBike>>,
) {
let window = windows.get_primary().unwrap();
let window_scale = window.height().min(window.width());
let dt = time.delta_seconds();
let mut transform = query.single_mut();
let d_yaw = (settings.sensitivity * dt * window_scale * istate.yaw).to_radians();
let rotation = Quat::from_axis_angle(transform.local_y(), d_yaw);
transform.rotate(rotation);
}
fn collisions(
mut events: EventReader<CollisionEvent>,
mut query: Query<(&Transform, &mut CyberBikeState)>,
) {
let (xform, mut state) = query.single_mut();
for event in events.iter() {
if let CollisionEvent::Started(_, _) = event {
state.colliding = true;
} else {
state.colliding = false;
}
}
// now see if we're currently colliding
if state.colliding {
let down = -xform.translation.normalize();
let vel = state.velocity;
let dvel = down * vel.dot(down);
state.velocity -= dvel;
}
}
pub struct CyberPhysicsPlugin;
impl Plugin for CyberPhysicsPlugin {
pub struct CyberActionPlugin;
impl Plugin for CyberActionPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<CyberBikeState>()
.init_resource::<MovementSettings>()
.add_plugin(PhysicsPlugin::default())
.add_system(collisions)
app.init_resource::<MovementSettings>()
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.add_startup_system_to_stage(StartupStage::PostStartup, setup_colliders)
.add_system(gravity)
.add_system(falling_cat)
.add_system(update_velocity)
.add_system(steer_cyberbike)
.add_system(apply_velocity);
.add_system(update_forces);
}
}

View file

@ -1,5 +1,4 @@
use bevy::prelude::*;
use heron::prelude::{CollisionShape, RigidBody};
use crate::Label;
@ -17,6 +16,8 @@ fn spawn_giant_sphere(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let mut color = Color::DARK_GRAY;
color.set_a(0.0);
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
@ -24,23 +25,19 @@ fn spawn_giant_sphere(
subdivisions: 24,
})),
material: materials.add(StandardMaterial {
base_color: Color::DARK_GRAY,
base_color: color,
metallic: 0.6,
perceptual_roughness: 0.4,
alpha_mode: AlphaMode::Mask(0.5),
..Default::default()
}),
..Default::default()
})
.insert(CyberSphere)
.insert(RigidBody::Static)
.insert(CollisionShape::Sphere {
radius: PLANET_RADIUS,
});
.insert(CyberSphere);
}
fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
use crate::action::CyberBikeState;
commands
.spawn_bundle((
Transform {
@ -53,13 +50,7 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
.with_children(|rider| {
rider.spawn_scene(asset_server.load("cyber-bike_no_y_up.glb#Scene0"));
})
.insert(CyberBike)
.insert(RigidBody::Dynamic)
.insert(CollisionShape::Capsule {
half_segment: 2.0,
radius: 0.8,
})
.insert(CyberBikeState::default());
.insert(CyberBike);
}
pub struct CyberGeomPlugin;

View file

@ -1,6 +1,6 @@
use bevy::prelude::*;
use cyber_rider::{
action::{CyberPhysicsPlugin, MovementSettings},
action::{CyberActionPlugin, MovementSettings},
camera::CyberCamPlugin,
disable_mouse_trap,
geometry::CyberGeomPlugin,
@ -11,10 +11,8 @@ use cyber_rider::{
};
const MOVEMENT_SETTINGS: MovementSettings = MovementSettings {
sensitivity: 0.3, // default: 1.0
accel: 20.0, // default: 40.0
drag: 0.0001, // default: 0.0005
gravity: 10.0, // default: 10.0
sensitivity: 4.0, // default: 1.0
accel: 15.0, // default: 40.0
};
fn main() {
@ -24,7 +22,7 @@ fn main() {
.add_plugin(CyberGeomPlugin)
.add_plugin(CyberGlamorPlugin)
.add_plugin(CyberInputPlugin)
.add_plugin(CyberPhysicsPlugin)
.add_plugin(CyberActionPlugin)
.insert_resource(MOVEMENT_SETTINGS)
.add_plugin(CyberCamPlugin)
.add_plugin(CyberSpaceLightsPlugin)

View file

@ -1,6 +1,7 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use crate::action::CyberBikeState;
use crate::geometry::CyberBike;
#[derive(Component)]
struct UpText;
@ -32,10 +33,13 @@ fn setup_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert(UpText);
}
fn update_ui(state_query: Query<&CyberBikeState>, mut text_query: Query<&mut Text, With<UpText>>) {
fn update_ui(
state_query: Query<&RigidBodyVelocityComponent, With<CyberBike>>,
mut text_query: Query<&mut Text, With<UpText>>,
) {
let mut text = text_query.single_mut();
let state = state_query.single();
text.sections[0].value = format!("spd: {:.2}", state.velocity.length(),);
text.sections[0].value = format!("spd: {:.2}", state.linvel.magnitude());
}
pub struct CyberUIPlugin;