From f07d9f968ef0340e8da7d49a84f73bbceeae9ff3 Mon Sep 17 00:00:00 2001 From: Royiex Date: Sun, 11 Feb 2024 22:26:34 +0000 Subject: [PATCH] Print Only Horizontal Velocity The Speed function is unnecessary I forgot to remove it --- src/physics_worker.rs | 296 +++++++++++++++++++++++------------------- 1 file changed, 164 insertions(+), 132 deletions(-) diff --git a/src/physics_worker.rs b/src/physics_worker.rs index 8a483f9..3c4a949 100644 --- a/src/physics_worker.rs +++ b/src/physics_worker.rs @@ -1,133 +1,165 @@ -use crate::physics::{MouseState,PhysicsInputInstruction}; -use strafesnet_common::integer::Time; -use strafesnet_common::instruction::{TimedInstruction,InstructionConsumer}; -#[derive(Debug)] -pub enum InputInstruction { - MoveMouse(glam::IVec2), - MoveRight(bool), - MoveUp(bool), - MoveBack(bool), - MoveLeft(bool), - MoveDown(bool), - MoveForward(bool), - Jump(bool), - Zoom(bool), - Reset, -} -pub enum Instruction{ - Input(InputInstruction), - Render, - Resize(winit::dpi::PhysicalSize,crate::settings::UserSettings), - 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::compat_worker::QNWorker>{ - let mut mouse_blocking=true; - let mut last_mouse_time=physics.next_mouse.time; - let mut timeline=std::collections::VecDeque::new(); - crate::compat_worker::QNWorker::new(move |ins:TimedInstruction|{ - if if let Some(phys_input)=match &ins.instruction{ - Instruction::Input(input_instruction)=>match input_instruction{ - &InputInstruction::MoveMouse(m)=>{ - if mouse_blocking{ - //tell the game state which is living in the past about its future - timeline.push_front(TimedInstruction{ - 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. - //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{ - time:last_mouse_time, - instruction:PhysicsInputInstruction::ReplaceMouse( - MouseState{time:last_mouse_time,pos:physics.next_mouse.pos}, - MouseState{time:ins.time,pos:m} - ), - }); - //delay physics execution until we have an interpolation target - mouse_blocking=true; - } - last_mouse_time=ins.time; - None - }, - &InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)), - &InputInstruction::MoveLeft(s)=>Some(PhysicsInputInstruction::SetMoveLeft(s)), - &InputInstruction::MoveBack(s)=>Some(PhysicsInputInstruction::SetMoveBack(s)), - &InputInstruction::MoveRight(s)=>Some(PhysicsInputInstruction::SetMoveRight(s)), - &InputInstruction::MoveUp(s)=>Some(PhysicsInputInstruction::SetMoveUp(s)), - &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), - }, - Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle), - Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle), - Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle), - Instruction::Render=>Some(PhysicsInputInstruction::Idle), - }{ - //non-mouse event - timeline.push_back(TimedInstruction{ - time:ins.time, - instruction:phys_input, - }); - - if 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){ - 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(); - }, - _=>(), - } - }) +use crate::physics::{MouseState,PhysicsInputInstruction}; +use strafesnet_common::integer::Time; +use strafesnet_common::instruction::{TimedInstruction,InstructionConsumer}; +use strafesnet_common::integer::{self,Planar64,Planar64Vec3,Planar64Mat3,Angle32,Ratio64,Ratio64Vec2}; + +#[derive(Debug)] +pub enum InputInstruction { + MoveMouse(glam::IVec2), + MoveRight(bool), + MoveUp(bool), + MoveBack(bool), + MoveLeft(bool), + MoveDown(bool), + MoveForward(bool), + Jump(bool), + Zoom(bool), + Reset, +} +pub enum Instruction{ + Input(InputInstruction), + Render, + Resize(winit::dpi::PhysicalSize,crate::settings::UserSettings), + GenerateModels(crate::model::IndexedModelInstances), + ClearModels, + //Graphics(crate::graphics_worker::Instruction), +} + +pub struct Speed{ + pub player_vel:Planar64Vec3, + pub time:Time +} + +impl std::ops::Neg for Speed{ + type Output=Self; + fn neg(self)->Self::Output{ + Self{ + player_vel:self.player_vel, + time:self.time + } + } +} + +impl Speed{ + pub fn new(player_vel:Planar64Vec3,time:Time)->Self{ + Self{ + player_vel, + time, + } + } +} + pub fn new(mut physics:crate::physics::PhysicsState,mut graphics_worker:crate::compat_worker::INWorker)->crate::compat_worker::QNWorker>{ + let mut mouse_blocking=true; + let mut last_mouse_time=physics.next_mouse.time; + let mut timeline=std::collections::VecDeque::new(); + let mut next_velocity_print=std::time::Instant::now(); + let mut player_vel = physics.body.velocity.length(); + crate::compat_worker::QNWorker::new(move |ins:TimedInstruction|{ + if if let Some(phys_input)=match &ins.instruction{ + Instruction::Input(input_instruction)=>match input_instruction{ + &InputInstruction::MoveMouse(m)=>{ + if mouse_blocking{ + //tell the game state which is living in the past about its future + timeline.push_front(TimedInstruction{ + 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. + //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{ + time:last_mouse_time, + instruction:PhysicsInputInstruction::ReplaceMouse( + MouseState{time:last_mouse_time,pos:physics.next_mouse.pos}, + MouseState{time:ins.time,pos:m} + ), + }); + //delay physics execution until we have an interpolation target + mouse_blocking=true; + } + last_mouse_time=ins.time; + None + }, + &InputInstruction::MoveForward(s)=>Some(PhysicsInputInstruction::SetMoveForward(s)), + &InputInstruction::MoveLeft(s)=>Some(PhysicsInputInstruction::SetMoveLeft(s)), + &InputInstruction::MoveBack(s)=>Some(PhysicsInputInstruction::SetMoveBack(s)), + &InputInstruction::MoveRight(s)=>Some(PhysicsInputInstruction::SetMoveRight(s)), + &InputInstruction::MoveUp(s)=>Some(PhysicsInputInstruction::SetMoveUp(s)), + &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), + }, + Instruction::GenerateModels(_)=>Some(PhysicsInputInstruction::Idle), + Instruction::ClearModels=>Some(PhysicsInputInstruction::Idle), + Instruction::Resize(_,_)=>Some(PhysicsInputInstruction::Idle), + Instruction::Render=>Some(PhysicsInputInstruction::Idle), + }{ + //non-mouse event + timeline.push_back(TimedInstruction{ + time:ins.time, + instruction:phys_input, + }); + + if 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){ + 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(); + }, + _=>(), + } + }) } \ No newline at end of file