From 2f82a27ab254902ad66daacc8745b9c06e5e6ef1 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 01:19:17 +0200 Subject: [PATCH 01/23] change player's orbital position based on game start time --- src/data/defs.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/data/defs.txt b/src/data/defs.txt index 2d75984..f23d175 100644 --- a/src/data/defs.txt +++ b/src/data/defs.txt @@ -122,11 +122,11 @@ actor 0 0 0 only_in_map_at_dist 1e7 amalthea clickable no physics off - actor 0 127093 0 moonlet + actor 0 0 0 moonlet name Thebe relativeto jupiter id thebe - orbit 221900e3 0.34 + orbitaround jupiter 221900e3 scale 50e3 moon yes angularmomentum 0 0.025 0 @@ -259,7 +259,8 @@ actor 0 0 0 actor 0 593051 0 suitv2 template person relativeto jupiter - orbit 221900e3 0.338 + orbitaround jupiter 221900e3 + orbit_phase_offset 0.002 player yes id player wants maxvelocity none From 797b10625593e688b191ec3f48d882afc1b9ad61 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 01:21:41 +0200 Subject: [PATCH 02/23] lower player closer to the orbital plane --- src/data/defs.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data/defs.txt b/src/data/defs.txt index f23d175..3fd429e 100644 --- a/src/data/defs.txt +++ b/src/data/defs.txt @@ -256,7 +256,7 @@ actor 0 0 0 physics off -actor 0 593051 0 suitv2 +actor 0 59305 0 suitv2 template person relativeto jupiter orbitaround jupiter 221900e3 From 159dfe8e1943eaaf8c256301b32a9f6b49d75be4 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 02:34:18 +0200 Subject: [PATCH 03/23] add comment --- src/nature.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nature.rs b/src/nature.rs index 6b47701..fa35ab1 100644 --- a/src/nature.rs +++ b/src/nature.rs @@ -163,6 +163,7 @@ pub fn inverse_lorentz_factor_custom_c(speed: f64, c: f64) -> f64 { (1.0 - (speed.powf(2.0) / c.powf(2.0))).sqrt() } +/// Calculates orbit duration in seconds, with given parameters, assuming circular orbit. pub fn simple_orbital_period(mass: f64, distance: f64) -> f64 { return 2.0 * PI * (distance.powf(3.0) / (G * mass)).sqrt(); } From c6750eae461504da00077a12e6f86846c9e9501e Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 02:57:25 +0200 Subject: [PATCH 04/23] add nature::orbital_velocity --- src/nature.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/nature.rs b/src/nature.rs index fa35ab1..78b9284 100644 --- a/src/nature.rs +++ b/src/nature.rs @@ -11,6 +11,7 @@ // This module manages the messy, impure parts of our universe. use crate::prelude::*; +use bevy::prelude::Vec3; pub const OXYGEN_USE_KG_PER_S: f32 = 1e-5; pub const OXY_S: f32 = OXYGEN_USE_KG_PER_S; @@ -168,6 +169,17 @@ pub fn simple_orbital_period(mass: f64, distance: f64) -> f64 { return 2.0 * PI * (distance.powf(3.0) / (G * mass)).sqrt(); } +/// Calculates the orbital velocity with given parameters, assuming prograde circular orbit. +pub fn orbital_velocity(coords: Vec3, mass: f64) -> Vec3 { + let r = coords.length() as f64; + let speed = (G * mass / r).sqrt() as f32; + + // This generates a perpendicular orbital vector in the prograde direction + let perpendicular = Vec3::new(-coords.z, 0.0, -coords.x).normalize(); + + return perpendicular * speed; +} + pub fn phase_dist_to_coords(phase_radians: f64, distance: f64) -> DVec3 { return DVec3::new( distance * phase_radians.cos(), From 169b9ee2571edfd03376c582c19114f152e39f69 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 03:15:25 +0200 Subject: [PATCH 05/23] add nature::gravitational_acceleration, incl. test --- src/nature.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/nature.rs b/src/nature.rs index 78b9284..360059e 100644 --- a/src/nature.rs +++ b/src/nature.rs @@ -28,9 +28,11 @@ pub const G: f64 = 6.6743015e-11; // Gravitational constant in Nm²/kg² pub const SOL_RADIUS: f64 = 696_300_000.0; pub const JUPITER_RADIUS: f64 = 71_492_000.0; pub const JUPITER_RING_RADIUS: f64 = 229_000_000.0; +pub const EARTH_RADIUS: f64 = 6_371_000.0; pub const SOL_MASS: f64 = 1.9885e30; pub const JUPITER_MASS: f64 = 1.8982e27; +pub const EARTH_MASS: f64 = 5.972168e24; // Each star's values: (x, y, z, magnitude, color index, distance, name) pub const STARS: &[(f32, f32, f32, f32, f32, f32, &str)] = &include!("data/stars.in"); @@ -180,6 +182,22 @@ pub fn orbital_velocity(coords: Vec3, mass: f64) -> Vec3 { return perpendicular * speed; } +/// Calculates the acceleration towards a mass in m/s +pub fn gravitational_acceleration(coords: Vec3, mass: f64) -> Vec3 { + let r_squared = coords.length_squared() as f64; + let acceleration_magnitude = G * mass / r_squared; + return -acceleration_magnitude as f32 * (coords / r_squared.sqrt() as f32); +} + +#[test] +fn test_gravitational_acceleration() { + let coords = Vec3::new(EARTH_RADIUS as f32, 0.0, 0.0); + let mass = EARTH_MASS; + let g = gravitational_acceleration(coords, mass); + let g_rounded = (g * 10.0).round() / 10.0; + assert_eq!(g_rounded, Vec3::new(-9.8, 0.0, 0.0)); +} + pub fn phase_dist_to_coords(phase_radians: f64, distance: f64) -> DVec3 { return DVec3::new( distance * phase_radians.cos(), From 1614ece72afac240313d72b9c3bf8fbcb8ea5c6f Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 03:25:07 +0200 Subject: [PATCH 06/23] update the 2 new nature functions using DVec3 instead of Vec3 --- src/nature.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/nature.rs b/src/nature.rs index 360059e..17b8ae2 100644 --- a/src/nature.rs +++ b/src/nature.rs @@ -11,7 +11,6 @@ // This module manages the messy, impure parts of our universe. use crate::prelude::*; -use bevy::prelude::Vec3; pub const OXYGEN_USE_KG_PER_S: f32 = 1e-5; pub const OXY_S: f32 = OXYGEN_USE_KG_PER_S; @@ -172,30 +171,30 @@ pub fn simple_orbital_period(mass: f64, distance: f64) -> f64 { } /// Calculates the orbital velocity with given parameters, assuming prograde circular orbit. -pub fn orbital_velocity(coords: Vec3, mass: f64) -> Vec3 { - let r = coords.length() as f64; - let speed = (G * mass / r).sqrt() as f32; +pub fn orbital_velocity(coords: DVec3, mass: f64) -> DVec3 { + let r = coords.length(); + let speed = (G * mass / r).sqrt(); // This generates a perpendicular orbital vector in the prograde direction - let perpendicular = Vec3::new(-coords.z, 0.0, -coords.x).normalize(); + let perpendicular = DVec3::new(-coords.z, 0.0, -coords.x).normalize(); return perpendicular * speed; } /// Calculates the acceleration towards a mass in m/s -pub fn gravitational_acceleration(coords: Vec3, mass: f64) -> Vec3 { - let r_squared = coords.length_squared() as f64; +pub fn gravitational_acceleration(coords: DVec3, mass: f64) -> DVec3 { + let r_squared = coords.length_squared(); let acceleration_magnitude = G * mass / r_squared; - return -acceleration_magnitude as f32 * (coords / r_squared.sqrt() as f32); + return -acceleration_magnitude * (coords / r_squared.sqrt()); } #[test] fn test_gravitational_acceleration() { - let coords = Vec3::new(EARTH_RADIUS as f32, 0.0, 0.0); + let coords = DVec3::new(EARTH_RADIUS, 0.0, 0.0); let mass = EARTH_MASS; let g = gravitational_acceleration(coords, mass); let g_rounded = (g * 10.0).round() / 10.0; - assert_eq!(g_rounded, Vec3::new(-9.8, 0.0, 0.0)); + assert_eq!(g_rounded, DVec3::new(-9.8, 0.0, 0.0)); } pub fn phase_dist_to_coords(phase_radians: f64, distance: f64) -> DVec3 { From 24a9b208bda8cdcb823338db9dc2ed6b4eeabdf2 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 04:13:26 +0200 Subject: [PATCH 07/23] fix direction of orbital velocity --- src/nature.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nature.rs b/src/nature.rs index 17b8ae2..7071fc9 100644 --- a/src/nature.rs +++ b/src/nature.rs @@ -176,7 +176,7 @@ pub fn orbital_velocity(coords: DVec3, mass: f64) -> DVec3 { let speed = (G * mass / r).sqrt(); // This generates a perpendicular orbital vector in the prograde direction - let perpendicular = DVec3::new(-coords.z, 0.0, -coords.x).normalize(); + let perpendicular = DVec3::new(coords.z, 0.0, -coords.x).normalize(); return perpendicular * speed; } From e56f931951c87be36f64c3d5fde4988dd1c0bf7d Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 04:13:53 +0200 Subject: [PATCH 08/23] ignore gforce for 0.01s on startup, to survive a large starting velocity --- src/actor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/actor.rs b/src/actor.rs index 81c4151..7baef30 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -128,7 +128,7 @@ impl Default for ExperiencesGForce { visual_effect_threshold: 20.0, visual_effect: 0.0, last_linear_velocity: DVec3::splat(0.0), - ignore_gforce_seconds: 0.0, + ignore_gforce_seconds: 0.01, } } } From 974bf9cb8d4e7cd916aee0c8ed0cb6a7e4859258 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 04:14:51 +0200 Subject: [PATCH 09/23] add feature flag for toggling the generic ring asteroids --- src/world.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/world.rs b/src/world.rs index 0c09de0..05d6d32 100644 --- a/src/world.rs +++ b/src/world.rs @@ -19,6 +19,7 @@ use bevy_xpbd_3d::prelude::*; use fastrand; use std::collections::HashMap; +const ENABLE_ASTEROIDS: bool = true; const ASTEROID_UPDATE_INTERVAL: f32 = 0.1; // seconds const ASTEROID_SIZE_FACTOR: f32 = 10.0; const RING_THICKNESS: f64 = 8.0e6; @@ -36,19 +37,21 @@ pub struct WorldPlugin; impl Plugin for WorldPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); - app.add_systems(PostUpdate, handle_despawn); - app.add_systems(Update, spawn_despawn_asteroids); app.add_systems(Update, handle_respawn.run_if(on_event::())); app.add_plugins(PhysicsPlugins::default()); //app.add_plugins(PhysicsDebugPlugin::default()); app.insert_resource(Gravity(DVec3::splat(0.0))); - app.insert_resource(AsteroidUpdateTimer(Timer::from_seconds( - ASTEROID_UPDATE_INTERVAL, - TimerMode::Repeating, - ))); app.insert_resource(ActiveAsteroids(HashMap::new())); - app.add_event::(); app.add_event::(); + if ENABLE_ASTEROIDS { + app.insert_resource(AsteroidUpdateTimer(Timer::from_seconds( + ASTEROID_UPDATE_INTERVAL, + TimerMode::Repeating, + ))); + app.add_systems(Update, spawn_despawn_asteroids); + app.add_systems(PostUpdate, handle_despawn_asteroids); + app.add_event::(); + } } } @@ -344,7 +347,7 @@ fn spawn_despawn_asteroids( } } -fn handle_despawn( +fn handle_despawn_asteroids( mut commands: Commands, mut er_despawn: EventReader, mut db: ResMut, From 7be6b0746fa73626eea0c5401323b7532c69a549 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 04:15:16 +0200 Subject: [PATCH 10/23] disable ring asteroids by default --- src/world.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/world.rs b/src/world.rs index 05d6d32..a703844 100644 --- a/src/world.rs +++ b/src/world.rs @@ -19,7 +19,7 @@ use bevy_xpbd_3d::prelude::*; use fastrand; use std::collections::HashMap; -const ENABLE_ASTEROIDS: bool = true; +const ENABLE_ASTEROIDS: bool = false; const ASTEROID_UPDATE_INTERVAL: f32 = 0.1; // seconds const ASTEROID_SIZE_FACTOR: f32 = 10.0; const RING_THICKNESS: f64 = 8.0e6; From e16a650b221d143d5735b2b9b28729c5ba395ef2 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 04:17:28 +0200 Subject: [PATCH 11/23] apply gravity towards Jupiter for objects orbiting Jupiter --- src/actor.rs | 34 ++++++++++++++++++++++++++++++++++ src/cmd.rs | 4 ++++ 2 files changed, 38 insertions(+) diff --git a/src/actor.rs b/src/actor.rs index 7baef30..ed94544 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -44,6 +44,7 @@ impl Plugin for ActorPlugin { app.add_systems( Update, ( + handle_gravity, handle_input.run_if(in_control), handle_collisions, handle_damage, @@ -51,9 +52,16 @@ impl Plugin for ActorPlugin { ); app.add_systems(PostUpdate, (handle_vehicle_enter_exit,)); app.add_event::(); + app.insert_resource(GravityUpdateTimer(Timer::from_seconds( + 1.0, + TimerMode::Repeating, + ))); } } +#[derive(Resource)] +pub struct GravityUpdateTimer(Timer); + #[derive(Copy, Clone)] pub enum DamageType { Unknown, @@ -157,6 +165,8 @@ pub struct WantsMaxVelocity(pub f64); pub struct WantsToLookAt(pub String); #[derive(Component)] pub struct Identifier(pub String); +#[derive(Component)] +pub struct OrbitsJupiter; #[derive(Component)] pub struct LifeForm { @@ -632,3 +642,27 @@ fn handle_gforce( } } } + +fn handle_gravity( + time: Res