automatically match velocity to the closest object

This commit is contained in:
yuni 2024-07-13 14:30:02 +02:00
parent 5e9441dd5e
commit e5f9409fd8

View file

@ -25,6 +25,7 @@ use bevy_xpbd_3d::prelude::*;
use std::collections::HashMap; use std::collections::HashMap;
pub const INITIAL_ZOOM_LEVEL: f64 = 10.0; pub const INITIAL_ZOOM_LEVEL: f64 = 10.0;
pub const MAX_DIST_FOR_MATCH_VELOCITY: f64 = 10000.0;
pub struct CameraPlugin; pub struct CameraPlugin;
@ -422,6 +423,7 @@ pub fn apply_input_to_player(
key_input: Res<ButtonInput<KeyCode>>, key_input: Res<ButtonInput<KeyCode>>,
q_audiosinks: Query<(&audio::Sfx, &AudioSink)>, q_audiosinks: Query<(&audio::Sfx, &AudioSink)>,
q_target: Query<&LinearVelocity, (With<hud::IsTargeted>, Without<actor::PlayerCamera>)>, q_target: Query<&LinearVelocity, (With<hud::IsTargeted>, Without<actor::PlayerCamera>)>,
q_closest: Query<(&Position, &LinearVelocity), (Without<hud::IsTargeted>, Without<actor::PlayerCamera>)>,
mut q_playercam: Query< mut q_playercam: Query<
( (
Entity, Entity,
@ -458,9 +460,22 @@ pub fn apply_input_to_player(
{ {
let target_v: DVec3 = if let Ok(target) = q_target.get_single() { let target_v: DVec3 = if let Ok(target) = q_target.get_single() {
target.0 target.0
} else {
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 { } else {
let relative_pos = pos.0 - jupiter_pos.0; let relative_pos = pos.0 - jupiter_pos.0;
nature::orbital_velocity(relative_pos, nature::JUPITER_MASS) nature::orbital_velocity(relative_pos, nature::JUPITER_MASS)
}
}; };
// Handle key input // Handle key input
if focused { if focused {