From 37e8e40c3f938088400e9486e5ec849ff91b240c Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Fri, 25 Feb 2022 14:53:00 -0800 Subject: [PATCH 1/9] Move collider setup to separate file and plugin. Just make more room in the actions file for fancier dynamic stuff. --- src/action.rs | 158 ++++++++--------------------------------------- src/colliders.rs | 119 +++++++++++++++++++++++++++++++++++ src/geometry.rs | 2 +- src/lib.rs | 1 + src/main.rs | 7 ++- 5 files changed, 152 insertions(+), 135 deletions(-) create mode 100644 src/colliders.rs diff --git a/src/action.rs b/src/action.rs index 41b936c..b0acb3a 100644 --- a/src/action.rs +++ b/src/action.rs @@ -1,132 +1,30 @@ -use bevy::{ - prelude::*, - render::mesh::{Indices, VertexAttributeValues}, -}; -#[allow(unused_imports)] -use bevy_rapier3d::{na::Vector3, physics::PhysicsSystems::StepWorld, prelude::*}; +use bevy::prelude::*; +use bevy_rapier3d::{na::Vector3, prelude::*}; -use crate::{ - geometry::{CyberBike, CyberSphere, PLANET_RADIUS, SPAWN_ALTITUDE}, - input::InputState, -}; +use crate::{geometry::CyberBike, input::InputState}; -/// Mouse sensitivity and movement speed pub struct MovementSettings { pub sensitivity: f32, pub accel: f32, + pub gravity: f32, } impl Default for MovementSettings { fn default() -> Self { Self { - sensitivity: 1.0, - accel: 40., + sensitivity: 10.0, + accel: 20.0, + gravity: 9.8, } } } -fn setup_colliders( - mut commands: Commands, - meshes: Res>, - planet_query: Query<(Entity, &Handle), With>, - bike_query: Query<(Entity, &Transform), With>, +fn gravity( + xform: Query<&Transform, With>, + settings: Res, + mut config: ResMut, ) { - let (planet, mesh_handle) = planet_query.single(); - - let mesh = meshes.get(mesh_handle).unwrap(); - - let mut shape = ColliderShape::ball(PLANET_RADIUS); - - let vertices = mesh.attribute(Mesh::ATTRIBUTE_POSITION).unwrap(); - if let VertexAttributeValues::Float32x3(verts) = vertices { - if let Some(Indices::U32(indices)) = mesh.indices() { - let mut idxs = Vec::new(); - for idx in indices.chunks_exact(3) { - idxs.push([idx[0], idx[1], idx[2]]); - } - let vertices = verts - .iter() - .map(|p| Point::from_slice(p)) - .collect::>>(); - - shape = ColliderShape::trimesh(vertices, idxs); - } - } - - let pbody = RigidBodyBundle { - body_type: RigidBodyType::Static.into(), - ccd: RigidBodyCcd { - ccd_enabled: true, - ccd_thickness: 2.0, - ccd_max_dist: SPAWN_ALTITUDE, - ..Default::default() - } - .into(), - ..Default::default() - }; - let pcollide = ColliderBundle { - shape: shape.into(), - material: ColliderMaterial { - friction: 0.05, - restitution: 0.00, - ..Default::default() - } - .into(), - ..Default::default() - }; - - commands - .entity(planet) - .insert_bundle(pbody) - .insert_bundle(pcollide); - - // bike is the easy part - let (bike, xform) = bike_query.single(); - setup_bike_collider(bike, xform, &mut commands); -} - -fn setup_bike_collider(bike: Entity, xform: &Transform, commands: &mut Commands) { - let mut bbody = RigidBodyBundle::default(); - - bbody.damping.angular_damping = 0.8; - bbody.damping.linear_damping = 0.1; - - bbody.ccd = RigidBodyCcd { - ccd_enabled: true, - ccd_thickness: 0.7, - ccd_max_dist: 1.5, - ..Default::default() - } - .into(); - - 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.25).into(), - 0.4, - ); - let bcollide = ColliderBundle { - shape: shape.into(), - mass_properties: ColliderMassProps::Density(0.6).into(), - material: ColliderMaterial { - friction: 0.0, - restitution: 0.0, - ..Default::default() - } - .into(), - ..Default::default() - }; - commands - .entity(bike) - .insert_bundle(bbody) - .insert_bundle(bcollide) - .insert(ColliderPositionSync::Discrete); -} - -fn gravity(xform: Query<&Transform, With>, mut config: ResMut) { - let gravity = xform.single().translation.normalize() * -7.80; + let gravity = xform.single().translation.normalize() * -settings.gravity; config.gravity = gravity.into(); } @@ -145,7 +43,18 @@ fn falling_cat( forces.torque = torque.into(); } -fn update_forces( +fn drag( + mut query: Query<(&RigidBodyVelocityComponent, &mut RigidBodyForcesComponent), With>, +) { + let (vels, mut forces) = query.single_mut(); + + if let Some(vel) = vels.linvel.try_normalize(0.001) { + let v2 = vels.linvel.magnitude_squared(); + forces.force -= vel * v2 * 0.002; + } +} + +fn input_forces( settings: Res, input: Res, mut query: Query< @@ -171,29 +80,14 @@ fn update_forces( forces.torque += torque; } -fn drag( - mut query: Query<(&RigidBodyVelocityComponent, &mut RigidBodyForcesComponent), With>, -) { - let (vels, mut forces) = query.single_mut(); - - if let Some(vel) = vels.linvel.try_normalize(0.001) { - let v2 = vels.linvel.magnitude_squared(); - forces.force -= vel * v2 * 0.002; - } -} - pub struct CyberActionPlugin; impl Plugin for CyberActionPlugin { fn build(&self, app: &mut App) { app.init_resource::() .add_plugin(RapierPhysicsPlugin::::default()) - .add_startup_system_to_stage( - StartupStage::PostStartup, - setup_colliders.label("colliders"), - ) .add_system(gravity) .add_system(falling_cat.label("cat")) - .add_system(drag.label("drag").after("uforces")) - .add_system(update_forces.label("uforces").after("cat")); + .add_system(drag.label("drag").after("iforces")) + .add_system(input_forces.label("iforces").after("cat")); } } diff --git a/src/colliders.rs b/src/colliders.rs new file mode 100644 index 0000000..086c1db --- /dev/null +++ b/src/colliders.rs @@ -0,0 +1,119 @@ +use bevy::{ + prelude::*, + render::mesh::{Indices, VertexAttributeValues}, +}; +use bevy_rapier3d::prelude::*; + +use crate::geometry::{CyberBike, CyberSphere, PLANET_RADIUS, SPAWN_ALTITUDE}; + +fn setup_colliders( + mut commands: Commands, + meshes: Res>, + planet_query: Query<(Entity, &Handle), With>, + bike_query: Query<(Entity, &Transform), With>, +) { + let (planet, mesh_handle) = planet_query.single(); + + let mesh = meshes.get(mesh_handle).unwrap(); + + let mut shape = ColliderShape::ball(PLANET_RADIUS); + + let vertices = mesh.attribute(Mesh::ATTRIBUTE_POSITION).unwrap(); + if let VertexAttributeValues::Float32x3(verts) = vertices { + if let Some(Indices::U32(indices)) = mesh.indices() { + let mut idxs = Vec::new(); + for idx in indices.chunks_exact(3) { + idxs.push([idx[0], idx[1], idx[2]]); + } + let vertices = verts + .iter() + .map(|p| Point::from_slice(p)) + .collect::>>(); + + shape = ColliderShape::trimesh(vertices, idxs); + } + } + + let pbody = RigidBodyBundle { + body_type: RigidBodyType::Static.into(), + ccd: RigidBodyCcd { + ccd_enabled: true, + ccd_thickness: 2.0, + ccd_max_dist: SPAWN_ALTITUDE, + ..Default::default() + } + .into(), + ..Default::default() + }; + let pcollide = ColliderBundle { + shape: shape.into(), + material: ColliderMaterial { + friction: 0.05, + restitution: 0.00, + ..Default::default() + } + .into(), + ..Default::default() + }; + + commands + .entity(planet) + .insert_bundle(pbody) + .insert_bundle(pcollide); + + // bike is the easy part + let (bike, xform) = bike_query.single(); + setup_bike_collider(bike, xform, &mut commands); +} + +fn setup_bike_collider(bike: Entity, xform: &Transform, commands: &mut Commands) { + let mut bbody = RigidBodyBundle::default(); + + bbody.damping.angular_damping = 0.8; + bbody.damping.linear_damping = 0.1; + bbody.activation = RigidBodyActivation::cannot_sleep().into(); + + bbody.ccd = RigidBodyCcd { + ccd_enabled: true, + ccd_thickness: 0.7, + ccd_max_dist: 1.5, + ..Default::default() + } + .into(); + + 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.25).into(), + 0.4, + ); + let bcollide = ColliderBundle { + shape: shape.into(), + mass_properties: ColliderMassProps::Density(0.6).into(), + material: ColliderMaterial { + friction: 0.0, + restitution: 0.0, + ..Default::default() + } + .into(), + ..Default::default() + }; + commands + .entity(bike) + .insert_bundle(bbody) + .insert_bundle(bcollide) + .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"), + ); + } +} diff --git a/src/geometry.rs b/src/geometry.rs index 87b0eb8..62a2142 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -22,7 +22,7 @@ fn spawn_giant_sphere( mut meshes: ResMut>, mut materials: ResMut>, ) { - let color = Color::rgb(0.2, 0.17, 0.2); + let color = Color::rgb(0.2, 0.1, 0.2); let isphere = shape::Icosphere { radius: PLANET_RADIUS, subdivisions: 79, diff --git a/src/lib.rs b/src/lib.rs index 13e9be9..a218813 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use bevy::{ pub mod action; pub mod camera; +pub mod colliders; pub mod geometry; pub mod glamor; pub mod input; diff --git a/src/main.rs b/src/main.rs index 892da6d..2a96bbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use bevy::prelude::*; use cyber_rider::{ action::{CyberActionPlugin, MovementSettings}, camera::CyberCamPlugin, + colliders::CyberCollidersPlugin, disable_mouse_trap, geometry::CyberGeomPlugin, glamor::CyberGlamorPlugin, @@ -11,8 +12,9 @@ use cyber_rider::{ }; const MOVEMENT_SETTINGS: MovementSettings = MovementSettings { - sensitivity: 10.0, // default: 1.0 - accel: 30.0, // default: 40.0 + sensitivity: 10.0, // steering + accel: 30.0, // thrust + gravity: 8.0, }; fn main() { @@ -33,6 +35,7 @@ fn main() { .add_plugin(CyberCamPlugin) .add_plugin(CyberSpaceLightsPlugin) .add_plugin(CyberUIPlugin) + .add_plugin(CyberCollidersPlugin) .add_startup_system(disable_mouse_trap) .add_system(bevy::input::system::exit_on_esc_system); From 5b4b526aafd80a9975ca9230dbf41f9ecceeb7ed Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 26 Feb 2022 23:31:01 -0800 Subject: [PATCH 2/9] 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(); From 181110c309fd353c5104d029349b62b7765671ce Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 27 Feb 2022 16:18:26 -0800 Subject: [PATCH 3/9] Jesus, the cyber bike is fucking massive. --- src/colliders.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/colliders.rs b/src/colliders.rs index c5abe93..263d460 100644 --- a/src/colliders.rs +++ b/src/colliders.rs @@ -91,13 +91,13 @@ 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, -5.25).into(), - Vec3::new(0.0, 0.0, 5.25).into(), - 5.2, + Vec3::new(0.0, 0.0, -2.7).into(), + Vec3::new(0.0, 0.0, 2.0).into(), + 1.0, ); let bcollide = ColliderBundle { shape: shape.into(), - mass_properties: ColliderMassProps::Density(0.6).into(), + mass_properties: ColliderMassProps::Density(0.04).into(), material: ColliderMaterial { friction: 0.0, restitution: 0.0, From 281d8b8050e7ee015f129cae75832a14ac698330 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 28 Feb 2022 15:14:38 -0800 Subject: [PATCH 4/9] Add feature to enable debug collider viz. --- Cargo.toml | 2 ++ src/colliders.rs | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index ba220cc..59050de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,8 @@ noise = { git = "https://github.com/Razaekel/noise-rs" } hexasphere = "7" wgpu = "0.12" +[features] +debug_render = [] [dependencies.bevy] version = "0.6" diff --git a/src/colliders.rs b/src/colliders.rs index 263d460..dae6aad 100644 --- a/src/colliders.rs +++ b/src/colliders.rs @@ -123,6 +123,7 @@ fn setup_bike_collider(bike: Entity, xform: &Transform, commands: &mut Commands) pub struct CyberCollidersPlugin; impl Plugin for CyberCollidersPlugin { + #[cfg(feature = "debug_render")] fn build(&self, app: &mut App) { app.add_plugin(RapierRenderPlugin) .add_startup_system_to_stage( @@ -130,4 +131,12 @@ impl Plugin for CyberCollidersPlugin { setup_colliders.label("colliders"), ); } + + #[cfg(not(feature = "debug_render"))] + fn build(&self, app: &mut App) { + app.add_startup_system_to_stage( + StartupStage::PostStartup, + setup_colliders.label("colliders"), + ); + } } From dcdf11eeb9aa59579d55cbc7c19399fbbcb43e66 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 28 Feb 2022 15:32:04 -0800 Subject: [PATCH 5/9] Move bevy_rapier dep to git url instead of filesystem. --- Cargo.lock | 5 +++-- Cargo.toml | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 58b3243..a1602d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -461,6 +461,7 @@ dependencies = [ [[package]] name = "bevy_rapier3d" version = "0.12.1" +source = "git+https://github.com/nebkor/bevy_rapier?branch=debug-render-capsule#ab6be508dee4b38e886be67148ef4f7657f4c160" dependencies = [ "bevy", "nalgebra", @@ -2327,9 +2328,9 @@ checksum = "a0d463f2884048e7153449a55166f91028d5b0ea53c79377099ce4e8cf0cf9bb" [[package]] name = "redox_syscall" -version = "0.2.10" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +checksum = "8380fe0152551244f0747b1bf41737e0f8a74f97a14ccefd1148187271634f3c" dependencies = [ "bitflags", ] diff --git a/Cargo.toml b/Cargo.toml index 59050de..2fea74a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,8 @@ features = [ ] [dependencies.bevy_rapier3d] -path = "../bevy_rapier/bevy_rapier3d" +git = "https://github.com/nebkor/bevy_rapier" +branch = "debug-render-capsule" features = ["parallel", "simd-nightly"] # version = "0.12" From d068ba30dbbca0a87e0c62faa2c3c8afbd3beb70 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 28 Feb 2022 18:18:23 -0800 Subject: [PATCH 6/9] Fix overhangs in generated terrain. --- Cargo.lock | 2 +- Cargo.toml | 1 + src/geometry.rs | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a1602d3..e09fddb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -461,7 +461,7 @@ dependencies = [ [[package]] name = "bevy_rapier3d" version = "0.12.1" -source = "git+https://github.com/nebkor/bevy_rapier?branch=debug-render-capsule#ab6be508dee4b38e886be67148ef4f7657f4c160" +source = "git+https://github.com/nebkor/bevy_rapier?branch=debug-render-capsule#3387b8ff9d3615033144cb2e22ad4a965caa6b2b" dependencies = [ "bevy", "nalgebra", diff --git a/Cargo.toml b/Cargo.toml index 2fea74a..705b307 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ features = [ [dependencies.bevy_rapier3d] git = "https://github.com/nebkor/bevy_rapier" +#path = "../bevy_rapier/bevy_rapier3d" branch = "debug-render-capsule" features = ["parallel", "simd-nightly"] # version = "0.12" diff --git a/src/geometry.rs b/src/geometry.rs index 4389d7b..6c5445b 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -96,7 +96,7 @@ fn gen_planet(sphere: Icosphere) -> Mesh { .iter() .map(|&p| { let disp = noise.get(p.as_dvec3().into()) as f32 * 0.05; - let pt = p + disp; + let pt = p + (p.normalize() * disp); pt.into() }) .collect::>(); From 2d950401dc1399091c2d75e8aa6337a123825efe Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 1 Mar 2022 20:52:31 -0800 Subject: [PATCH 7/9] Remove normals in setup for planet. They get generated later in glamor.rs as flat normals on de-duped verts. --- src/geometry.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/geometry.rs b/src/geometry.rs index 6c5445b..04cf20f 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -87,11 +87,9 @@ fn gen_planet(sphere: Icosphere) -> Mesh { [norm_azimuth, norm_inclination] }); - // TODO: use displaced points for normals by replacing raw_points with - // noise-displaced points. let noise = HybridMulti::::default(); - let raw_points = generated + let noisy_points = generated .raw_points() .iter() .map(|&p| { @@ -101,17 +99,11 @@ fn gen_planet(sphere: Icosphere) -> Mesh { }) .collect::>(); - let points = raw_points + let points = noisy_points .iter() .map(|&p| (Vec3::from_slice(&p) * sphere.radius).into()) .collect::>(); - let normals = raw_points - .iter() - .copied() - .map(Into::into) - .collect::>(); - let uvs = generated.raw_data().to_owned(); let mut indices = Vec::with_capacity(generated.indices_per_main_triangle() * 20); @@ -125,7 +117,6 @@ fn gen_planet(sphere: Icosphere) -> Mesh { let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); mesh.set_indices(Some(indices)); mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, points); - mesh.set_attribute(Mesh::ATTRIBUTE_NORMAL, normals); mesh.set_attribute(Mesh::ATTRIBUTE_UV_0, uvs); mesh } From 93dd8bde9272f762c33726279d052b6e1e0c7a31 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 1 Mar 2022 21:42:16 -0800 Subject: [PATCH 8/9] Add debug camera and switching. Debug camera is in front and off to the side. --- src/camera.rs | 118 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 27 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 9305bfa..0667d90 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,58 +1,122 @@ -use bevy::prelude::*; - -use crate::{ - geometry::{CyberBikeModel, SPAWN_ALTITUDE}, - input::InputState, +use bevy::{ + prelude::*, + render::camera::{ActiveCameras, Camera, CameraPlugin}, }; -pub(crate) const CAM_DIST: f32 = 15.0; +use crate::{geometry::CyberBikeModel, input::InputState}; // 85 degrees in radians const MAX_PITCH: f32 = 1.48353; -#[derive(Component, Debug)] -pub struct CyberCam; +#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Component)] +enum CyberCameras { + Hero, + Debug, +} -fn setup_cybercam(mut commands: Commands) { - let projection = PerspectiveProjection { +impl CyberCameras { + fn next(self) -> Self { + match self { + CyberCameras::Debug => CyberCameras::Hero, + CyberCameras::Hero => CyberCameras::Debug, + } + } +} + +fn setup_cybercams(mut commands: Commands) { + let hero_projection = PerspectiveProjection { fov: std::f32::consts::FRAC_PI_3, ..Default::default() }; commands .spawn_bundle(PerspectiveCameraBundle { - transform: Transform::from_xyz(SPAWN_ALTITUDE + CAM_DIST, 0.0, 0.0) - .looking_at(Vec3::ZERO, Vec3::Y), - perspective_projection: projection, + perspective_projection: hero_projection, ..Default::default() }) - .insert(CyberCam); + .insert(CyberCameras::Hero); + + commands + .spawn_bundle(PerspectiveCameraBundle::with_name("Inactive")) + .insert(CyberCameras::Debug); } fn follow_cyberbike( - bike_query: Query<&Transform, (Without, With)>, - mut cam_query: Query<&mut Transform, (With, Without)>, + mut query: QuerySet<( + // 0: the bike + QueryState<&Transform, With>, + // 1: the cameras + QueryState<(&mut Transform, &CyberCameras)>, + )>, input: Res, ) { - let bike_xform = bike_query.single(); + let bike_xform = *query.q0().single(); let up = bike_xform.translation.normalize(); - let look_at = bike_xform.translation + (bike_xform.forward() * 200.0); - let cam_pos = bike_xform.translation + (bike_xform.back() * 2.7) + (up * 2.4); + for (mut cam_xform, cam_type) in query.q1().iter_mut() { + match *cam_type { + CyberCameras::Hero => { + let look_at = bike_xform.translation + (bike_xform.forward() * 200.0); + let cam_pos = bike_xform.translation + (bike_xform.back() * 2.7) + (up * 2.4); - let mut cam_xform = cam_query.single_mut(); - cam_xform.translation = cam_pos; - cam_xform.look_at(look_at, up); + cam_xform.translation = cam_pos; + cam_xform.look_at(look_at, up); - // handle input pitch - let angle = input.pitch.powi(3) * MAX_PITCH; - let axis = cam_xform.right(); - cam_xform.rotate(Quat::from_axis_angle(axis, angle)); + // handle input pitch + let angle = input.pitch.powi(3) * MAX_PITCH; + let axis = cam_xform.right(); + cam_xform.rotate(Quat::from_axis_angle(axis, angle)); + } + CyberCameras::Debug => { + let pos = bike_xform.translation + + (bike_xform.forward() * 10.0) + + (bike_xform.left() * 30.0) + + (bike_xform.up() * 7.0); + cam_xform.translation = pos; + cam_xform.look_at(bike_xform.translation, up); + } + } + } +} + +fn update_active_camera( + mut active_cams: ResMut, + state: Res>, + mut query: Query<(&mut Camera, &CyberCameras)>, +) { + active_cams.remove(CameraPlugin::CAMERA_3D); + + // set all cameras to inactive + for (mut cam, _) in query.iter_mut() { + cam.name = Some("Inactive".to_string()); + } + + // find the camera with the current state, set its name to be active + for (mut cam, _) in query + .iter_mut() + .filter(|(_, cybercam)| state.current().eq(cybercam)) + { + cam.name = Some(CameraPlugin::CAMERA_3D.to_string()); + } + + active_cams.add(CameraPlugin::CAMERA_3D); +} + +fn cycle_cam_state(mut state: ResMut>, mut keys: ResMut>) { + if keys.just_pressed(KeyCode::D) { + let new_state = state.current().next(); + info!("{:?}", new_state); + state.set(new_state).unwrap(); + keys.reset(KeyCode::D); + } } pub struct CyberCamPlugin; impl Plugin for CyberCamPlugin { fn build(&self, app: &mut bevy::prelude::App) { - app.add_startup_system(setup_cybercam) + app.add_startup_system(setup_cybercams) + .add_state(CyberCameras::Hero) + .add_system(cycle_cam_state) + .add_system(update_active_camera) .add_system(follow_cyberbike); } } From b6fa54a99c8ba0a30bdb98f0276eab726eec1af7 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 1 Mar 2022 22:31:44 -0800 Subject: [PATCH 9/9] Minor tidy. --- src/colliders.rs | 4 ++-- src/geometry.rs | 8 ++++---- src/glamor.rs | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/colliders.rs b/src/colliders.rs index dae6aad..0c910ea 100644 --- a/src/colliders.rs +++ b/src/colliders.rs @@ -4,7 +4,7 @@ use bevy::{ }; use bevy_rapier3d::prelude::*; -use crate::geometry::{CyberBikeModel, CyberSphere, PLANET_RADIUS, SPAWN_ALTITUDE}; +use crate::geometry::{CyberBikeModel, CyberPlanet, PLANET_RADIUS, SPAWN_ALTITUDE}; #[derive(Component)] pub struct CyberBikeBody; @@ -15,7 +15,7 @@ pub struct CyberBikeCollider; fn setup_colliders( mut commands: Commands, meshes: Res>, - planet_query: Query<(Entity, &Handle), With>, + planet_query: Query<(Entity, &Handle), With>, bike_query: Query<(Entity, &Transform), With>, ) { let (planet, mesh_handle) = planet_query.single(); diff --git a/src/geometry.rs b/src/geometry.rs index 04cf20f..a481b89 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -15,9 +15,9 @@ pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.015; pub struct CyberBikeModel; #[derive(Component)] -pub struct CyberSphere; +pub struct CyberPlanet; -fn spawn_giant_sphere( +fn spawn_planet( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, @@ -43,7 +43,7 @@ fn spawn_giant_sphere( ..Default::default() }) - .insert(CyberSphere); + .insert(CyberPlanet); } fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { @@ -65,7 +65,7 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { pub struct CyberGeomPlugin; impl Plugin for CyberGeomPlugin { fn build(&self, app: &mut App) { - app.add_startup_system(spawn_giant_sphere.label(Label::Geometry)) + app.add_startup_system(spawn_planet.label(Label::Geometry)) .add_startup_system(spawn_cyberbike.label(Label::Geometry)); } } diff --git a/src/glamor.rs b/src/glamor.rs index 1f8eb0c..9ef6dde 100644 --- a/src/glamor.rs +++ b/src/glamor.rs @@ -10,7 +10,7 @@ use bevy::{ use bevy_polyline::{Polyline, PolylineBundle, PolylineMaterial, PolylinePlugin}; use rand::{thread_rng, Rng}; -use crate::{geometry::CyberSphere, lights::AnimateCyberLightWireframe}; +use crate::{geometry::CyberPlanet, lights::AnimateCyberLightWireframe}; pub const BISEXY_COLOR: Color = Color::hsla(292.0, 0.9, 0.60, 1.1); @@ -19,7 +19,7 @@ fn wireframe_planet( mut meshes: ResMut>, mut polylines: ResMut>, mut polymats: ResMut>, - query: Query<&Handle, With>, + query: Query<&Handle, With>, ) { let handle = query.single(); let mesh = meshes.get_mut(handle).unwrap();