From adf53b77039107c34d6a350996ac2e6d915708d6 Mon Sep 17 00:00:00 2001 From: Verox001 Date: Tue, 6 May 2025 18:40:32 +0200 Subject: [PATCH] Added 3D possibility to body --- simulator/src/main.rs | 8 ++++---- solar_engine/src/body.rs | 8 +++++--- solar_engine/src/simulator.rs | 15 +++++++-------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index e6b9afb..ca22bbe 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -9,14 +9,14 @@ pub async fn run() { let mut sim = simulator.write().unwrap(); sim.add_body(Body { name: "Sun".into(), - position: [0.0, 0.0], - velocity: [0.0, 0.0], + position: Vector3::new(0.0, 0.0, 0.0), + velocity: Vector3::new(0.0, 0.0, 0.0), mass: 1.989e30, }); sim.add_body(Body { name: "Earth".into(), - position: [1.496e11, 0.0], - velocity: [0.0, 29780.0], + position: Vector3::new(1.496e11, 0.0, 0.0), + velocity: Vector3::new(0.0, 29780.0, 0.0), mass: 5.972e24, }); } diff --git a/solar_engine/src/body.rs b/solar_engine/src/body.rs index 5de5a52..5d1c23c 100644 --- a/solar_engine/src/body.rs +++ b/solar_engine/src/body.rs @@ -1,13 +1,15 @@ +use cgmath::Vector3; + #[derive(Debug)] pub struct Body { pub name: String, pub mass: f64, - pub position: [f64; 2], - pub velocity: [f64; 2], + pub position: Vector3, + pub velocity: Vector3, } impl Body { - pub fn new(name: &str, mass: f64, position: [f64; 2], velocity: [f64; 2]) -> Self { + pub fn new(name: &str, mass: f64, position: Vector3, velocity: Vector3) -> Self { Self { name: name.to_string(), mass, diff --git a/solar_engine/src/simulator.rs b/solar_engine/src/simulator.rs index 7e7a2eb..2fe54f1 100644 --- a/solar_engine/src/simulator.rs +++ b/solar_engine/src/simulator.rs @@ -1,4 +1,5 @@ use std::sync::Mutex; +use cgmath::Vector3; use crate::body::Body; use rayon::prelude::*; @@ -36,8 +37,8 @@ impl Simulator { #[derive(Clone)] struct State { - position: [f64; 2], - velocity: [f64; 2], + position: Vector3, + velocity: Vector3, } let original_states: Vec = self @@ -96,14 +97,12 @@ impl Simulator { .iter() .enumerate() .map(|(i, s)| State { - position: [ - s.position[0] + k1_pos[i][0] * dt / 2.0, - s.position[1] + k1_pos[i][1] * dt / 2.0, - ], - velocity: [ + position: Vector3::new(s.position[0], s.position[1], 0.0), + velocity: Vector3::new( s.velocity[0] + k1_vel[i][0] * dt / 2.0, s.velocity[1] + k1_vel[i][1] * dt / 2.0, - ], + 0.0, + ), }) .collect::>();