Compare commits

...

2 Commits

Author SHA1 Message Date
f07d9f968e Print Only Horizontal Velocity
The Speed function is unnecessary I forgot to remove it
2024-02-11 22:26:34 +00:00
2cef79da1d Something idk 2024-02-11 22:24:40 +00:00
2 changed files with 2073 additions and 2023 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,133 +1,165 @@
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,InstructionConsumer};
#[derive(Debug)] use strafesnet_common::integer::{self,Planar64,Planar64Vec3,Planar64Mat3,Angle32,Ratio64,Ratio64Vec2};
pub enum InputInstruction {
MoveMouse(glam::IVec2), #[derive(Debug)]
MoveRight(bool), pub enum InputInstruction {
MoveUp(bool), MoveMouse(glam::IVec2),
MoveBack(bool), MoveRight(bool),
MoveLeft(bool), MoveUp(bool),
MoveDown(bool), MoveBack(bool),
MoveForward(bool), MoveLeft(bool),
Jump(bool), MoveDown(bool),
Zoom(bool), MoveForward(bool),
Reset, Jump(bool),
} Zoom(bool),
pub enum Instruction{ Reset,
Input(InputInstruction), }
Render, pub enum Instruction{
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings), Input(InputInstruction),
GenerateModels(crate::model::IndexedModelInstances), Render,
ClearModels, Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings),
//Graphics(crate::graphics_worker::Instruction), GenerateModels(crate::model::IndexedModelInstances),
} ClearModels,
//Graphics(crate::graphics_worker::Instruction),
pub fn new(mut physics:crate::physics::PhysicsState,mut graphics_worker:crate::compat_worker::INWorker<crate::graphics_worker::Instruction>)->crate::compat_worker::QNWorker<TimedInstruction<Instruction>>{ }
let mut mouse_blocking=true;
let mut last_mouse_time=physics.next_mouse.time; pub struct Speed{
let mut timeline=std::collections::VecDeque::new(); pub player_vel:Planar64Vec3,
crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{ pub time:Time
if if let Some(phys_input)=match &ins.instruction{ }
Instruction::Input(input_instruction)=>match input_instruction{
&InputInstruction::MoveMouse(m)=>{ impl std::ops::Neg for Speed{
if mouse_blocking{ type Output=Self;
//tell the game state which is living in the past about its future fn neg(self)->Self::Output{
timeline.push_front(TimedInstruction{ Self{
time:last_mouse_time, player_vel:self.player_vel,
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:m}), time:self.time
}); }
}else{ }
//mouse has just started moving again after being still for longer than 10ms. }
//replace the entire mouse interpolation state to avoid an intermediate state with identical m0.t m1.t timestamps which will divide by zero
timeline.push_front(TimedInstruction{ impl Speed{
time:last_mouse_time, pub fn new(player_vel:Planar64Vec3,time:Time)->Self{
instruction:PhysicsInputInstruction::ReplaceMouse( Self{
MouseState{time:last_mouse_time,pos:physics.next_mouse.pos}, player_vel,
MouseState{time:ins.time,pos:m} time,
), }
}); }
//delay physics execution until we have an interpolation target }
mouse_blocking=true; pub fn new(mut physics:crate::physics::PhysicsState,mut graphics_worker:crate::compat_worker::INWorker<crate::graphics_worker::Instruction>)->crate::compat_worker::QNWorker<TimedInstruction<Instruction>>{
} let mut mouse_blocking=true;
last_mouse_time=ins.time; let mut last_mouse_time=physics.next_mouse.time;
None let mut timeline=std::collections::VecDeque::new();
}, let mut next_velocity_print=std::time::Instant::now();
&InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)), let mut player_vel = physics.body.velocity.length();
&InputInstruction::MoveLeft(s)=>Some(PhysicsInputInstruction::SetMoveLeft(s)), crate::compat_worker::QNWorker::new(move |ins:TimedInstruction<Instruction>|{
&InputInstruction::MoveBack(s)=>Some(PhysicsInputInstruction::SetMoveBack(s)), if if let Some(phys_input)=match &ins.instruction{
&InputInstruction::MoveRight(s)=>Some(PhysicsInputInstruction::SetMoveRight(s)), Instruction::Input(input_instruction)=>match input_instruction{
&InputInstruction::MoveUp(s)=>Some(PhysicsInputInstruction::SetMoveUp(s)), &InputInstruction::MoveMouse(m)=>{
&InputInstruction::MoveDown(s)=>Some(PhysicsInputInstruction::SetMoveDown(s)), if mouse_blocking{
&InputInstruction::Jump(s)=>Some(PhysicsInputInstruction::SetJump(s)), //tell the game state which is living in the past about its future
&InputInstruction::Zoom(s)=>Some(PhysicsInputInstruction::SetZoom(s)), timeline.push_front(TimedInstruction{
InputInstruction::Reset=>Some(PhysicsInputInstruction::Reset), time:last_mouse_time,
}, instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:m}),
Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle), });
Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle), }else{
Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle), //mouse has just started moving again after being still for longer than 10ms.
Instruction::Render=>Some(PhysicsInputInstruction::Idle), //replace the entire mouse interpolation state to avoid an intermediate state with identical m0.t m1.t timestamps which will divide by zero
}{ timeline.push_front(TimedInstruction{
//non-mouse event time:last_mouse_time,
timeline.push_back(TimedInstruction{ instruction:PhysicsInputInstruction::ReplaceMouse(
time:ins.time, MouseState{time:last_mouse_time,pos:physics.next_mouse.pos},
instruction:phys_input, MouseState{time:ins.time,pos:m}
}); ),
});
if mouse_blocking{ //delay physics execution until we have an interpolation target
//assume the mouse has stopped moving after 10ms. mouse_blocking=true;
//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, last_mouse_time=ins.time;
//so mouse events are probably not handled separately from drawing and fire right before it :( None
if Time::from_millis(10)<ins.time-physics.next_mouse.time{ },
//push an event to extrapolate no movement from &InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)),
timeline.push_front(TimedInstruction{ &InputInstruction::MoveLeft(s)=>Some(PhysicsInputInstruction::SetMoveLeft(s)),
time:last_mouse_time, &InputInstruction::MoveBack(s)=>Some(PhysicsInputInstruction::SetMoveBack(s)),
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{time:ins.time,pos:physics.next_mouse.pos}), &InputInstruction::MoveRight(s)=>Some(PhysicsInputInstruction::SetMoveRight(s)),
}); &InputInstruction::MoveUp(s)=>Some(PhysicsInputInstruction::SetMoveUp(s)),
last_mouse_time=ins.time; &InputInstruction::MoveDown(s)=>Some(PhysicsInputInstruction::SetMoveDown(s)),
//stop blocking. the mouse is not moving so the physics does not need to live in the past and wait for interpolation targets. &InputInstruction::Jump(s)=>Some(PhysicsInputInstruction::SetJump(s)),
mouse_blocking=false; &InputInstruction::Zoom(s)=>Some(PhysicsInputInstruction::SetZoom(s)),
true InputInstruction::Reset=>Some(PhysicsInputInstruction::Reset),
}else{ },
false Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle),
} Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle),
}else{ Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle),
//keep this up to date so that it can be used as a known-timestamp Instruction::Render=>Some(PhysicsInputInstruction::Idle),
//that the mouse was not moving when the mouse starts moving again }{
last_mouse_time=ins.time; //non-mouse event
true timeline.push_back(TimedInstruction{
} time:ins.time,
}else{ instruction:phys_input,
//mouse event });
true
}{ if mouse_blocking{
//empty queue //assume the mouse has stopped moving after 10ms.
while let Some(instruction)=timeline.pop_front(){ //shitty mice are 125Hz which is 8ms so this should cover that.
physics.run(instruction.time); //setting this to 100us still doesn't print even though it's 10x lower than the polling rate,
physics.process_instruction(TimedInstruction{ //so mouse events are probably not handled separately from drawing and fire right before it :(
time:instruction.time, if Time::from_millis(10)<ins.time-physics.next_mouse.time{
instruction:crate::physics::PhysicsInstruction::Input(instruction.instruction), //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.next_mouse.pos}),
match ins.instruction{ });
Instruction::Render=>{ last_mouse_time=ins.time;
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.next_mouse.pos)).unwrap(); //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;
Instruction::Resize(size,user_settings)=>{ true
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap(); }else{
}, false
Instruction::GenerateModels(indexed_model_instances)=>{ }
physics.generate_models(&indexed_model_instances); }else{
physics.spawn(indexed_model_instances.spawn_point); //keep this up to date so that it can be used as a known-timestamp
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(indexed_model_instances)).unwrap(); //that the mouse was not moving when the mouse starts moving again
}, last_mouse_time=ins.time;
Instruction::ClearModels=>{ true
physics.clear(); }
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap(); }else{
}, //mouse event
_=>(), true
} }{
}) //empty queue
while let Some(instruction)=timeline.pop_front(){
physics.run(instruction.time);
physics.process_instruction(TimedInstruction{
time:instruction.time,
instruction:crate::physics::PhysicsInstruction::Input(instruction.instruction),
});
}
//some random print stuff
if 3.0/5.0<next_velocity_print.elapsed().as_secs_f64(){
next_velocity_print=next_velocity_print+std::time::Duration::from_secs_f64(1.0/30.0);
println!("velocity: {} u/s", (Planar64Vec3::new(physics.body.velocity.x(), Planar64::int(0), physics.body.velocity.z())).length()*(Planar64::int(130)/9));
}
}
match ins.instruction{
Instruction::Render=>{
graphics_worker.send(crate::graphics_worker::Instruction::Render(physics.output(),ins.time,physics.next_mouse.pos)).unwrap();
},
Instruction::Resize(size,user_settings)=>{
graphics_worker.send(crate::graphics_worker::Instruction::Resize(size,user_settings)).unwrap();
},
Instruction::GenerateModels(indexed_model_instances)=>{
physics.generate_models(&indexed_model_instances);
physics.spawn(indexed_model_instances.spawn_point);
graphics_worker.send(crate::graphics_worker::Instruction::GenerateModels(indexed_model_instances)).unwrap();
},
Instruction::ClearModels=>{
physics.clear();
graphics_worker.send(crate::graphics_worker::Instruction::ClearModels).unwrap();
},
_=>(),
}
})
} }