From b700e0fe248c504bf7310e0a0a9e4443d5f67815 Mon Sep 17 00:00:00 2001 From: hut Date: Fri, 5 Apr 2024 20:57:21 +0200 Subject: [PATCH] implement matching velocity with targeted object --- README.md | 2 +- src/camera.rs | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 33fbf23..b4f052d 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Links: # Key Bindings -- Space: Slow down +- Space: Slow down (or match velocity) - AWSD/Shift/Ctrl: Accelerate - R: Rotate (hold & move mouse) - E: Interact: Talk to people, enter vehicles diff --git a/src/camera.rs b/src/camera.rs index 2d5c2dc..58f5f9f 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -8,7 +8,7 @@ use bevy::transform::TransformSystem; use bevy::math::{DVec3, DQuat}; use bevy_xpbd_3d::prelude::*; use std::f32::consts::PI; -use crate::{actor, audio, settings}; +use crate::{actor, audio, hud, settings}; pub struct CameraPlugin; @@ -151,6 +151,7 @@ fn apply_input_to_player( rocket_sound_controller: Query<&AudioSink, With>, ion_sound_controller: Query<&AudioSink, With>, electricmotor_sound_controller: Query<&AudioSink, With>, + q_target: Query<&LinearVelocity, (With, Without)>, mut q_playercam: Query<( &Transform, &mut actor::Engine, @@ -169,6 +170,12 @@ fn apply_input_to_player( 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() { // Handle key input if focused { @@ -191,7 +198,7 @@ fn apply_input_to_player( axis_input.y -= 1.2; } 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 { 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; if key_input.pressed(settings.key_stop) { // Decelerate + let dv = target_v - v.0; for i in 0..3 { - if v[i].abs() < threshold { - v[i] = 0.0; + if dv[i].abs() < threshold { + 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 - v[i] = 0.0; + v[i] = target_v[i]; acceleration_total[i] = 0.0; } }