add input plugin, doesn't do anything yet
This commit is contained in:
parent
15c79c62b8
commit
4bfd8740fb
3 changed files with 69 additions and 9 deletions
51
src/input.rs
Normal file
51
src/input.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use bevy::{
|
||||
input::gamepad::{GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadEvent},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
#[derive(Default, Debug, Resource)]
|
||||
pub(crate) struct InputState {
|
||||
pub yaw: f32,
|
||||
pub throttle: f32,
|
||||
pub brake: bool,
|
||||
pub pitch: f32,
|
||||
}
|
||||
|
||||
fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputState>) {
|
||||
for pad_event in events.read() {
|
||||
match pad_event {
|
||||
GamepadEvent::Button(button_event) => {
|
||||
let GamepadButtonChangedEvent { button, value, .. } = button_event;
|
||||
match button {
|
||||
GamepadButton::RightTrigger2 => istate.throttle = *value,
|
||||
GamepadButton::LeftTrigger2 => istate.throttle = -value,
|
||||
GamepadButton::East => {
|
||||
istate.brake = value > &0.5;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
GamepadEvent::Axis(axis_event) => {
|
||||
let GamepadAxisChangedEvent { axis, value, .. } = axis_event;
|
||||
match axis {
|
||||
GamepadAxis::LeftStickX => {
|
||||
istate.yaw = *value;
|
||||
}
|
||||
GamepadAxis::RightStickY => {
|
||||
istate.pitch = *value;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
GamepadEvent::Connection(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CyberInputPlugin;
|
||||
impl Plugin for CyberInputPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<InputState>()
|
||||
.add_systems(Update, (update_input));
|
||||
}
|
||||
}
|
|
@ -15,10 +15,12 @@ use bevy_inspector_egui::quick::WorldInspectorPlugin;
|
|||
|
||||
mod bike;
|
||||
mod camera;
|
||||
mod input;
|
||||
mod physics;
|
||||
|
||||
use bike::CyberBikePlugin;
|
||||
use camera::CamPlug;
|
||||
use input::CyberInputPlugin;
|
||||
use physics::CyberPhysicsPlugin;
|
||||
|
||||
fn main() {
|
||||
|
@ -27,6 +29,7 @@ fn main() {
|
|||
DefaultPlugins,
|
||||
CamPlug,
|
||||
CyberBikePlugin,
|
||||
CyberInputPlugin,
|
||||
CyberPhysicsPlugin,
|
||||
WorldInspectorPlugin::new(),
|
||||
))
|
||||
|
|
|
@ -45,12 +45,6 @@ impl Default for CatControllerState {
|
|||
}
|
||||
|
||||
impl CatControllerState {
|
||||
pub fn decay(&mut self) {
|
||||
if self.roll_integral.abs() > 0.001 {
|
||||
self.roll_integral *= self.decay_factor;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_roll(&mut self, error: f32, dt: f32) -> (f32, f32) {
|
||||
let lim = self.roll_limit;
|
||||
self.roll_integral = (self.roll_integral + (error * dt)).min(lim).max(-lim);
|
||||
|
@ -65,13 +59,13 @@ impl CatControllerState {
|
|||
}
|
||||
|
||||
mod systems {
|
||||
use std::f32::consts::FRAC_PI_3;
|
||||
use std::f32::consts::{FRAC_PI_3, FRAC_PI_4};
|
||||
|
||||
use avian3d::prelude::*;
|
||||
use bevy::prelude::*;
|
||||
|
||||
use super::{CatControllerSettings, CatControllerState, CyberLean};
|
||||
use crate::bike::CyberBikeBody;
|
||||
use crate::{bike::CyberBikeBody, input::InputState};
|
||||
|
||||
fn _yaw_to_angle(yaw: f32) -> f32 {
|
||||
let v = yaw.powi(5) * FRAC_PI_3;
|
||||
|
@ -95,13 +89,15 @@ mod systems {
|
|||
|
||||
pub(super) fn calculate_lean(
|
||||
bike_state: Query<(&LinearVelocity, &Transform), With<CyberBikeBody>>,
|
||||
input: Res<InputState>,
|
||||
mut lean: ResMut<CyberLean>,
|
||||
) {
|
||||
let (velocity, xform) = bike_state.single();
|
||||
let vel = velocity.dot(*xform.forward());
|
||||
let v_squared = vel.powi(2);
|
||||
let steering_angle = yaw_to_angle(input.yaw);
|
||||
let wheel_base = 1.145f32;
|
||||
let radius = wheel_base;
|
||||
let radius = wheel_base / steering_angle.tan();
|
||||
let gravity = -9.8f32;
|
||||
let v2_r = v_squared / radius;
|
||||
let tan_theta = (v2_r / gravity).clamp(-FRAC_PI_3, FRAC_PI_3);
|
||||
|
@ -121,6 +117,7 @@ mod systems {
|
|||
mut gizmos: Gizmos,
|
||||
) {
|
||||
let (xform, mut torque, mut control_vars) = bike_query.single_mut();
|
||||
|
||||
let world_up = Vec3::Y; //xform.translation.normalize();
|
||||
let rot = Quat::from_axis_angle(*xform.back(), lean.lean);
|
||||
let target_up = rotate_point(&world_up, &rot).normalize();
|
||||
|
@ -160,6 +157,15 @@ mod systems {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn yaw_to_angle(yaw: f32) -> f32 {
|
||||
let v = yaw.powi(5) * FRAC_PI_4;
|
||||
if v.is_normal() {
|
||||
v
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use systems::{apply_lean, calculate_lean};
|
||||
|
|
Loading…
Reference in a new issue