mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Calculate delta time properly, fix low fps collision issues by decreasing max dt
This commit is contained in:
parent
2c24ba7776
commit
af432ec510
@ -38,7 +38,7 @@ pub struct DeltaTime(pub f32);
|
|||||||
/// too fast, we'd skip important physics events like collisions. This constant determines the
|
/// 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
|
/// 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.
|
/// 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 struct Changes {
|
||||||
pub new_chunks: HashSet<Vec2<i32>>,
|
pub new_chunks: HashSet<Vec2<i32>>,
|
||||||
@ -188,6 +188,11 @@ impl State {
|
|||||||
self.ecs.read_resource::<Time>().0
|
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.
|
/// Get a reference to this state's terrain.
|
||||||
pub fn terrain(&self) -> Fetch<TerrainMap> {
|
pub fn terrain(&self) -> Fetch<TerrainMap> {
|
||||||
self.ecs.read_resource::<TerrainMap>()
|
self.ecs.read_resource::<TerrainMap>()
|
||||||
|
@ -118,6 +118,7 @@ impl Scene {
|
|||||||
Vec3::zero(),
|
Vec3::zero(),
|
||||||
-Vec3::unit_y(),
|
-Vec3::unit_y(),
|
||||||
Rgba::broadcast(1.0),
|
Rgba::broadcast(1.0),
|
||||||
|
1.0 / 60.0, // TODO: Use actual deltatime here?
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -474,10 +474,9 @@ impl FigureMgr {
|
|||||||
let time = client.state().get_time();
|
let time = client.state().get_time();
|
||||||
let ecs = client.state().ecs();
|
let ecs = client.state().ecs();
|
||||||
let view_distance = client.view_distance().unwrap_or(1);
|
let view_distance = client.view_distance().unwrap_or(1);
|
||||||
|
let dt = client.state().get_delta_time();
|
||||||
// Get player position.
|
// Get player position.
|
||||||
let player_pos = client
|
let player_pos = ecs
|
||||||
.state()
|
|
||||||
.ecs()
|
|
||||||
.read_storage::<comp::Pos>()
|
.read_storage::<comp::Pos>()
|
||||||
.get(client.entity())
|
.get(client.entity())
|
||||||
.map_or(Vec3::zero(), |pos| pos.0);
|
.map_or(Vec3::zero(), |pos| pos.0);
|
||||||
@ -584,7 +583,7 @@ impl FigureMgr {
|
|||||||
&target_skeleton,
|
&target_skeleton,
|
||||||
client.state().ecs().read_resource::<DeltaTime>().0,
|
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(_) => {
|
Body::Quadruped(_) => {
|
||||||
let state = self.quadruped_states.entry(entity).or_insert_with(|| {
|
let state = self.quadruped_states.entry(entity).or_insert_with(|| {
|
||||||
@ -616,7 +615,7 @@ impl FigureMgr {
|
|||||||
&target_skeleton,
|
&target_skeleton,
|
||||||
client.state().ecs().read_resource::<DeltaTime>().0,
|
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(_) => {
|
Body::QuadrupedMedium(_) => {
|
||||||
let state =
|
let state =
|
||||||
@ -655,7 +654,7 @@ impl FigureMgr {
|
|||||||
&target_skeleton,
|
&target_skeleton,
|
||||||
client.state().ecs().read_resource::<DeltaTime>().0,
|
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
|
// TODO: Non-character actors
|
||||||
@ -764,10 +763,11 @@ impl<S: Skeleton> FigureState<S> {
|
|||||||
pos: Vec3<f32>,
|
pos: Vec3<f32>,
|
||||||
ori: Vec3<f32>,
|
ori: Vec3<f32>,
|
||||||
col: Rgba<f32>,
|
col: Rgba<f32>,
|
||||||
|
dt: f32,
|
||||||
) {
|
) {
|
||||||
// Update interpolate pos
|
// Update interpolate pos
|
||||||
self.pos = Lerp::lerp(self.pos, pos, 0.4);
|
self.pos = Lerp::lerp(self.pos, pos, (0.4f32).powf(60.0).powf(dt));
|
||||||
self.ori = Slerp::slerp(self.ori, ori, 0.2);
|
self.ori = Slerp::slerp(self.ori, ori, (0.2f32).powf(60.0).powf(dt));
|
||||||
|
|
||||||
let mat = Mat4::<f32>::identity()
|
let mat = Mat4::<f32>::identity()
|
||||||
* Mat4::translation_3d(self.pos)
|
* Mat4::translation_3d(self.pos)
|
||||||
|
@ -151,7 +151,7 @@ impl PlayState for SessionState {
|
|||||||
self.controller.move_dir = unit_vecs.0 * dir_vec[0] + unit_vecs.1 * dir_vec[1];
|
self.controller.move_dir = unit_vecs.0 * dir_vec[0] + unit_vecs.1 * dir_vec[1];
|
||||||
|
|
||||||
// Perform an in-game tick.
|
// 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);
|
error!("Failed to tick the scene: {:?}", err);
|
||||||
return PlayStateResult::Pop;
|
return PlayStateResult::Pop;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user