Added 3D possibility to body

This commit is contained in:
Verox001 2025-05-06 18:40:32 +02:00
parent 966aabc649
commit adf53b7703
3 changed files with 16 additions and 15 deletions

View File

@ -9,14 +9,14 @@ pub async fn run() {
let mut sim = simulator.write().unwrap(); let mut sim = simulator.write().unwrap();
sim.add_body(Body { sim.add_body(Body {
name: "Sun".into(), name: "Sun".into(),
position: [0.0, 0.0], position: Vector3::new(0.0, 0.0, 0.0),
velocity: [0.0, 0.0], velocity: Vector3::new(0.0, 0.0, 0.0),
mass: 1.989e30, mass: 1.989e30,
}); });
sim.add_body(Body { sim.add_body(Body {
name: "Earth".into(), name: "Earth".into(),
position: [1.496e11, 0.0], position: Vector3::new(1.496e11, 0.0, 0.0),
velocity: [0.0, 29780.0], velocity: Vector3::new(0.0, 29780.0, 0.0),
mass: 5.972e24, mass: 5.972e24,
}); });
} }

View File

@ -1,13 +1,15 @@
use cgmath::Vector3;
#[derive(Debug)] #[derive(Debug)]
pub struct Body { pub struct Body {
pub name: String, pub name: String,
pub mass: f64, pub mass: f64,
pub position: [f64; 2], pub position: Vector3<f64>,
pub velocity: [f64; 2], pub velocity: Vector3<f64>,
} }
impl Body { 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<f64>, velocity: Vector3<f64>) -> Self {
Self { Self {
name: name.to_string(), name: name.to_string(),
mass, mass,

View File

@ -1,4 +1,5 @@
use std::sync::Mutex; use std::sync::Mutex;
use cgmath::Vector3;
use crate::body::Body; use crate::body::Body;
use rayon::prelude::*; use rayon::prelude::*;
@ -36,8 +37,8 @@ impl Simulator {
#[derive(Clone)] #[derive(Clone)]
struct State { struct State {
position: [f64; 2], position: Vector3<f64>,
velocity: [f64; 2], velocity: Vector3<f64>,
} }
let original_states: Vec<State> = self let original_states: Vec<State> = self
@ -96,14 +97,12 @@ impl Simulator {
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, s)| State { .map(|(i, s)| State {
position: [ position: Vector3::new(s.position[0], s.position[1], 0.0),
s.position[0] + k1_pos[i][0] * dt / 2.0, velocity: Vector3::new(
s.position[1] + k1_pos[i][1] * dt / 2.0,
],
velocity: [
s.velocity[0] + k1_vel[i][0] * dt / 2.0, s.velocity[0] + k1_vel[i][0] * dt / 2.0,
s.velocity[1] + k1_vel[i][1] * dt / 2.0, s.velocity[1] + k1_vel[i][1] * dt / 2.0,
], 0.0,
),
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();