From c6750eae461504da00077a12e6f86846c9e9501e Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 8 Jun 2024 02:57:25 +0200 Subject: [PATCH] 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(),