From 3814aa415558e745d2fcacf81b31490b2bfdefa7 Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 13 May 2024 06:10:34 +0200 Subject: [PATCH] implement special relativity (for speed cheats only, so far) --- src/game.rs | 14 ++++++++------ src/nature.rs | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/game.rs b/src/game.rs index 5ee3351..4a7ba8f 100644 --- a/src/game.rs +++ b/src/game.rs @@ -136,13 +136,15 @@ fn handle_cheats( gforce.ignore_gforce_seconds = 1.0; v.0 = DVec3::ZERO; } - if key_input.pressed(settings.key_cheat_speed) { + if key_input.pressed(settings.key_cheat_speed) || key_input.pressed(settings.key_cheat_speed_backward) { gforce.ignore_gforce_seconds = 1.0; - v.0 += DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, boost)); - } - if key_input.pressed(settings.key_cheat_speed_backward) { - gforce.ignore_gforce_seconds = 1.0; - v.0 += DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, -boost)); + let sign = if key_input.pressed(settings.key_cheat_speed) { 1.0 } else { -1.0 }; + let dv = DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, sign * boost)); + let current_speed = v.0.length(); + let next_speed = (v.0 + dv).length(); + let avg_speed = (current_speed + next_speed) / 2.0; + let inv_lorentz = nature::inverse_lorentz_factor(avg_speed.clamp(0.0, nature::C)); + v.0 = v.0 + inv_lorentz * dv; } if key_input.just_pressed(settings.key_cheat_teleport) { if let Ok((transform, target_pos, target_v)) = q_target.get_single() { diff --git a/src/nature.rs b/src/nature.rs index 576cf0a..5704318 100644 --- a/src/nature.rs +++ b/src/nature.rs @@ -147,11 +147,11 @@ pub fn lorentz_factor_custom_c(speed: f64, c: f64) -> f64 { } pub fn inverse_lorentz_factor(speed: f64) -> f64 { - (1.0 - (speed.powf(2.0) / C.powf(2.0))).powf(0.5) + (1.0 - (speed.powf(2.0) / C.powf(2.0))).sqrt() } pub fn inverse_lorentz_factor_custom_c(speed: f64, c: f64) -> f64 { - (1.0 - (speed.powf(2.0) / c.powf(2.0))).powf(0.5) + (1.0 - (speed.powf(2.0) / c.powf(2.0))).sqrt() } pub fn simple_orbital_period(mass: f64, distance: f64) -> f64 {