From 5b4b526aafd80a9975ca9230dbf41f9ecceeb7ed Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 26 Feb 2022 23:31:01 -0800 Subject: [PATCH] Make bike model, body, and collider explicit, add debug collider viz. --- Cargo.lock | 14 ++++++-------- Cargo.toml | 3 ++- src/action.rs | 27 ++++++++++++++------------- src/camera.rs | 6 +++--- src/colliders.rs | 38 ++++++++++++++++++++++++++------------ src/geometry.rs | 4 ++-- src/ui.rs | 4 ++-- 7 files changed, 55 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 576bd16..58b3243 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "ab_glyph" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61caed9aec6daeee1ea38ccf5fb225e4f96c1eeead1b4a5c267324a63cf02326" +checksum = "d54a65e0d4f66f8536c98cb3ca81ca33b7e2ca43442465507a3a62291ec0d9e4" dependencies = [ "ab_glyph_rasterizer", "owned_ttf_parser", @@ -461,8 +461,6 @@ dependencies = [ [[package]] name = "bevy_rapier3d" version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5186c735d7aa202e982f93c71b444d34bf15fed44aca942f9b5f2940e3d06764" dependencies = [ "bevy", "nalgebra", @@ -2010,9 +2008,9 @@ checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" [[package]] name = "owned_ttf_parser" -version = "0.14.0" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ef05f2882a8b3e7acc10c153ade2631f7bfc8ce00d2bf3fb8f4e9d2ae6ea5c3" +checksum = "4fb1e509cfe7a12db2a90bfa057dfcdbc55a347f5da677c506b53dd099cfec9d" dependencies = [ "ttf-parser", ] @@ -2757,9 +2755,9 @@ dependencies = [ [[package]] name = "ttf-parser" -version = "0.14.0" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ccbe8381883510b6a2d8f1e32905bddd178c11caef8083086d0c0c9ab0ac281" +checksum = "c74c96594835e10fa545e2a51e8709f30b173a092bfd6036ef2cec53376244f3" [[package]] name = "typenum" diff --git a/Cargo.toml b/Cargo.toml index 57a5063..ba220cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,8 +24,9 @@ features = [ ] [dependencies.bevy_rapier3d] -version = "0.12" +path = "../bevy_rapier/bevy_rapier3d" features = ["parallel", "simd-nightly"] +# version = "0.12" # Maybe also enable only a small amount of optimization for our code: [profile.dev] diff --git a/src/action.rs b/src/action.rs index b0acb3a..23fd85f 100644 --- a/src/action.rs +++ b/src/action.rs @@ -1,7 +1,10 @@ use bevy::prelude::*; use bevy_rapier3d::{na::Vector3, prelude::*}; -use crate::{geometry::CyberBike, input::InputState}; +use crate::{ + colliders::{CyberBikeBody, CyberBikeCollider}, + input::InputState, +}; pub struct MovementSettings { pub sensitivity: f32, @@ -20,7 +23,7 @@ impl Default for MovementSettings { } fn gravity( - xform: Query<&Transform, With>, + xform: Query<&Transform, With>, settings: Res, mut config: ResMut, ) { @@ -29,7 +32,7 @@ fn gravity( } fn falling_cat( - mut bike_query: Query<(&Transform, &mut RigidBodyForcesComponent), With>, + mut bike_query: Query<(&Transform, &mut RigidBodyForcesComponent), With>, ) { let (bike_xform, mut forces) = bike_query.single_mut(); let up = bike_xform.translation.normalize(); @@ -44,7 +47,10 @@ fn falling_cat( } fn drag( - mut query: Query<(&RigidBodyVelocityComponent, &mut RigidBodyForcesComponent), With>, + mut query: Query< + (&RigidBodyVelocityComponent, &mut RigidBodyForcesComponent), + With, + >, ) { let (vels, mut forces) = query.single_mut(); @@ -57,16 +63,11 @@ fn drag( fn input_forces( settings: Res, input: Res, - mut query: Query< - ( - &Transform, - &mut RigidBodyForcesComponent, - &mut ColliderMaterialComponent, - ), - With, - >, + mut cquery: Query<&mut ColliderMaterialComponent, With>, + mut bquery: Query<(&Transform, &mut RigidBodyForcesComponent), With>, ) { - let (xform, mut forces, mut cmat) = query.single_mut(); + let (xform, mut forces) = bquery.single_mut(); + let mut cmat = cquery.single_mut(); // thrust let thrust: Vector3 = (xform.forward() * input.throttle * settings.accel).into(); diff --git a/src/camera.rs b/src/camera.rs index a9f5a66..9305bfa 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use crate::{ - geometry::{CyberBike, SPAWN_ALTITUDE}, + geometry::{CyberBikeModel, SPAWN_ALTITUDE}, input::InputState, }; @@ -29,8 +29,8 @@ fn setup_cybercam(mut commands: Commands) { } fn follow_cyberbike( - bike_query: Query<&Transform, (Without, With)>, - mut cam_query: Query<&mut Transform, (With, Without)>, + bike_query: Query<&Transform, (Without, With)>, + mut cam_query: Query<&mut Transform, (With, Without)>, input: Res, ) { let bike_xform = bike_query.single(); diff --git a/src/colliders.rs b/src/colliders.rs index 086c1db..c5abe93 100644 --- a/src/colliders.rs +++ b/src/colliders.rs @@ -4,13 +4,19 @@ use bevy::{ }; use bevy_rapier3d::prelude::*; -use crate::geometry::{CyberBike, CyberSphere, PLANET_RADIUS, SPAWN_ALTITUDE}; +use crate::geometry::{CyberBikeModel, CyberSphere, PLANET_RADIUS, SPAWN_ALTITUDE}; + +#[derive(Component)] +pub struct CyberBikeBody; + +#[derive(Component)] +pub struct CyberBikeCollider; fn setup_colliders( mut commands: Commands, meshes: Res>, planet_query: Query<(Entity, &Handle), With>, - bike_query: Query<(Entity, &Transform), With>, + bike_query: Query<(Entity, &Transform), With>, ) { let (planet, mesh_handle) = planet_query.single(); @@ -85,9 +91,9 @@ fn setup_bike_collider(bike: Entity, xform: &Transform, commands: &mut Commands) 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.25).into(), - 0.4, + Vec3::new(0.0, 0.0, -5.25).into(), + Vec3::new(0.0, 0.0, 5.25).into(), + 5.2, ); let bcollide = ColliderBundle { shape: shape.into(), @@ -103,17 +109,25 @@ fn setup_bike_collider(bike: Entity, xform: &Transform, commands: &mut Commands) commands .entity(bike) .insert_bundle(bbody) - .insert_bundle(bcollide) - .insert(ColliderPositionSync::Discrete); + .insert(CyberBikeBody) + .insert(RigidBodyPositionSync::Discrete) + .with_children(|e| { + e.spawn_bundle(bcollide) + .insert(ColliderDebugRender { + color: Color::GREEN, + }) + .insert(CyberBikeCollider) + .insert(ColliderPositionSync::Discrete); + }); } pub struct CyberCollidersPlugin; impl Plugin for CyberCollidersPlugin { fn build(&self, app: &mut App) { - // - app.add_startup_system_to_stage( - StartupStage::PostStartup, - setup_colliders.label("colliders"), - ); + app.add_plugin(RapierRenderPlugin) + .add_startup_system_to_stage( + StartupStage::PostStartup, + setup_colliders.label("colliders"), + ); } } diff --git a/src/geometry.rs b/src/geometry.rs index 62a2142..4389d7b 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -12,7 +12,7 @@ pub const PLANET_RADIUS: f32 = 5000.0; pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.015; #[derive(Component, Debug)] -pub struct CyberBike; +pub struct CyberBikeModel; #[derive(Component)] pub struct CyberSphere; @@ -59,7 +59,7 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { .with_children(|rider| { rider.spawn_scene(asset_server.load("cyber-bike_no_y_up.glb#Scene0")); }) - .insert(CyberBike); + .insert(CyberBikeModel); } pub struct CyberGeomPlugin; diff --git a/src/ui.rs b/src/ui.rs index 565b7ce..f2f3de1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use bevy_rapier3d::prelude::*; -use crate::geometry::CyberBike; +use crate::colliders::CyberBikeBody; #[derive(Component)] struct UpText; @@ -34,7 +34,7 @@ fn setup_ui(mut commands: Commands, asset_server: Res) { } fn update_ui( - state_query: Query<&RigidBodyVelocityComponent, With>, + state_query: Query<&RigidBodyVelocityComponent, With>, mut text_query: Query<&mut Text, With>, ) { let mut text = text_query.single_mut();