From 9a7b3b511a93e0f8d9488b338033b34ac64b9ba1 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 5 Jan 2022 00:16:22 -0800 Subject: [PATCH] Corrects orientation for local up. Adds dbg ui for orientation. Still using the model as the camera stand-in. Next up is enabling collision detection and gravity. --- src/flycam.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/src/flycam.rs b/src/flycam.rs index 8dcdb40..3256a19 100644 --- a/src/flycam.rs +++ b/src/flycam.rs @@ -8,10 +8,6 @@ use bevy::{ utils::tracing::info, }; -// stolen with neither shame nor pity from -// git@github.com:sburris0/bevy_flycam.git, b90f6fc, which is copyright 2020 -// Spencer Burris - pub const PLANET_RADIUS: f32 = 75.0; const PLAYER_DIST: f32 = PLANET_RADIUS + 300.0; @@ -21,6 +17,12 @@ struct PlayerState { velocity: Vec3, } +#[derive(Default)] +struct UpState { + player_up: Vec3, + cam_up: Vec3, +} + impl Default for PlayerState { fn default() -> Self { PlayerState { @@ -63,6 +65,9 @@ pub struct FlyCam; #[derive(Component, Debug)] pub struct CyberBike; +#[derive(Component)] +struct UpText; + /// Spawns the `Camera3dBundle` to be controlled fn setup_player(mut commands: Commands, asset_server: Res) { commands @@ -88,9 +93,49 @@ fn setup_player(mut commands: Commands, asset_server: Res) { .insert(CyberBike); } +fn setup_dbg_ui(mut commands: Commands, asset_server: Res) { + commands.spawn_bundle(UiCameraBundle::default()); + + commands + .spawn_bundle(TextBundle { + style: Style { + align_self: AlignSelf::FlexEnd, + ..Default::default() + }, + // Use `Text` directly + text: Text { + // Construct a `Vec` of `TextSection`s + sections: vec![TextSection { + value: "".to_string(), + style: TextStyle { + font: asset_server.load("fonts/FiraMono-Medium.ttf"), + font_size: 40.0, + color: Color::GOLD, + }, + }], + ..Default::default() + }, + ..Default::default() + }) + .insert(UpText); +} + +fn dbg_ui_system(state: Res, mut query: Query<&mut Text, With>) { + let cos = state.player_up.dot(state.cam_up); + + let mut text = query.single_mut(); + text.sections[0].value = format!( + "up: {:?}\ncam_up: {:?}\ntheta: {:.2}", + state.player_up, + state.cam_up, + cos.acos().to_degrees() + ); +} + fn player_move( time: Res