implement matching velocity with targeted object

This commit is contained in:
yuni 2024-04-05 20:57:21 +02:00
parent 2f65c652c8
commit b700e0fe24
2 changed files with 15 additions and 7 deletions

View file

@ -27,7 +27,7 @@ Links:
# Key Bindings # Key Bindings
- Space: Slow down - Space: Slow down (or match velocity)
- AWSD/Shift/Ctrl: Accelerate - AWSD/Shift/Ctrl: Accelerate
- R: Rotate (hold & move mouse) - R: Rotate (hold & move mouse)
- E: Interact: Talk to people, enter vehicles - E: Interact: Talk to people, enter vehicles

View file

@ -8,7 +8,7 @@ use bevy::transform::TransformSystem;
use bevy::math::{DVec3, DQuat}; use bevy::math::{DVec3, DQuat};
use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::prelude::*;
use std::f32::consts::PI; use std::f32::consts::PI;
use crate::{actor, audio, settings}; use crate::{actor, audio, hud, settings};
pub struct CameraPlugin; pub struct CameraPlugin;
@ -151,6 +151,7 @@ fn apply_input_to_player(
rocket_sound_controller: Query<&AudioSink, With<audio::ComponentRocketSound>>, rocket_sound_controller: Query<&AudioSink, With<audio::ComponentRocketSound>>,
ion_sound_controller: Query<&AudioSink, With<audio::ComponentIonSound>>, ion_sound_controller: Query<&AudioSink, With<audio::ComponentIonSound>>,
electricmotor_sound_controller: Query<&AudioSink, With<audio::ComponentElectricMotorSound>>, electricmotor_sound_controller: Query<&AudioSink, With<audio::ComponentElectricMotorSound>>,
q_target: Query<&LinearVelocity, (With<hud::IsTargeted>, Without<actor::PlayerCamera>)>,
mut q_playercam: Query<( mut q_playercam: Query<(
&Transform, &Transform,
&mut actor::Engine, &mut actor::Engine,
@ -169,6 +170,12 @@ fn apply_input_to_player(
focused = window_result.unwrap().focused; focused = window_result.unwrap().focused;
} }
let target_v: DVec3 = if let Ok(target) = q_target.get_single() {
target.0
} else {
DVec3::splat(0.0)
};
if let Ok((player_transform, mut engine, mut angularvelocity, mut v, mut torque)) = q_playercam.get_single_mut() { if let Ok((player_transform, mut engine, mut angularvelocity, mut v, mut torque)) = q_playercam.get_single_mut() {
// Handle key input // Handle key input
if focused { if focused {
@ -191,7 +198,7 @@ fn apply_input_to_player(
axis_input.y -= 1.2; axis_input.y -= 1.2;
} }
if key_input.pressed(settings.key_stop) { if key_input.pressed(settings.key_stop) {
let stop_direction = -v.normalize(); let stop_direction = (target_v - v.0).normalize();
if stop_direction.length_squared() > 0.3 { if stop_direction.length_squared() > 0.3 {
axis_input += 1.0 * DVec3::from(player_transform.rotation.inverse() * stop_direction.as_vec3()); axis_input += 1.0 * DVec3::from(player_transform.rotation.inverse() * stop_direction.as_vec3());
} }
@ -220,13 +227,14 @@ fn apply_input_to_player(
let threshold = 1e-5; let threshold = 1e-5;
if key_input.pressed(settings.key_stop) { if key_input.pressed(settings.key_stop) {
// Decelerate // Decelerate
let dv = target_v - v.0;
for i in 0..3 { for i in 0..3 {
if v[i].abs() < threshold { if dv[i].abs() < threshold {
v[i] = 0.0; v[i] = target_v[i];
} }
else if v[i].signum() != (v[i] + acceleration_total[i]).signum() { else if dv[i].signum() != (dv[i] + acceleration_total[i]).signum() {
// Almost stopped, but we overshot v=0 // Almost stopped, but we overshot v=0
v[i] = 0.0; v[i] = target_v[i];
acceleration_total[i] = 0.0; acceleration_total[i] = 0.0;
} }
} }