Compare commits

..

7 Commits

Author SHA1 Message Date
0a5a0f4c66 please fix this man 2023-10-04 23:13:45 -07:00
c61b6cfb90 this adds lag and is unnecessary 2023-10-04 22:21:13 -07:00
e8cb4a6f70 use absolute pos 2023-10-04 22:20:49 -07:00
8a81a57036 bro it takes 4 seconds to build now 2023-10-04 22:20:10 -07:00
17331ba609 time must advance! (2 bugs related to this)
global.mouse.time
physics.time
2023-10-04 20:39:42 -07:00
a24f8f5ff1 move physics to its own thread 2023-10-04 20:19:42 -07:00
e90520bb89 rename body to physics 2023-10-04 20:19:24 -07:00
3 changed files with 72 additions and 137 deletions

@ -2,7 +2,9 @@ use std::{borrow::Cow, time::Instant};
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};
use model::{Vertex,ModelInstance,ModelGraphicsInstance};
use physics::{InputInstruction, PhysicsInstruction};
use instruction::{TimedInstruction, InstructionConsumer};
use instruction::TimedInstruction;
use crate::instruction::InstructionConsumer;
mod model;
mod zeroes;
@ -119,7 +121,7 @@ pub struct GlobalState{
manual_mouse_lock:bool,
mouse:physics::MouseState,
graphics:GraphicsState,
physics_thread:worker::CompatWorker<TimedInstruction<InputInstruction>,physics::PhysicsOutputState,Box<dyn FnMut(TimedInstruction<InputInstruction>)->physics::PhysicsOutputState>>,
physics_thread:worker::Worker<TimedInstruction<InputInstruction>,physics::PhysicsOutputState>,
}
impl GlobalState{
@ -846,6 +848,7 @@ impl framework::Example for GlobalState {
//.snf = "SNMF"
//.snf = "SNBF"
if let (Ok(()),Ok(()))=(std::io::Read::read_exact(&mut input, &mut first_8),std::io::Seek::rewind(&mut input)){
//
if let Some(indexed_model_instances)={
match &first_8[0..4]{
b"<rob"=>{
@ -879,7 +882,7 @@ impl framework::Example for GlobalState {
physics.spawn_point=spawn_point;
physics.process_instruction(instruction::TimedInstruction{
time:physics.time,
instruction: PhysicsInstruction::Input(physics::PhysicsInputInstruction::Reset),
instruction: PhysicsInstruction::Input(InputInstruction::Reset),
});
physics.generate_models(&indexed_model_instances);
self.physics_thread=physics.into_worker();

@ -13,22 +13,7 @@ pub enum PhysicsInstruction {
// bool,//true = Force
// )
//InputInstructions conditionally activate RefreshWalkTarget (by doing what SetWalkTargetVelocity used to do and then flagging it)
Input(PhysicsInputInstruction),
}
#[derive(Debug)]
pub enum PhysicsInputInstruction {
ReplaceMouse(MouseState,MouseState),
SetNextMouse(MouseState),
SetMoveForward(bool),
SetMoveLeft(bool),
SetMoveBack(bool),
SetMoveRight(bool),
SetMoveUp(bool),
SetMoveDown(bool),
SetJump(bool),
SetZoom(bool),
Reset,
Idle,
Input(InputInstruction),
}
#[derive(Debug)]
pub enum InputInstruction {
@ -107,7 +92,7 @@ impl crate::instruction::InstructionConsumer<InputInstruction> for InputState{
*/
//hey dumbass just use a delta
#[derive(Clone,Debug)]
#[derive(Clone)]
pub struct MouseState {
pub pos: glam::IVec2,
pub time: TIME,
@ -121,11 +106,14 @@ impl Default for MouseState{
}
}
impl MouseState {
pub fn move_mouse(&mut self,pos:glam::IVec2,time:TIME){
self.time=time;
self.pos=pos;
}
pub fn lerp(&self,target:&MouseState,time:TIME)->glam::IVec2 {
let m0=self.pos.as_i64vec2();
let m1=target.pos.as_i64vec2();
//these are deltas
//let time=time.clamp(self.time, target.time);
let t1t=(target.time-time) as i64;
let tt0=(time-self.time) as i64;
let dt=(target.time-self.time) as i64;
@ -312,7 +300,7 @@ pub struct PhysicsOutputState{
}
impl PhysicsOutputState{
pub fn adjust_mouse(&self,mouse:&MouseState)->(glam::Vec3,glam::Vec2){
(self.body.extrapolated_position(mouse.time)+self.camera.offset,self.camera.simulate_move_angles(mouse.pos).as_vec2())
(self.body.extrapolated_position(mouse.time),self.camera.simulate_move_angles(mouse.pos).as_vec2())
}
}
@ -567,80 +555,53 @@ impl PhysicsState {
self.intersects.clear();
}
pub fn into_worker(mut self)->crate::worker::CompatWorker<TimedInstruction<InputInstruction>,PhysicsOutputState,Box<dyn FnMut(TimedInstruction<InputInstruction>)->PhysicsOutputState>>{
let mut mouse_blocking=true;
let mut last_mouse_time=self.next_mouse.time;
pub fn into_worker(mut self)->crate::worker::Worker<TimedInstruction<InputInstruction>,PhysicsOutputState>{
let mut last_time=0;
//last_time: this indicates the last time the mouse position was known.
//Only used to generate a MouseState right before mouse movement
//to finalize a long period of no movement and avoid interpolating from a long out-of-date MouseState.
let mut mouse_blocking=true;//waiting for next_mouse to be written
let mut timeline=std::collections::VecDeque::new();
crate::worker::CompatWorker::new(self.output(),Box::new(move |ins:TimedInstruction<InputInstruction>|{
if if let Some(phys_input)=match ins.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:self.next_mouse.pos},
MouseState{time:ins.time,pos:m}
),
});
//delay physics execution until we have an interpolation target
mouse_blocking=true;
crate::worker::Worker::new(self.output(),move |ins:TimedInstruction<InputInstruction>|{
let run_queue=match &ins.instruction{
InputInstruction::MoveMouse(_)=>{
//I FORGOT TO EDIT THE MOVE MOUSE TIMESTAMPS
if !mouse_blocking{
//mouse has not been moving for a while.
//make sure not to interpolate between two distant MouseStates.
//generate a mouse instruction with no movement timestamped at last InputInstruction
//Idle instructions are CRITICAL to keeping this value up to date
//interpolate normally (now that prev mouse pos is up to date)
// timeline.push_back(TimedInstruction{
// time:last_time,
// instruction:InputInstruction::MoveMouse(self.next_mouse.pos),
// });
}
last_mouse_time=ins.time;
None
mouse_blocking=true;//block physics until the next mouse event or mouse event timeout.
true//empty queue
},
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),
InputInstruction::Idle=>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 10_000_000<ins.time-self.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:self.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
_=>{
if mouse_blocking{
//maybe I can turn this inside out by making this anotehr state machine where 50_000_000 is an instruction timestamp
//check if last mouse move is within 50ms
if ins.time-self.next_mouse.time<50_000_000{
false//do not empty queue
}else{
mouse_blocking=false;
// timeline.push_back(TimedInstruction{
// time:ins.time,
// instruction:InputInstruction::MoveMouse(self.next_mouse.pos),
// });
true
}
}else{
false
true
}
}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
}{
},
};
last_time=ins.time;
timeline.push_back(ins);
if run_queue{
//empty queue
while let Some(instruction)=timeline.pop_front(){
self.run(instruction.time);
@ -651,7 +612,7 @@ impl PhysicsState {
}
}
self.output()
}))
})
}
pub fn output(&self)->PhysicsOutputState{
@ -1176,11 +1137,10 @@ impl crate::instruction::InstructionEmitter<PhysicsInstruction> for PhysicsState
impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsState {
fn process_instruction(&mut self, ins:TimedInstruction<PhysicsInstruction>) {
match &ins.instruction {
PhysicsInstruction::Input(PhysicsInputInstruction::Idle)
|PhysicsInstruction::Input(PhysicsInputInstruction::SetNextMouse(_))
|PhysicsInstruction::Input(PhysicsInputInstruction::ReplaceMouse(_,_))
PhysicsInstruction::Input(InputInstruction::Idle)
|PhysicsInstruction::Input(InputInstruction::MoveMouse(_))
|PhysicsInstruction::StrafeTick => (),
_=>println!("{}|{:?}",ins.time,ins.instruction),
_=>println!("{:?}",ins),
}
//selectively update body
match &ins.instruction {
@ -1305,32 +1265,29 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
let mut refresh_walk_target=true;
let mut refresh_walk_target_velocity=true;
match input_instruction{
PhysicsInputInstruction::SetNextMouse(m) => {
InputInstruction::MoveMouse(m) => {
self.camera.angles=self.camera.simulate_move_angles(self.next_mouse.pos);
(self.camera.mouse,self.next_mouse)=(self.next_mouse.clone(),m);
self.camera.mouse.move_mouse(self.next_mouse.pos,self.next_mouse.time);
self.next_mouse.move_mouse(m,self.time);
},
PhysicsInputInstruction::ReplaceMouse(m0,m1) => {
self.camera.angles=self.camera.simulate_move_angles(m0.pos);
(self.camera.mouse,self.next_mouse)=(m0,m1);
},
PhysicsInputInstruction::SetMoveForward(s) => self.set_control(StyleModifiers::CONTROL_MOVEFORWARD,s),
PhysicsInputInstruction::SetMoveLeft(s) => self.set_control(StyleModifiers::CONTROL_MOVELEFT,s),
PhysicsInputInstruction::SetMoveBack(s) => self.set_control(StyleModifiers::CONTROL_MOVEBACK,s),
PhysicsInputInstruction::SetMoveRight(s) => self.set_control(StyleModifiers::CONTROL_MOVERIGHT,s),
PhysicsInputInstruction::SetMoveUp(s) => self.set_control(StyleModifiers::CONTROL_MOVEUP,s),
PhysicsInputInstruction::SetMoveDown(s) => self.set_control(StyleModifiers::CONTROL_MOVEDOWN,s),
PhysicsInputInstruction::SetJump(s) => {
InputInstruction::MoveForward(s) => self.set_control(StyleModifiers::CONTROL_MOVEFORWARD,s),
InputInstruction::MoveLeft(s) => self.set_control(StyleModifiers::CONTROL_MOVELEFT,s),
InputInstruction::MoveBack(s) => self.set_control(StyleModifiers::CONTROL_MOVEBACK,s),
InputInstruction::MoveRight(s) => self.set_control(StyleModifiers::CONTROL_MOVERIGHT,s),
InputInstruction::MoveUp(s) => self.set_control(StyleModifiers::CONTROL_MOVEUP,s),
InputInstruction::MoveDown(s) => self.set_control(StyleModifiers::CONTROL_MOVEDOWN,s),
InputInstruction::Jump(s) => {
self.set_control(StyleModifiers::CONTROL_JUMP,s);
if self.grounded{
self.jump();
}
refresh_walk_target_velocity=false;
},
PhysicsInputInstruction::SetZoom(s) => {
InputInstruction::Zoom(s) => {
self.set_control(StyleModifiers::CONTROL_ZOOM,s);
refresh_walk_target=false;
},
PhysicsInputInstruction::Reset => {
InputInstruction::Reset => {
//temp
self.body.position=self.spawn_point;
self.body.velocity=glam::Vec3::ZERO;
@ -1341,7 +1298,7 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
self.grounded=false;
refresh_walk_target=false;
},
PhysicsInputInstruction::Idle => {refresh_walk_target=false;},//literally idle!
InputInstruction::Idle => {refresh_walk_target=false;},//literally idle!
}
if refresh_walk_target{
//calculate walk target velocity

@ -45,31 +45,6 @@ impl<Task:Send+'static,Value:Clone+Send+'static> Worker<Task,Value> {
}
}
pub struct CompatWorker<Task,Value:Clone,F>{
data:std::marker::PhantomData<Task>,
f:F,
value:Value,
}
impl<Task,Value:Clone,F:FnMut(Task)->Value> CompatWorker<Task,Value,F> {
pub fn new(value:Value,f:F) -> Self {
Self {
f,
value,
data:std::marker::PhantomData,
}
}
pub fn send(&mut self,task:Task)->Result<(),()>{
self.value=(self.f)(task);
Ok(())
}
pub fn grab_clone(&self)->Value{
self.value.clone()
}
}
#[test]//How to run this test with printing: cargo test --release -- --nocapture
fn test_worker() {
println!("hiiiii");