Compare commits

...

5 Commits

Author SHA1 Message Date
cc6450975c fix pre-pause time leak 2024-08-02 10:35:05 -07:00
500db0a5b9 warn about time travel 2024-08-02 10:28:29 -07:00
971e83e392 pre-transform timestamps 2024-08-02 10:28:19 -07:00
2d8f677501 bunch of stuff 2024-08-02 10:12:43 -07:00
3b689e58b3 wip 2024-08-02 09:43:12 -07:00
3 changed files with 38 additions and 16 deletions

View File

@ -1655,6 +1655,9 @@ fn atomic_input_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:TimedI
|PhysicsInstruction::Internal(PhysicsInternalInstruction::ReachWalkTargetVelocity)=>(), |PhysicsInstruction::Internal(PhysicsInternalInstruction::ReachWalkTargetVelocity)=>(),
_=>println!("{}|{:?}",ins.time,ins.instruction), _=>println!("{}|{:?}",ins.time,ins.instruction),
} }
if ins.time<state.time{
println!("@@@@ Time travel warning! {:?}",ins);
}
state.time=ins.time; state.time=ins.time;
match ins.instruction{ match ins.instruction{
PhysicsInstruction::Internal(instruction)=>atomic_internal_instruction(state,data,TimedInstruction{time:ins.time,instruction}), PhysicsInstruction::Internal(instruction)=>atomic_internal_instruction(state,data,TimedInstruction{time:ins.time,instruction}),

View File

@ -1,6 +1,8 @@
use crate::physics::{MouseState,PhysicsInputInstruction}; use crate::physics::{MouseState,PhysicsInputInstruction};
use strafesnet_common::integer::Time; use strafesnet_common::integer::Time;
use strafesnet_common::instruction::{TimedInstruction,InstructionConsumer}; use strafesnet_common::instruction::TimedInstruction;
use strafesnet_common::timer::{Scaled,Timer,TimerState};
#[derive(Debug)] #[derive(Debug)]
pub enum InputInstruction{ pub enum InputInstruction{
MoveMouse(glam::IVec2), MoveMouse(glam::IVec2),
@ -22,12 +24,14 @@ pub enum Instruction{
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings), Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
GenerateModels(strafesnet_common::map::CompleteMap), GenerateModels(strafesnet_common::map::CompleteMap),
ClearModels, ClearModels,
SetPaused(bool),
//Graphics(crate::graphics_worker::Instruction), //Graphics(crate::graphics_worker::Instruction),
} }
pub struct MouseInterpolator{ pub struct MouseInterpolator{
timeline:std::collections::VecDeque<TimedInstruction<PhysicsInputInstruction>>, timeline:std::collections::VecDeque<TimedInstruction<PhysicsInputInstruction>>,
last_mouse_time:Time, last_mouse_time:Time,//this value is pre-transformed to simulation time
mouse_blocking:bool, mouse_blocking:bool,
timer:Timer<Scaled>,
} }
impl MouseInterpolator{ impl MouseInterpolator{
fn push_mouse_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>,m:glam::IVec2){ fn push_mouse_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>,m:glam::IVec2){
@ -35,7 +39,7 @@ impl MouseInterpolator{
//tell the game state which is living in the past about its future //tell the game state which is living in the past about its future
self.timeline.push_front(TimedInstruction{ self.timeline.push_front(TimedInstruction{
time:self.last_mouse_time, time:self.last_mouse_time,
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:m}), instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:self.timer.time(ins.time),pos:m}),
}); });
}else{ }else{
//mouse has just started moving again after being still for longer than 10ms. //mouse has just started moving again after being still for longer than 10ms.
@ -44,13 +48,13 @@ impl MouseInterpolator{
time:self.last_mouse_time, time:self.last_mouse_time,
instruction:PhysicsInputInstruction::ReplaceMouse( instruction:PhysicsInputInstruction::ReplaceMouse(
MouseState{time:self.last_mouse_time,pos:physics.get_next_mouse().pos}, MouseState{time:self.last_mouse_time,pos:physics.get_next_mouse().pos},
MouseState{time:ins.time,pos:m} MouseState{time:self.timer.time(ins.time),pos:m}
), ),
}); });
//delay physics execution until we have an interpolation target //delay physics execution until we have an interpolation target
self.mouse_blocking=true; self.mouse_blocking=true;
} }
self.last_mouse_time=ins.time; self.last_mouse_time=self.timer.time(ins.time);
} }
/// returns the mapped physics input instruction /// returns the mapped physics input instruction
/// may or may not mutate internal state XD! /// may or may not mutate internal state XD!
@ -58,7 +62,9 @@ impl MouseInterpolator{
match &ins.instruction{ match &ins.instruction{
Instruction::Input(input_instruction)=>match input_instruction{ Instruction::Input(input_instruction)=>match input_instruction{
&InputInstruction::MoveMouse(m)=>{ &InputInstruction::MoveMouse(m)=>{
self.push_mouse_instruction(physics,ins,m); if !self.timer.is_paused(){
self.push_mouse_instruction(physics,ins,m);
}
None None
}, },
&InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)), &InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)),
@ -73,10 +79,18 @@ impl MouseInterpolator{
InputInstruction::Restart=>Some(PhysicsInputInstruction::Restart), InputInstruction::Restart=>Some(PhysicsInputInstruction::Restart),
InputInstruction::PracticeFly=>Some(PhysicsInputInstruction::PracticeFly), InputInstruction::PracticeFly=>Some(PhysicsInputInstruction::PracticeFly),
}, },
//do these really need to idle the physics?
//sending None dumps the instruction queue
Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle), Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle),
Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle), Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle),
Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle), Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle),
Instruction::Render=>Some(PhysicsInputInstruction::Idle), Instruction::Render=>Some(PhysicsInputInstruction::Idle),
&Instruction::SetPaused(paused)=>{
if let Err(e)=self.timer.set_paused(ins.time,paused){
println!("Cannot pause: {e}");
}
Some(PhysicsInputInstruction::Idle)
},
} }
} }
fn update_mouse_blocking(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->bool{ fn update_mouse_blocking(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->bool{
@ -85,13 +99,13 @@ impl MouseInterpolator{
//shitty mice are 125Hz which is 8ms so this should cover that. //shitty mice are 125Hz which is 8ms so this should cover that.
//setting this to 100us still doesn't print even though it's 10x lower than the polling rate, //setting this to 100us still doesn't print even though it's 10x lower than the polling rate,
//so mouse events are probably not handled separately from drawing and fire right before it :( //so mouse events are probably not handled separately from drawing and fire right before it :(
if Time::from_millis(10)<ins.time-physics.get_next_mouse().time{ if Time::from_millis(10)<self.timer.time(ins.time)-physics.get_next_mouse().time{
//push an event to extrapolate no movement from //push an event to extrapolate no movement from
self.timeline.push_front(TimedInstruction{ self.timeline.push_front(TimedInstruction{
time:self.last_mouse_time, time:self.last_mouse_time,
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:physics.get_next_mouse().pos}), instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:self.timer.time(ins.time),pos:physics.get_next_mouse().pos}),
}); });
self.last_mouse_time=ins.time; self.last_mouse_time=self.timer.time(ins.time);
//stop blocking. the mouse is not moving so the physics does not need to live in the past and wait for interpolation targets. //stop blocking. the mouse is not moving so the physics does not need to live in the past and wait for interpolation targets.
self.mouse_blocking=false; self.mouse_blocking=false;
true true
@ -100,8 +114,8 @@ impl MouseInterpolator{
} }
}else{ }else{
//keep this up to date so that it can be used as a known-timestamp //keep this up to date so that it can be used as a known-timestamp
//that the mouse was not moving when the mouse starts moving again //that the mouse was Timer<Scaled>not moving when the mouse starts moving again
self.last_mouse_time=ins.time; self.last_mouse_time=self.timer.time(ins.time);
true true
} }
} }
@ -110,7 +124,7 @@ impl MouseInterpolator{
if let Some(phys_input)=phys_input_option{ if let Some(phys_input)=phys_input_option{
//non-mouse event //non-mouse event
self.timeline.push_back(TimedInstruction{ self.timeline.push_back(TimedInstruction{
time:ins.time, time:self.timer.time(ins.time),
instruction:phys_input, instruction:phys_input,
}); });
@ -122,8 +136,8 @@ impl MouseInterpolator{
} }
} }
fn empty_queue(&mut self,physics:&mut crate::physics::PhysicsContext){ fn empty_queue(&mut self,physics:&mut crate::physics::PhysicsContext){
while let Some(instruction)=self.timeline.pop_front(){ while let Some(ins)=self.timeline.pop_front(){
physics.run_input_instruction(instruction); physics.run_input_instruction(ins);
} }
} }
fn handle_instruction(&mut self,physics:&mut crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>){ fn handle_instruction(&mut self,physics:&mut crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>){
@ -140,12 +154,13 @@ pub fn new(mut physics:crate::physics::PhysicsContext,mut graphics_worker:crate:
mouse_blocking:true, mouse_blocking:true,
last_mouse_time:physics.get_next_mouse().time, last_mouse_time:physics.get_next_mouse().time,
timeline:std::collections::VecDeque::new(), timeline:std::collections::VecDeque::new(),
timer:Timer::from_state(Scaled::identity(),false),
}; };
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{ crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
interpolator.handle_instruction(&mut physics,&ins); interpolator.handle_instruction(&mut physics,&ins);
match ins.instruction{ match ins.instruction{
Instruction::Render=>{ Instruction::Render=>{
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.get_next_mouse().pos)).unwrap(); graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),interpolator.timer.time(ins.time),physics.get_next_mouse().pos)).unwrap();
}, },
Instruction::Resize(size,user_settings)=>{ Instruction::Resize(size,user_settings)=>{
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap(); graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap();

View File

@ -35,8 +35,12 @@ impl WindowContext<'_>{
Err(e)=>println!("Failed to load map: {e}"), Err(e)=>println!("Failed to load map: {e}"),
} }
}, },
winit::event::WindowEvent::Focused(_state)=>{ winit::event::WindowEvent::Focused(state)=>{
//pause unpause //pause unpause
self.physics_thread.send(TimedInstruction{
time,
instruction:crate::physics_worker::Instruction::SetPaused(!state),
}).unwrap();
//recalculate pressed keys on focus //recalculate pressed keys on focus
}, },
winit::event::WindowEvent::KeyboardInput{ winit::event::WindowEvent::KeyboardInput{