mouse interpolator abstraction

This commit is contained in:
Quaternions 2024-07-30 11:52:36 -07:00
parent 171cd8f2e4
commit 38df41e043

View File

@ -23,109 +23,141 @@ pub enum Instruction{
ClearModels, ClearModels,
//Graphics(crate::graphics_worker::Instruction), //Graphics(crate::graphics_worker::Instruction),
} }
pub struct MouseInterpolator{
pub fn new(mut physics:crate::physics::PhysicsContext,mut graphics_worker:crate::compat_worker::INWorker<crate::graphics_worker::Instruction>)->crate::compat_worker::QNWorker<TimedInstruction<Instruction>>{ timeline:std::collections::VecDeque<TimedInstruction<PhysicsInputInstruction>>,
let mut mouse_blocking=true; last_mouse_time:Time,
let mut last_mouse_time=physics.get_next_mouse().time; mouse_blocking:bool,
let mut timeline=std::collections::VecDeque::new(); }
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{ impl MouseInterpolator{
if if let Some(phys_input)=match &ins.instruction{ fn push_mouse_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>,m:glam::IVec2){
Instruction::Input(input_instruction)=>match input_instruction{ if self.mouse_blocking{
&InputInstruction::MoveMouse(m)=>{ //tell the game state which is living in the past about its future
if mouse_blocking{ self.timeline.push_front(TimedInstruction{
//tell the game state which is living in the past about its future time:self.last_mouse_time,
timeline.push_front(TimedInstruction{ instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:m}),
time:last_mouse_time, });
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:m}), }else{
}); //mouse has just started moving again after being still for longer than 10ms.
}else{ //replace the entire mouse interpolation state to avoid an intermediate state with identical m0.t m1.t timestamps which will divide by zero
//mouse has just started moving again after being still for longer than 10ms. self.timeline.push_front(TimedInstruction{
//replace the entire mouse interpolation state to avoid an intermediate state with identical m0.t m1.t timestamps which will divide by zero time:self.last_mouse_time,
timeline.push_front(TimedInstruction{ instruction:PhysicsInputInstruction::ReplaceMouse(
time:last_mouse_time, MouseState{time:self.last_mouse_time,pos:physics.get_next_mouse().pos},
instruction:PhysicsInputInstruction::ReplaceMouse( MouseState{time:ins.time,pos:m}
MouseState{time:last_mouse_time,pos:physics.get_next_mouse().pos}, ),
MouseState{time:ins.time,pos:m} });
), //delay physics execution until we have an interpolation target
}); self.mouse_blocking=true;
//delay physics execution until we have an interpolation target }
mouse_blocking=true; self.last_mouse_time=ins.time;
} }
last_mouse_time=ins.time; /// returns the mapped physics input instruction
None /// may or may not mutate internal state XD!
}, fn map_instruction(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->Option<PhysicsInputInstruction>{
&InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)), match &ins.instruction{
&InputInstruction::MoveLeft(s)=>Some(PhysicsInputInstruction::SetMoveLeft(s)), Instruction::Input(input_instruction)=>match input_instruction{
&InputInstruction::MoveBack(s)=>Some(PhysicsInputInstruction::SetMoveBack(s)), &InputInstruction::MoveMouse(m)=>{
&InputInstruction::MoveRight(s)=>Some(PhysicsInputInstruction::SetMoveRight(s)), self.push_mouse_instruction(physics,ins,m);
&InputInstruction::MoveUp(s)=>Some(PhysicsInputInstruction::SetMoveUp(s)), None
&InputInstruction::MoveDown(s)=>Some(PhysicsInputInstruction::SetMoveDown(s)),
&InputInstruction::Jump(s)=>Some(PhysicsInputInstruction::SetJump(s)),
&InputInstruction::Zoom(s)=>Some(PhysicsInputInstruction::SetZoom(s)),
InputInstruction::Reset=>Some(PhysicsInputInstruction::Reset),
InputInstruction::PracticeFly=>Some(PhysicsInputInstruction::PracticeFly),
}, },
Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle), &InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)),
Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle), &InputInstruction::MoveLeft(s)=>Some(PhysicsInputInstruction::SetMoveLeft(s)),
Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle), &InputInstruction::MoveBack(s)=>Some(PhysicsInputInstruction::SetMoveBack(s)),
Instruction::Render=>Some(PhysicsInputInstruction::Idle), &InputInstruction::MoveRight(s)=>Some(PhysicsInputInstruction::SetMoveRight(s)),
}{ &InputInstruction::MoveUp(s)=>Some(PhysicsInputInstruction::SetMoveUp(s)),
//non-mouse event &InputInstruction::MoveDown(s)=>Some(PhysicsInputInstruction::SetMoveDown(s)),
timeline.push_back(TimedInstruction{ &InputInstruction::Jump(s)=>Some(PhysicsInputInstruction::SetJump(s)),
time:ins.time, &InputInstruction::Zoom(s)=>Some(PhysicsInputInstruction::SetZoom(s)),
instruction:phys_input, InputInstruction::Reset=>Some(PhysicsInputInstruction::Reset),
InputInstruction::PracticeFly=>Some(PhysicsInputInstruction::PracticeFly),
},
Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle),
Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle),
Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle),
Instruction::Render=>Some(PhysicsInputInstruction::Idle),
}
}
fn update_mouse_blocking(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>)->bool{
if self.mouse_blocking{
//assume the mouse has stopped moving after 10ms.
//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,
//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{
//push an event to extrapolate no movement from
self.timeline.push_front(TimedInstruction{
time:self.last_mouse_time,
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:physics.get_next_mouse().pos}),
}); });
self.last_mouse_time=ins.time;
if mouse_blocking{ //stop blocking. the mouse is not moving so the physics does not need to live in the past and wait for interpolation targets.
//assume the mouse has stopped moving after 10ms. self.mouse_blocking=false;
//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,
//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{
//push an event to extrapolate no movement from
timeline.push_front(TimedInstruction{
time:last_mouse_time,
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:physics.get_next_mouse().pos}),
});
last_mouse_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.
mouse_blocking=false;
true
}else{
false
}
}else{
//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
last_mouse_time=ins.time;
true
}
}else{
//mouse event
true true
}{ }else{
//empty queue false
while let Some(instruction)=timeline.pop_front(){
physics.run_input_instruction(instruction);
}
} }
match ins.instruction{ }else{
Instruction::Render=>{ //keep this up to date so that it can be used as a known-timestamp
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.get_next_mouse().pos)).unwrap(); //that the mouse was not moving when the mouse starts moving again
}, self.last_mouse_time=ins.time;
Instruction::Resize(size,user_settings)=>{ true
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap(); }
}, }
Instruction::GenerateModels(map)=>{ /// returns whether or not to empty the instruction queue
physics.generate_models(&map); fn handle_physics_input(&mut self,physics:&crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>,phys_input_option:Option<PhysicsInputInstruction>)->bool{
physics.spawn(); if let Some(phys_input)=phys_input_option{
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(map)).unwrap(); //non-mouse event
}, self.timeline.push_back(TimedInstruction{
Instruction::ClearModels=>{ time:ins.time,
physics.clear(); instruction:phys_input,
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap(); });
},
_=>(), //this returns the bool for us
} self.update_mouse_blocking(physics,ins)
}) }else{
} //mouse event
true
}
}
fn empty_queue(&mut self,physics:&mut crate::physics::PhysicsContext){
while let Some(instruction)=self.timeline.pop_front(){
physics.run_input_instruction(instruction);
}
}
fn handle_instruction(&mut self,physics:&mut crate::physics::PhysicsContext,ins:&TimedInstruction<Instruction>){
let physics_input_option=self.map_instruction(physics,ins);
let should_empty_queue=self.handle_physics_input(physics,ins,physics_input_option);
if should_empty_queue{
self.empty_queue(physics);
}
}
}
pub fn new(mut physics:crate::physics::PhysicsContext,mut graphics_worker:crate::compat_worker::INWorker<crate::graphics_worker::Instruction>)->crate::compat_worker::QNWorker<TimedInstruction<Instruction>>{
let mut interpolator=MouseInterpolator{
mouse_blocking:true,
last_mouse_time:physics.get_next_mouse().time,
timeline:std::collections::VecDeque::new(),
};
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
interpolator.handle_instruction(&mut physics,&ins);
match ins.instruction{
Instruction::Render=>{
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.get_next_mouse().pos)).unwrap();
},
Instruction::Resize(size,user_settings)=>{
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap();
},
Instruction::GenerateModels(map)=>{
physics.generate_models(&map);
physics.spawn();
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(map)).unwrap();
},
Instruction::ClearModels=>{
physics.clear();
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap();
},
_=>(),
}
})
}