Calculate delta time properly, fix low fps collision issues by decreasing max dt

This commit is contained in:
Joshua Barretto 2019-06-25 22:00:26 +01:00
parent 2c24ba7776
commit af432ec510
4 changed files with 16 additions and 10 deletions

View File

@ -38,7 +38,7 @@ pub struct DeltaTime(pub f32);
/// too fast, we'd skip important physics events like collisions. This constant determines the
/// upper limit. If delta time exceeds this value, the game's physics will begin to produce time
/// lag. Ideally, we'd avoid such a situation.
const MAX_DELTA_TIME: f32 = 0.15;
const MAX_DELTA_TIME: f32 = 0.03;
pub struct Changes {
pub new_chunks: HashSet<Vec2<i32>>,
@ -188,6 +188,11 @@ impl State {
self.ecs.read_resource::<Time>().0
}
/// Get the current delta time.
pub fn get_delta_time(&self) -> f32 {
self.ecs.read_resource::<DeltaTime>().0
}
/// Get a reference to this state's terrain.
pub fn terrain(&self) -> Fetch<TerrainMap> {
self.ecs.read_resource::<TerrainMap>()

View File

@ -118,6 +118,7 @@ impl Scene {
Vec3::zero(),
-Vec3::unit_y(),
Rgba::broadcast(1.0),
1.0 / 60.0, // TODO: Use actual deltatime here?
);
}

View File

@ -474,10 +474,9 @@ impl FigureMgr {
let time = client.state().get_time();
let ecs = client.state().ecs();
let view_distance = client.view_distance().unwrap_or(1);
let dt = client.state().get_delta_time();
// Get player position.
let player_pos = client
.state()
.ecs()
let player_pos = ecs
.read_storage::<comp::Pos>()
.get(client.entity())
.map_or(Vec3::zero(), |pos| pos.0);
@ -584,7 +583,7 @@ impl FigureMgr {
&target_skeleton,
client.state().ecs().read_resource::<DeltaTime>().0,
);
state.update(renderer, pos.0, ori.0, col);
state.update(renderer, pos.0, ori.0, col, dt);
}
Body::Quadruped(_) => {
let state = self.quadruped_states.entry(entity).or_insert_with(|| {
@ -616,7 +615,7 @@ impl FigureMgr {
&target_skeleton,
client.state().ecs().read_resource::<DeltaTime>().0,
);
state.update(renderer, pos.0, ori.0, col);
state.update(renderer, pos.0, ori.0, col, dt);
}
Body::QuadrupedMedium(_) => {
let state =
@ -655,7 +654,7 @@ impl FigureMgr {
&target_skeleton,
client.state().ecs().read_resource::<DeltaTime>().0,
);
state.update(renderer, pos.0, ori.0, col);
state.update(renderer, pos.0, ori.0, col, dt);
}
},
// TODO: Non-character actors
@ -764,10 +763,11 @@ impl<S: Skeleton> FigureState<S> {
pos: Vec3<f32>,
ori: Vec3<f32>,
col: Rgba<f32>,
dt: f32,
) {
// Update interpolate pos
self.pos = Lerp::lerp(self.pos, pos, 0.4);
self.ori = Slerp::slerp(self.ori, ori, 0.2);
self.pos = Lerp::lerp(self.pos, pos, (0.4f32).powf(60.0).powf(dt));
self.ori = Slerp::slerp(self.ori, ori, (0.2f32).powf(60.0).powf(dt));
let mat = Mat4::<f32>::identity()
* Mat4::translation_3d(self.pos)

View File

@ -151,7 +151,7 @@ impl PlayState for SessionState {
self.controller.move_dir = unit_vecs.0 * dir_vec[0] + unit_vecs.1 * dir_vec[1];
// Perform an in-game tick.
if let Err(err) = self.tick(clock.get_last_delta()) {
if let Err(err) = self.tick(clock.get_avg_delta()) {
error!("Failed to tick the scene: {:?}", err);
return PlayStateResult::Pop;
}