update the 2 new nature functions using DVec3 instead of Vec3

This commit is contained in:
yuni 2024-06-08 03:25:07 +02:00
parent 169b9ee257
commit 1614ece72a

View file

@ -11,7 +11,6 @@
// This module manages the messy, impure parts of our universe. // This module manages the messy, impure parts of our universe.
use crate::prelude::*; use crate::prelude::*;
use bevy::prelude::Vec3;
pub const OXYGEN_USE_KG_PER_S: f32 = 1e-5; pub const OXYGEN_USE_KG_PER_S: f32 = 1e-5;
pub const OXY_S: f32 = OXYGEN_USE_KG_PER_S; 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. /// Calculates the orbital velocity with given parameters, assuming prograde circular orbit.
pub fn orbital_velocity(coords: Vec3, mass: f64) -> Vec3 { pub fn orbital_velocity(coords: DVec3, mass: f64) -> DVec3 {
let r = coords.length() as f64; let r = coords.length();
let speed = (G * mass / r).sqrt() as f32; let speed = (G * mass / r).sqrt();
// This generates a perpendicular orbital vector in the prograde direction // 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; return perpendicular * speed;
} }
/// Calculates the acceleration towards a mass in m/s /// Calculates the acceleration towards a mass in m/s
pub fn gravitational_acceleration(coords: Vec3, mass: f64) -> Vec3 { pub fn gravitational_acceleration(coords: DVec3, mass: f64) -> DVec3 {
let r_squared = coords.length_squared() as f64; let r_squared = coords.length_squared();
let acceleration_magnitude = G * mass / r_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] #[test]
fn test_gravitational_acceleration() { 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 mass = EARTH_MASS;
let g = gravitational_acceleration(coords, mass); let g = gravitational_acceleration(coords, mass);
let g_rounded = (g * 10.0).round() / 10.0; 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 { pub fn phase_dist_to_coords(phase_radians: f64, distance: f64) -> DVec3 {