From 3a1c4ebff8c25162c6b076d07b47997db7fc313b Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 26 Jan 2022 16:53:37 -0800 Subject: [PATCH] Handle pitching the camera with controller. --- src/camera.rs | 14 +++++++++++++- src/input.rs | 6 ++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 35b7303..c084d87 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,9 +1,15 @@ use bevy::prelude::*; -use crate::geometry::{CyberBike, SPAWN_ALTITUDE}; +use crate::{ + geometry::{CyberBike, SPAWN_ALTITUDE}, + input::InputState, +}; pub(crate) const CAM_DIST: f32 = 15.0; +// 85 degrees in radians +const MAX_PITCH: f32 = 1.48353; + #[derive(Component, Debug)] pub struct CyberCam; @@ -20,6 +26,7 @@ fn setup_cybercam(mut commands: Commands) { fn follow_cyberbike( bike_query: Query<&Transform, (Without, With)>, mut cam_query: Query<&mut Transform, (With, Without)>, + input: Res, ) { let bike_xform = bike_query.single(); let up = bike_xform.translation.normalize(); @@ -30,6 +37,11 @@ fn follow_cyberbike( let mut cam_xform = cam_query.single_mut(); 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)); } pub struct CyberCamPlugin; diff --git a/src/input.rs b/src/input.rs index 4010952..3d4ad18 100644 --- a/src/input.rs +++ b/src/input.rs @@ -9,7 +9,7 @@ pub(crate) struct InputState { pub yaw: f32, pub throttle: f32, pub brake: bool, - pub _pitch: f32, + pub pitch: f32, } fn update_input(events: Res>, mut istate: ResMut) { @@ -32,7 +32,9 @@ fn update_input(events: Res>, mut istate: ResMut {} + GamepadEventType::AxisChanged(GamepadAxisType::LeftStickY, val) => { + istate.pitch = val; + } _ => { info!("unhandled gamepad event: {:?}", ev); }