add "target_velocity" var to handle_wants_maxvelocity()

This commit is contained in:
yuni 2024-06-11 00:05:03 +02:00
parent d9af542d54
commit 76272a7fc2

View file

@ -545,25 +545,32 @@ fn handle_wants_maxrotation(
}
}
/// Slows down NPC's movement until they reach their target velocity.
fn handle_wants_maxvelocity(
time: Res<Time>,
mut query: Query<(&mut LinearVelocity, &Engine, &WantsMaxVelocity)>,
) {
let dt = time.delta_seconds();
for (mut v, engine, maxv) in &mut query {
let total = v.0.length();
if total <= maxv.0 + EPSILON {
if total > maxv.0 {
v.0 = DVec3::splat(0.0);
let target_velocity = DVec3::splat(0.0);
let relative_velocity = v.0 - target_velocity;
let relative_speed = relative_velocity.length();
if relative_speed <= maxv.0 + EPSILON {
// it's already pretty close to the target
if relative_speed > maxv.0 {
// but not quite the target, so let's set it to the target
v.0 = target_velocity;
}
} else {
// slow it down a little bit
// TODO: respect engine parameters for different thrusts for different directions
let avg_thrust =
(engine.thrust_forward + engine.thrust_back + engine.thrust_sideways) / 3.0;
let acceleration = (avg_thrust * dt) as f64 * -v.0;
let acceleration = (avg_thrust * dt) as f64 * -relative_velocity;
v.0 += acceleration;
if v.0.length() + EPSILON < acceleration.length() {
v.0 = DVec3::splat(0.0);
v.0 = target_velocity;
}
}
}