WIP crisper camera controls (prevent overshooting when braking)

This commit is contained in:
yuni 2024-11-17 00:25:43 +01:00
parent 3cef44c4b2
commit 2e35f90a19

View file

@ -822,6 +822,7 @@ fn handle_wants_acceleration(
let mut thruster_on = false; let mut thruster_on = false;
if let (Some(mut engine), Some(accel)) = (engine, accel) { if let (Some(mut engine), Some(accel)) = (engine, accel) {
let mut delta_v = DVec3::ZERO; let mut delta_v = DVec3::ZERO;
let mut allow_fullstop = false;
let boost = engine.current_boost_factor; let boost = engine.current_boost_factor;
engine.currently_matching_velocity = false; engine.currently_matching_velocity = false;
@ -836,7 +837,11 @@ fn handle_wants_acceleration(
} }
if accel.direction != DVec3::ZERO { if accel.direction != DVec3::ZERO {
// Player is pressing AWSD keys
delta_v += (trans.rotation * accel.direction.as_vec3()).as_dvec3(); delta_v += (trans.rotation * accel.direction.as_vec3()).as_dvec3();
} else if accel.brake {
// Player is only pressing space
allow_fullstop = true;
} }
if delta_v.length_squared() > 0.003 { if delta_v.length_squared() > 0.003 {
@ -858,8 +863,25 @@ fn handle_wants_acceleration(
* ENGINE_SPEED_FACTOR as f64 * ENGINE_SPEED_FACTOR as f64
* engine.current_boost_factor; * engine.current_boost_factor;
let final_accel = delta_v.normalize() * engine_factor * dt as f64;
// Apply acceleration to velocity // Apply acceleration to velocity
**v += delta_v.normalize() * engine_factor * dt as f64; if allow_fullstop {
if let Some(target_v) = closest_map.get(&entity) {
// Prevent overshooting when matching velocity, which
// would result in oscillating acceleration back and forth
for axis in 0..3 {
let original = v[axis];
let target = target_v[axis];
v[axis] += final_accel[axis];
if (original - target).signum() != (v[axis] - target).signum() {
v[axis] = target;
}
}
}
} else {
**v += final_accel;
}
// Visual effect // Visual effect
if engine.engine_type == EngineType::Monopropellant { if engine.engine_type == EngineType::Monopropellant {