finally fix player velocity/momentum

This commit is contained in:
yuni 2024-03-21 18:45:43 +01:00
parent 50d8d1faee
commit 624582f361

View file

@ -2,7 +2,7 @@ use bevy::prelude::*;
use bevy::input::mouse::MouseMotion;
use bevy::window::PrimaryWindow;
use std::f32::consts::*;
use crate::{settings, audio};
use crate::{settings, audio, actor};
pub struct CameraControllerPlugin;
@ -35,7 +35,7 @@ impl Default for CameraController {
enabled: true,
initialized: false,
sensitivity: 0.5,
move_speed: 1.0,
move_speed: 10.0,
friction: 0.1,
pitch: 1.0, // pitch=0/yaw=0 -> face sun
yaw: 0.3,
@ -52,7 +52,7 @@ fn run_camera_controller(
mut mouse_events: EventReader<MouseMotion>,
key_input: Res<ButtonInput<KeyCode>>,
thruster_sound_controller: Query<&AudioSink, With<audio::ComponentThrusterSound>>,
mut query: Query<(&mut Transform, &mut CameraController), With<Camera>>,
mut query: Query<(&mut Transform, &mut CameraController, &mut actor::Actor), With<Camera>>,
) {
let dt = time.delta_seconds();
let mut play_thruster_sound = false;
@ -63,7 +63,7 @@ fn run_camera_controller(
focused = window_result.unwrap().focused;
}
if let Ok((mut transform, mut controller)) = query.get_single_mut() {
if let Ok((mut transform, mut controller, mut actor)) = query.get_single_mut() {
if !controller.initialized {
controller.initialized = true;
transform.rotation =
@ -97,6 +97,12 @@ fn run_camera_controller(
}
}
if key_input.pressed(settings.key_stop) {
actor.v = actor.v * 0.99;
if actor.v.length_squared() < 1e-6 {
actor.v = Vec3::ZERO;
}
}
let friction = if key_input.pressed(settings.key_stop) {
controller.friction.clamp(0.0, 1.0)
} else {
@ -116,10 +122,16 @@ fn run_camera_controller(
}
let forward = *transform.forward();
let right = *transform.right();
transform.translation += controller.velocity.x * dt * right
actor.v += controller.velocity.x * dt * right
+ controller.velocity.y * dt * Vec3::Y
+ controller.velocity.z * dt * forward;
controller.velocity = Vec3::ZERO;
//transform.translation += controller.velocity.x * dt * right
//+ controller.velocity.y * dt * Vec3::Y
//+ controller.velocity.z * dt * forward;
// Handle mouse input
let mut mouse_delta = Vec2::ZERO;
for mouse_event in mouse_events.read() {