Added timewarp to Simulator
This commit is contained in:
parent
ba0e668616
commit
de8dca3536
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
|||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use cgmath::num_traits::ToPrimitive;
|
use cgmath::num_traits::{pow, ToPrimitive};
|
||||||
use cgmath::Rotation3;
|
use cgmath::Rotation3;
|
||||||
use pollster::FutureExt;
|
use pollster::FutureExt;
|
||||||
use wgpu::util::DeviceExt;
|
use wgpu::util::DeviceExt;
|
||||||
@ -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(5000.0);
|
let mut sim = Simulator::new();
|
||||||
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],
|
||||||
@ -338,7 +338,8 @@ impl<'a> State<'a> {
|
|||||||
|
|
||||||
{
|
{
|
||||||
let mut sim = simulator_clone.write().unwrap();
|
let mut sim = simulator_clone.write().unwrap();
|
||||||
sim.step(dt * 50000.0);
|
let timewarp = sim.get_timewarp();
|
||||||
|
sim.step(dt * timewarp as f64);
|
||||||
}
|
}
|
||||||
|
|
||||||
thread::sleep(Duration::from_millis(1));
|
thread::sleep(Duration::from_millis(1));
|
||||||
@ -371,15 +372,21 @@ impl<'a> State<'a> {
|
|||||||
if event.state == ElementState::Pressed {
|
if event.state == ElementState::Pressed {
|
||||||
return match event.key_without_modifiers().as_ref() {
|
return match event.key_without_modifiers().as_ref() {
|
||||||
Key::Character(".") => {
|
Key::Character(".") => {
|
||||||
/*let mut sim = self.simulator.write().unwrap();
|
let mut sim = self.simulator.write().unwrap();
|
||||||
sim.increase_timewarp();
|
sim.increase_timewarp();
|
||||||
println!("Timewarp: {}", sim.get_timewarp());*/
|
println!("Timewarp: {}", sim.get_timewarp());
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Key::Character(",") => {
|
Key::Character(",") => {
|
||||||
/*let mut sim = self.simulator.write().unwrap();
|
let mut sim = self.simulator.write().unwrap();
|
||||||
sim.decrease_timewarp();
|
sim.decrease_timewarp();
|
||||||
println!("Timewarp: {}", sim.get_timewarp());*/
|
println!("Timewarp: {}", sim.get_timewarp());
|
||||||
|
true
|
||||||
|
}
|
||||||
|
Key::Character("-") => {
|
||||||
|
let mut sim = self.simulator.write().unwrap();
|
||||||
|
sim.reset_timewarp();
|
||||||
|
println!("Timewarp: {}", sim.get_timewarp());
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
use std::sync::atomic::AtomicUsize;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use crate::body::Body;
|
use crate::body::Body;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
@ -7,7 +8,7 @@ const G: f64 = 6.67430e-11;
|
|||||||
pub struct Simulator {
|
pub struct Simulator {
|
||||||
pub bodies: Vec<Body>,
|
pub bodies: Vec<Body>,
|
||||||
pub time: f64,
|
pub time: f64,
|
||||||
pub timestep: f64,
|
timewarp: AtomicUsize
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn distance_squared(a: [f64; 2], b: [f64; 2]) -> f64 {
|
pub fn distance_squared(a: [f64; 2], b: [f64; 2]) -> f64 {
|
||||||
@ -17,11 +18,11 @@ pub fn distance_squared(a: [f64; 2], b: [f64; 2]) -> f64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Simulator {
|
impl Simulator {
|
||||||
pub fn new(timestep: f64) -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
bodies: Vec::new(),
|
bodies: Vec::new(),
|
||||||
time: 0.0,
|
time: 0.0,
|
||||||
timestep,
|
timewarp: AtomicUsize::new(1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,4 +145,24 @@ impl Simulator {
|
|||||||
|
|
||||||
self.time += dt;
|
self.time += dt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn increase_timewarp(&self) {
|
||||||
|
let current_timewarp = self.timewarp.load(std::sync::atomic::Ordering::SeqCst);
|
||||||
|
self.timewarp.store(current_timewarp * 2, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decrease_timewarp(&self) {
|
||||||
|
let current_timewarp = self.timewarp.load(std::sync::atomic::Ordering::SeqCst);
|
||||||
|
if current_timewarp > 1 {
|
||||||
|
self.timewarp.store(current_timewarp / 2, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset_timewarp(&self) {
|
||||||
|
self.timewarp.store(1, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_timewarp(&self) -> usize {
|
||||||
|
self.timewarp.load(std::sync::atomic::Ordering::SeqCst)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user