From b72a5965592cd034671b0a1db338a7cc960a608c Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 8 Apr 2024 03:00:48 +0200 Subject: [PATCH] fix distance display, and autoconvert large numbers to other units --- src/hud.rs | 15 ++++++++------- src/nature.rs | 21 +++++++++++++++++++++ src/world.rs | 8 +++++--- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/hud.rs b/src/hud.rs index 7ceccd2..c7b6ace 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -522,17 +522,14 @@ fn update_hud( text.sections[17].value = format!("{integrity:.0}%"); let speed = cam_v.length(); let kmh = speed * 60.0 * 60.0 / 1000.0; - text.sections[19].value = format!("{speed:.0}m/s | {kmh:.0}km/h"); + let speed_readable = nature::readable_distance(speed); + text.sections[19].value = format!("{speed_readable}/s | {kmh:.0}km/h"); // Target display let (x, y, z, dist_scalar) : (f64, f64, f64, f64); if let Ok((_, IsClickable { distance: Some(dist), .. })) = q_target.get_single() { - if *dist >= 100000.0 { - dist_scalar = f64::NAN; - } else { - dist_scalar = *dist; - } (x, y, z) = (0.0, 0.0, 0.0); + dist_scalar = *dist; } else { let target: Option; @@ -560,7 +557,11 @@ fn update_hud( text.sections[7].value = format!("distance: UNKNOWN"); } else if dist_scalar != 0.0 { - text.sections[7].value = format!("{x:.0}m / {z:.0}m / {y:.0}m / distance: {dist_scalar:.0}m"); + let x_readable = nature::readable_distance(x); + let y_readable = nature::readable_distance(y); + let z_readable = nature::readable_distance(z); + let distance_readable = nature::readable_distance(dist_scalar); + text.sections[7].value = format!("{x_readable} / {z_readable} / {y_readable} / distance: {distance_readable}"); } else { text.sections[7].value = format!("TARGET ERROR"); diff --git a/src/nature.rs b/src/nature.rs index 66a02ef..6d16b69 100644 --- a/src/nature.rs +++ b/src/nature.rs @@ -7,6 +7,7 @@ pub const OXY_S: f32 = OXYGEN_USE_KG_PER_S; pub const OXY_M: f32 = OXYGEN_USE_KG_PER_S * 60.0; pub const OXY_H: f32 = OXYGEN_USE_KG_PER_S * 60.0 * 60.0; pub const OXY_D: f32 = OXYGEN_USE_KG_PER_S * 60.0 * 60.0 * 24.0; +pub const LIGHTYEAR2METER: f64 = 9_460_730_472_580_800.0; pub const PARSEC2METER: f64 = 3.0857e16; pub const DIST_JUPTER_SUN: f64 = 778479.0e6; pub const EARTH_GRAVITY: f32 = 9.81; @@ -81,3 +82,23 @@ pub fn ring_density(radius: f32) -> f32 { return density; } + +pub fn readable_distance(distance: f64) -> String { + if distance > LIGHTYEAR2METER * 0.001 { + let lightyears = distance / LIGHTYEAR2METER; + return format!("{lightyears:.3}ly"); + } + if distance >= 1.0e10 { + let gigameters = distance * 1.0e-9; + return format!("{gigameters:.1}Gm"); + } + if distance >= 1.0e7 { + let megameters = distance * 1.0e-6; + return format!("{megameters:.1}Mm"); + } + if distance >= 1.0e4 { + let kilometers = distance * 1.0e-3; + return format!("{kilometers:.1}km"); + } + return format!("{distance:.1}m"); +} diff --git a/src/world.rs b/src/world.rs index d47486b..5f8013e 100644 --- a/src/world.rs +++ b/src/world.rs @@ -151,9 +151,11 @@ pub fn setup( }); let mesh_distance = 1e9; let starchart_distance = if is_sun { - nature::DIST_JUPTER_SUN + Some(nature::DIST_JUPTER_SUN) + } else if star.5 >= 100000.0 { + None } else { - nature::PARSEC2METER * star.5 as f64 + Some(nature::PARSEC2METER * star.5 as f64) }; let name = if star.6.is_empty() { "Uncharted Star".to_string() @@ -164,7 +166,7 @@ pub fn setup( Star, hud::IsClickable { name: Some(name), - distance: Some(starchart_distance), + distance: starchart_distance, }, PbrBundle { mesh: sphere_handle.clone(),