Removed Thread Jitter from Simulation Thread Lock
This commit is contained in:
parent
5cd61b6086
commit
ba0e668616
@ -2,7 +2,7 @@ use std::cmp::max;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::{Duration, Instant};
|
||||||
use cgmath::num_traits::ToPrimitive;
|
use cgmath::num_traits::ToPrimitive;
|
||||||
use cgmath::Rotation3;
|
use cgmath::Rotation3;
|
||||||
use pollster::FutureExt;
|
use pollster::FutureExt;
|
||||||
@ -287,7 +287,7 @@ impl<'a> State<'a> {
|
|||||||
index_count: circle_indices.len() as u32,
|
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 {
|
sim.add_body(Body {
|
||||||
name: "Sun".to_string(),
|
name: "Sun".to_string(),
|
||||||
position: [0.0, 0.0],
|
position: [0.0, 0.0],
|
||||||
@ -301,21 +301,7 @@ impl<'a> State<'a> {
|
|||||||
mass: 5.972e24,
|
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 instances = {
|
||||||
let sim = simulator.read().unwrap();
|
|
||||||
let sun_pos = sim.bodies[0].position;
|
let sun_pos = sim.bodies[0].position;
|
||||||
sim.bodies.iter().enumerate().map(|(i, b)| RenderInstance {
|
sim.bodies.iter().enumerate().map(|(i, b)| RenderInstance {
|
||||||
position: cgmath::Vector3::new(
|
position: cgmath::Vector3::new(
|
||||||
@ -341,6 +327,24 @@ impl<'a> State<'a> {
|
|||||||
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
|
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 {
|
Self {
|
||||||
surface,
|
surface,
|
||||||
device,
|
device,
|
||||||
@ -389,8 +393,9 @@ impl<'a> State<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self) {
|
fn update(&mut self) {
|
||||||
|
let sim = self.simulator.read().unwrap();
|
||||||
|
|
||||||
let updated_instances: Vec<RenderInstance> = {
|
let updated_instances: Vec<RenderInstance> = {
|
||||||
let sim = self.simulator.read().unwrap();
|
|
||||||
sim.bodies.iter().enumerate().map(|(i, b)| RenderInstance {
|
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),
|
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)),
|
rotation: cgmath::Quaternion::from_angle_z(cgmath::Deg(0.0)),
|
||||||
|
|||||||
@ -29,8 +29,7 @@ impl Simulator {
|
|||||||
self.bodies.push(body);
|
self.bodies.push(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn step(&mut self) {
|
pub fn step(&mut self, dt: f64) {
|
||||||
let dt = self.timestep;
|
|
||||||
let n = self.bodies.len();
|
let n = self.bodies.len();
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -87,7 +86,7 @@ impl Simulator {
|
|||||||
.map(|mutex| mutex.into_inner().unwrap())
|
.map(|mutex| mutex.into_inner().unwrap())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
let k1_pos = original_states.iter().map(|s| s.velocity).collect::<Vec<_>>();
|
let k1_pos = original_states.iter().map(|s| s.velocity).collect::<Vec<_>>();
|
||||||
let k1_vel = compute_accelerations(&original_states, &masses);
|
let k1_vel = compute_accelerations(&original_states, &masses);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user