Handle pitching the camera with controller.

This commit is contained in:
Joe Ardent 2022-01-26 16:53:37 -08:00
parent 7cb70b218b
commit 3a1c4ebff8
2 changed files with 17 additions and 3 deletions

View File

@ -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<CyberCam>, With<CyberBike>)>,
mut cam_query: Query<&mut Transform, (With<CyberCam>, Without<CyberBike>)>,
input: Res<InputState>,
) {
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;

View File

@ -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<Events<GamepadEvent>>, mut istate: ResMut<InputState>) {
@ -32,7 +32,9 @@ fn update_input(events: Res<Events<GamepadEvent>>, mut istate: ResMut<InputState
istate.yaw = -val;
}
// ignore spurious vertical movement for now
GamepadEventType::AxisChanged(GamepadAxisType::LeftStickY, _) => {}
GamepadEventType::AxisChanged(GamepadAxisType::LeftStickY, val) => {
istate.pitch = val;
}
_ => {
info!("unhandled gamepad event: {:?}", ev);
}