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::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<RenderInstance> = {
|
||||
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)),
|
||||
|
||||
@ -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)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user