Fixed timesteps for windsim

This commit is contained in:
Ludvig Böklin
2021-06-03 14:01:21 +02:00
parent 788d0e7e13
commit bd76bf823a
2 changed files with 28 additions and 25 deletions

View File

@ -1,5 +1,5 @@
#![allow(dead_code)]
use crate::windsim::{WindSim, MS_BETWEEN_TICKS};
use crate::windsim::WindSim;
use common::{
comp::{Fluid, PhysicsState, Pos, Vel},
resources::DeltaTime,
@ -43,19 +43,14 @@ impl<'a> System<'a> for Sys {
z: inline_tweak::tweak!(200.0),
}),
Vel(Vec3 {
x: inline_tweak::tweak!(300.0) * r.sin(),
y: inline_tweak::tweak!(300.0) * r.cos(),
z: inline_tweak::tweak!(10.0) * rng.gen_range(-0.25..0.25),
x: inline_tweak::tweak!(60.0) * r.sin(),
y: inline_tweak::tweak!(60.0) * r.cos(),
z: inline_tweak::tweak!(0.0) * rng.gen_range(-0.25..0.25),
}),
)
})
.collect::<Vec<_>>();
// If MS_BETWEEN_TICKS is 1000 it runs the sim once per second
if windsim.ms_since_update >= MS_BETWEEN_TICKS {
windsim.tick(wind_sources, &DeltaTime((MS_BETWEEN_TICKS / 1000) as f32));
} else {
windsim.ms_since_update += (dt.0 * 1000.0) as u32;
}
windsim.tick(wind_sources, &dt);
for (pos, physics_state) in (&positions, &mut physics_states).join() {
physics_state.in_fluid = physics_state

View File

@ -18,13 +18,13 @@ pub const GRID_SIZE: Vec3<usize> = Vec3 {
y: Y_SIZE,
z: Z_SIZE,
};
pub const MS_BETWEEN_TICKS: u32 = 1000;
const TPS: u32 = 5;
#[derive(Default)]
pub struct WindSim {
grid: WindGrid,
blocks_per_cell: Vec3<u32>,
pub ms_since_update: u32,
timer: f32,
}
impl WindSim {
@ -32,7 +32,7 @@ impl WindSim {
Self {
grid: WindGrid::default(),
blocks_per_cell: cell_size_in_blocks(world_size),
ms_since_update: MS_BETWEEN_TICKS,
timer: std::f32::INFINITY,
}
}
@ -66,19 +66,27 @@ impl WindSim {
// Abstraction for running the simulation
pub fn tick(&mut self, sources: Vec<(Pos, Vel)>, dt: &DeltaTime) {
for (pos, vel) in sources {
self.add_velocity_source(pos, vel);
self.timer += dt.0;
let step = 1.0 / TPS as f32;
if self.timer >= step {
for (pos, vel) in sources {
self.add_velocity_source(pos, vel);
}
step_fluid(
&mut self.grid.density,
&mut self.grid.x_vel,
&mut self.grid.y_vel,
&mut self.grid.z_vel,
step,
0.1,
true,
);
if self.timer.is_infinite() {
self.timer = 0.0;
} else {
self.timer -= step;
}
}
step_fluid(
&mut self.grid.density,
&mut self.grid.x_vel,
&mut self.grid.y_vel,
&mut self.grid.z_vel,
dt.0,
0.1,
true,
);
self.ms_since_update = 0;
}
}