From ba0e668616a83ebb5df05e950498743f5c13c03b Mon Sep 17 00:00:00 2001 From: Verox001 Date: Sun, 4 May 2025 23:14:46 +0200 Subject: [PATCH] Removed Thread Jitter from Simulation Thread Lock --- simulator/src/main.rs | 39 ++++++++++++++++++++--------------- solar_engine/src/simulator.rs | 5 ++--- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 91be791..6eaf0a0 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -2,7 +2,7 @@ use std::cmp::max; use std::collections::HashMap; use std::sync::{Arc, RwLock}; use std::thread; -use std::time::Duration; +use std::time::{Duration, Instant}; use cgmath::num_traits::ToPrimitive; use cgmath::Rotation3; use pollster::FutureExt; @@ -287,7 +287,7 @@ impl<'a> State<'a> { index_count: circle_indices.len() as u32, }); - let mut sim = Simulator::new(43200.0); + let mut sim = Simulator::new(5000.0); sim.add_body(Body { name: "Sun".to_string(), position: [0.0, 0.0], @@ -301,21 +301,7 @@ impl<'a> State<'a> { mass: 5.972e24, }); - let simulator = Arc::new(RwLock::new(sim)); - - let sim_clone = simulator.clone(); - thread::spawn(move || { - loop { - { - let mut sim = sim_clone.write().unwrap(); - sim.step(); - } - thread::sleep(Duration::from_millis(16)); - } - }); - let instances = { - let sim = simulator.read().unwrap(); let sun_pos = sim.bodies[0].position; sim.bodies.iter().enumerate().map(|(i, b)| RenderInstance { position: cgmath::Vector3::new( @@ -341,6 +327,24 @@ impl<'a> State<'a> { usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, }); + let simulator = Arc::new(RwLock::new(sim)); + let simulator_clone = simulator.clone(); + thread::spawn(move || { + let mut last = Instant::now(); + loop { + let now = Instant::now(); + let dt = now.duration_since(last).as_secs_f64(); + last = now; + + { + let mut sim = simulator_clone.write().unwrap(); + sim.step(dt * 50000.0); + } + + thread::sleep(Duration::from_millis(1)); + } + }); + Self { surface, device, @@ -389,8 +393,9 @@ impl<'a> State<'a> { } fn update(&mut self) { + let sim = self.simulator.read().unwrap(); + let updated_instances: Vec = { - let sim = self.simulator.read().unwrap(); sim.bodies.iter().enumerate().map(|(i, b)| RenderInstance { position: cgmath::Vector3::new((b.position[0] / 1.496e11) as f32, (b.position[1] / 1.496e11) as f32, 0.0), rotation: cgmath::Quaternion::from_angle_z(cgmath::Deg(0.0)), diff --git a/solar_engine/src/simulator.rs b/solar_engine/src/simulator.rs index 208af12..2690587 100644 --- a/solar_engine/src/simulator.rs +++ b/solar_engine/src/simulator.rs @@ -29,8 +29,7 @@ impl Simulator { self.bodies.push(body); } - pub fn step(&mut self) { - let dt = self.timestep; + pub fn step(&mut self, dt: f64) { let n = self.bodies.len(); #[derive(Clone)] @@ -87,7 +86,7 @@ impl Simulator { .map(|mutex| mutex.into_inner().unwrap()) .collect() } - + let k1_pos = original_states.iter().map(|s| s.velocity).collect::>(); let k1_vel = compute_accelerations(&original_states, &masses);