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