From e5f9409fd80ca978339f534334d3879882145c61 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 13 Jul 2024 14:30:02 +0200 Subject: [PATCH] automatically match velocity to the closest object --- src/camera.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index e8aa821..472bd14 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -25,6 +25,7 @@ use bevy_xpbd_3d::prelude::*; use std::collections::HashMap; pub const INITIAL_ZOOM_LEVEL: f64 = 10.0; +pub const MAX_DIST_FOR_MATCH_VELOCITY: f64 = 10000.0; pub struct CameraPlugin; @@ -422,6 +423,7 @@ pub fn apply_input_to_player( key_input: Res>, q_audiosinks: Query<(&audio::Sfx, &AudioSink)>, q_target: Query<&LinearVelocity, (With, Without)>, + q_closest: Query<(&Position, &LinearVelocity), (Without, Without)>, mut q_playercam: Query< ( Entity, @@ -459,8 +461,21 @@ pub fn apply_input_to_player( let target_v: DVec3 = if let Ok(target) = q_target.get_single() { target.0 } else { - let relative_pos = pos.0 - jupiter_pos.0; - nature::orbital_velocity(relative_pos, nature::JUPITER_MASS) + let mut closest_distance = MAX_DIST_FOR_MATCH_VELOCITY; + let mut closest_velocity = None; + for (testpos, v) in q_closest.iter() { + let distance = (pos.0 - testpos.0).length(); + if distance < closest_distance { + closest_velocity = Some(v); + closest_distance = distance; + } + } + if closest_velocity.is_some() { + closest_velocity.unwrap().0 + } else { + let relative_pos = pos.0 - jupiter_pos.0; + nature::orbital_velocity(relative_pos, nature::JUPITER_MASS) + } }; // Handle key input if focused {