diff --git a/src/body.rs b/src/body.rs index da756e1..8b78b76 100644 --- a/src/body.rs +++ b/src/body.rs @@ -6,7 +6,6 @@ pub enum PhysicsInstruction { CollisionEnd(RelativeCollision), StrafeTick, Jump, - RefreshWalkTarget,//this could be a helper function instead of an instruction ReachWalkTargetVelocity, // Water, // Spawn( @@ -15,8 +14,20 @@ pub enum PhysicsInstruction { // bool,//true = Force // ) //Both of these conditionally activate RefreshWalkTarget (by doing what SetWalkTargetVelocity used to do and then flagging it) + Input(InputInstruction), +} +#[derive(Debug)] +pub enum InputInstruction { MoveMouse(glam::IVec2), - SetControls(u32,u32),//can activate Jump + MoveForward(bool), + MoveLeft(bool), + MoveBack(bool), + MoveRight(bool), + MoveUp(bool), + MoveDown(bool), + Jump(bool), + Zoom(bool), + Reset, } pub struct Body { @@ -90,6 +101,15 @@ pub struct MouseInterpolationState { } impl MouseInterpolationState { + pub fn new() -> Self { + Self { + interpolation:MouseInterpolation::Lerp, + time0:0, + time1:1,//ONE NANOSECOND!!!! avoid divide by zero + mouse0:glam::IVec2::ZERO, + mouse1:glam::IVec2::ZERO, + } + } pub fn move_mouse(&mut self,time:TIME,pos:glam::IVec2){ self.time0=self.time1; self.mouse0=self.mouse1; @@ -115,7 +135,6 @@ impl MouseInterpolationState { pub enum WalkEnum{ Reached, Transient, - Invalid, } pub struct WalkState { pub target_velocity: glam::Vec3, @@ -127,7 +146,7 @@ impl WalkState { Self{ target_velocity:glam::Vec3::ZERO, target_time:0, - state:WalkEnum::Invalid, + state:WalkEnum::Reached, } } } @@ -135,22 +154,63 @@ impl WalkState { // Note: we use the Y=up coordinate space in this example. pub struct Camera { offset: glam::Vec3, - angles: glam::Vec3, + angles: glam::DVec2,//YAW AND THEN PITCH //punch: glam::Vec3, //punch_velocity: glam::Vec3, fov: glam::Vec2,//slope - sensitivity: glam::Vec2, + sensitivity: glam::DVec2, + time: TIME, } +#[inline] +fn mat3_from_rotation_y_f64(angle: f64) -> glam::Mat3 { + let (sina, cosa) = angle.sin_cos(); + glam::Mat3::from_cols( + glam::Vec3::new(cosa as f32, 0.0, -sina as f32), + glam::Vec3::Y, + glam::Vec3::new(sina as f32, 0.0, cosa as f32), + ) +} +#[inline] +fn perspective_rh(fov_x_slope: f32, fov_y_slope: f32, z_near: f32, z_far: f32) -> glam::Mat4 { + //glam_assert!(z_near > 0.0 && z_far > 0.0); + let r = z_far / (z_near - z_far); + glam::Mat4::from_cols( + glam::Vec4::new(1.0/fov_x_slope, 0.0, 0.0, 0.0), + glam::Vec4::new(0.0, 1.0/fov_y_slope, 0.0, 0.0), + glam::Vec4::new(0.0, 0.0, r, -1.0), + glam::Vec4::new(0.0, 0.0, r * z_near, 0.0), + ) +} impl Camera { - fn from_offset(offset:glam::Vec3,aspect:f32) -> Self { + pub fn from_offset(offset:glam::Vec3,aspect:f32) -> Self { Self{ offset, - angles: glam::Vec3::ZERO, + angles: glam::DVec2::ZERO, fov: glam::vec2(aspect,1.0), - sensitivity: glam::vec2(1.0/2048.0,1.0/2048.0), + sensitivity: glam::dvec2(1.0/2048.0,1.0/2048.0), + time: 0, } } + fn simulate_move_angles(&self, delta: glam::IVec2) -> glam::DVec2 { + let mut a=self.angles-self.sensitivity*delta.as_dvec2(); + a.y=a.y.clamp(-std::f64::consts::PI, std::f64::consts::PI); + return a + } + fn simulate_move_rotation_y(&self, delta_x: i32) -> glam::Mat3 { + mat3_from_rotation_y_f64(self.angles.x-self.sensitivity.x*(delta_x as f64)) + } + pub fn proj(&self)->glam::Mat4{ + perspective_rh(self.fov.x, self.fov.y, 0.5, 1000.0) + } + pub fn view(&self,pos:glam::Vec3)->glam::Mat4{ + //f32 good enough for view matrix + glam::Mat4::from_translation(pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.angles.y as f32, self.angles.x as f32, 0f32) + } + pub fn set_fov_aspect(&mut self,fov:f32,aspect:f32){ + self.fov.x=fov*aspect; + self.fov.y=fov; + } } const CONTROL_MOVEFORWARD:u32 = 0b00000001; @@ -441,7 +501,6 @@ impl PhysicsState { } } } - fn next_strafe_instruction(&self) -> Option> { return Some(TimedInstruction{ time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num, @@ -481,6 +540,31 @@ impl PhysicsState { // }); // } + fn refresh_walk_target(&mut self){ + //calculate acceleration yada yada + if self.grounded{ + let mut v=self.walk.target_velocity; + self.contact_constrain_velocity(&mut v); + let mut target_diff=v-self.body.velocity; + target_diff.y=0f32; + if target_diff==glam::Vec3::ZERO{ + let mut a=glam::Vec3::ZERO; + self.contact_constrain_acceleration(&mut a); + self.body.acceleration=a; + self.walk.state=WalkEnum::Reached; + }else{ + let accel=self.walk_accel.min(self.gravity.length()*self.friction); + let time_delta=target_diff.length()/accel; + let mut a=target_diff/time_delta; + self.contact_constrain_acceleration(&mut a); + self.body.acceleration=a; + self.walk.target_time=self.body.time+((time_delta as f64)*1_000_000_000f64) as TIME; + self.walk.state=WalkEnum::Transient; + } + }else{ + self.walk.state=WalkEnum::Reached;//there is no walk target while not grounded + } + } fn next_walk_instruction(&self) -> Option> { //check if you have a valid walk state and create an instruction if self.grounded{ @@ -489,10 +573,6 @@ impl PhysicsState { time:self.walk.target_time, instruction:PhysicsInstruction::ReachWalkTargetVelocity }), - WalkEnum::Invalid=>Some(TimedInstruction{ - time:self.time, - instruction:PhysicsInstruction::RefreshWalkTarget, - }), WalkEnum::Reached=>None, } }else{ @@ -808,13 +888,12 @@ impl crate::instruction::InstructionConsumer for PhysicsStat fn process_instruction(&mut self, ins:TimedInstruction) { match &ins.instruction { PhysicsInstruction::StrafeTick => (), + PhysicsInstruction::Input(InputInstruction::MoveMouse(_)) => (), _=>println!("{:?}",ins), } //selectively update body match &ins.instruction { - PhysicsInstruction::SetWalkTargetVelocity(_) - |PhysicsInstruction::SetControlDir(_) => self.time=ins.time,//TODO: queue instructions - PhysicsInstruction::RefreshWalkTarget + PhysicsInstruction::Input(_) |PhysicsInstruction::ReachWalkTargetVelocity |PhysicsInstruction::CollisionStart(_) |PhysicsInstruction::CollisionEnd(_) @@ -836,14 +915,13 @@ impl crate::instruction::InstructionConsumer for PhysicsStat let mut v=self.body.velocity; self.contact_constrain_velocity(&mut v); self.body.velocity=v; - self.walk.state=WalkEnum::Invalid; + self.refresh_walk_target(); }, PhysicsInstruction::CollisionEnd(c) => { self.contacts.remove(&c);//remove contact before calling contact_constrain_acceleration let mut a=self.gravity; self.contact_constrain_acceleration(&mut a); self.body.acceleration=a; - self.walk.state=WalkEnum::Invalid; //check ground match &c.face { AabbFace::Top => { @@ -851,16 +929,14 @@ impl crate::instruction::InstructionConsumer for PhysicsStat }, _ => (), } - }, - PhysicsInstruction::SetControlDir(control_dir)=>{ - self.temp_control_dir=control_dir; - self.walk.state=WalkEnum::Invalid; + self.refresh_walk_target(); }, PhysicsInstruction::StrafeTick => { - //let control_dir=self.get_control_dir();//this should respect your mouse interpolation settings - let d=self.body.velocity.dot(self.temp_control_dir); + let camera_mat=self.camera.simulate_move_rotation_y(self.mouse_interpolation.interpolated_position(self.time).x-self.mouse_interpolation.mouse0.x); + let control_dir=camera_mat*get_control_dir(self.controls); + let d=self.body.velocity.dot(control_dir); if d for PhysicsStat let mut v=self.body.velocity+glam::Vec3::new(0.0,0.715588/2.0*100.0,0.0); self.contact_constrain_velocity(&mut v); self.body.velocity=v; - self.walk.state=WalkEnum::Invalid; + self.refresh_walk_target(); }, PhysicsInstruction::ReachWalkTargetVelocity => { //precisely set velocity @@ -882,32 +958,25 @@ impl crate::instruction::InstructionConsumer for PhysicsStat self.body.velocity=v; self.walk.state=WalkEnum::Reached; }, - PhysicsInstruction::RefreshWalkTarget => { - //calculate acceleration yada yada - if self.grounded{ - let mut v=self.walk.target_velocity; - self.contact_constrain_velocity(&mut v); - let mut target_diff=v-self.body.velocity; - target_diff.y=0f32; - if target_diff==glam::Vec3::ZERO{ - let mut a=glam::Vec3::ZERO; - self.contact_constrain_acceleration(&mut a); - self.body.acceleration=a; - self.walk.state=WalkEnum::Reached; - }else{ - let accel=self.walk_accel.min(self.gravity.length()*self.friction); - let time_delta=target_diff.length()/accel; - let mut a=target_diff/time_delta; - self.contact_constrain_acceleration(&mut a); - self.body.acceleration=a; - self.walk.target_time=self.body.time+((time_delta as f64)*1_000_000_000f64) as TIME; - self.walk.state=WalkEnum::Transient; - } + PhysicsInstruction::Input(input_instruction) => { + match input_instruction{ + InputInstruction::MoveMouse(m) => self.mouse_interpolation.move_mouse(self.time,m), + InputInstruction::MoveForward(s) => self.controls=if s{self.controls|CONTROL_MOVEFORWARD}else{self.controls&!CONTROL_MOVEFORWARD}, + InputInstruction::MoveLeft(s) => self.controls=if s{self.controls|CONTROL_MOVELEFT}else{self.controls&!CONTROL_MOVELEFT}, + InputInstruction::MoveBack(s) => self.controls=if s{self.controls|CONTROL_MOVEBACK}else{self.controls&!CONTROL_MOVEBACK}, + InputInstruction::MoveRight(s) => self.controls=if s{self.controls|CONTROL_MOVERIGHT}else{self.controls&!CONTROL_MOVERIGHT}, + InputInstruction::MoveUp(s) => self.controls=if s{self.controls|CONTROL_MOVEUP}else{self.controls&!CONTROL_MOVEUP}, + InputInstruction::MoveDown(s) => self.controls=if s{self.controls|CONTROL_MOVEDOWN}else{self.controls&!CONTROL_MOVEDOWN}, + InputInstruction::Jump(s) => self.controls=if s{self.controls|CONTROL_JUMP}else{self.controls&!CONTROL_JUMP}, + InputInstruction::Zoom(s) => self.controls=if s{self.controls|CONTROL_ZOOM}else{self.controls&!CONTROL_ZOOM}, + InputInstruction::Reset => println!("reset"), } - }, - PhysicsInstruction::SetWalkTargetVelocity(v) => { - self.walk.target_velocity=v; - self.walk.state=WalkEnum::Invalid; + //calculate control dir + let camera_mat=self.camera.simulate_move_rotation_y(self.mouse_interpolation.interpolated_position(self.time).x-self.mouse_interpolation.mouse0.x); + let control_dir=camera_mat*get_control_dir(self.controls); + //calculate walk target velocity + self.walk.target_velocity=self.walkspeed*control_dir; + self.refresh_walk_target(); }, } } diff --git a/src/main.rs b/src/main.rs index 2e390fa..8ade9a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use bytemuck::{Pod, Zeroable}; -use strafe_client::{instruction::{TimedInstruction, self}, body::MouseInterpolationState}; +use strafe_client::{instruction::{TimedInstruction, InstructionConsumer},body::{InputInstruction, PhysicsInstruction}}; use std::{borrow::Cow, time::Instant}; use wgpu::{util::DeviceExt, AstcBlock, AstcChannel}; @@ -261,22 +261,11 @@ fn generate_modeldatas(data:obj::ObjData,color:[f32;4]) -> Vec{ modeldatas } -#[inline] -fn perspective_rh(fov_x_slope: f32, fov_y_slope: f32 z_near: f32, z_far: f32) -> glam::Mat4 { - //glam_assert!(z_near > 0.0 && z_far > 0.0); - let r = z_far / (z_near - z_far); - glam::Mat4::from_cols( - glam::Vec4::new(1.0/fov_x_slope, 0.0, 0.0, 0.0), - glam::Vec4::new(0.0, 1.0/fov_y_slope, 0.0, 0.0), - glam::Vec4::new(0.0, 0.0, r, -1.0), - glam::Vec4::new(0.0, 0.0, r * z_near, 0.0), - ) -} -fn to_uniform_data(camera: &Camera, pos: glam::Vec3) -> [f32; 16 * 3 + 4] { - let proj = perspective_rh(self.fov_x, self.fov_y, 0.5, 1000.0); +fn to_uniform_data(camera: &strafe_client::body::Camera, pos: glam::Vec3) -> [f32; 16 * 3 + 4] { + let proj=camera.proj(); let proj_inv = proj.inverse(); - let view = glam::Mat4::from_translation(pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.angles.y, self.angles.x, self.angles.z); + let view=camera.view(pos); let view_inv = view.inverse(); let mut raw = [0f32; 16 * 3 + 4]; @@ -526,12 +515,12 @@ impl strafe_client::framework::Example for GraphicsData { jump_trying: false, walkspeed: 18.0, contacts: std::collections::HashSet::new(), - models: Vec::new(), - walk: strafe_client::body::WalkState::new(), - hitbox_halfsize: glam::vec3(1.0,2.5,1.0), - camera: Camera::from_offset(glam::vec3(0.0,4.5-2.5,0.0),(config.width as f32)/(config.height as f32)), - mouse_interpolation: strafe_client::body::MouseInterpolationState::new(), - controls: 0, + models: Vec::new(), + walk: strafe_client::body::WalkState::new(), + hitbox_halfsize: glam::vec3(1.0,2.5,1.0), + camera: strafe_client::body::Camera::from_offset(glam::vec3(0.0,4.5-2.5,0.0),(config.width as f32)/(config.height as f32)), + mouse_interpolation: strafe_client::body::MouseInterpolationState::new(), + controls: 0, }; //load textures @@ -720,7 +709,7 @@ impl strafe_client::framework::Example for GraphicsData { multiview: None, }); - let camera_uniforms = camera.to_uniform_data(physics.body.extrapolated_position(0)); + let camera_uniforms = to_uniform_data(&physics.camera,physics.body.extrapolated_position(0)); let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Camera"), contents: bytemuck::cast_slice(&camera_uniforms), @@ -756,7 +745,7 @@ impl strafe_client::framework::Example for GraphicsData { let mut graphics=GraphicsData { handy_unit_cube:unit_cube, start_time: Instant::now(), - screen_size, + screen_size: (config.width,config.height), physics, pipelines:GraphicsPipelines{ skybox:sky_pipeline, @@ -806,58 +795,57 @@ impl strafe_client::framework::Example for GraphicsData { fn device_event(&mut self, event: winit::event::DeviceEvent) { //there's no way this is the best way get a timestamp. let time=self.start_time.elapsed().as_nanos() as i64; + self.physics.run(time);//call it a day match event { - winit::event::DeviceEvent::KeyboardInput { - input: - winit::event::KeyboardInput { - state, - scancode: keycode, - .. - }, - } => { + winit::event::DeviceEvent::Key(winit::event::KeyboardInput { + state, + scancode: keycode, + .. + }) => { let s=match state { winit::event::ElementState::Pressed => true, winit::event::ElementState::Released => false, }; - if let Some(instruction)=match keycode { - 119 => Some(InputInstruction::MoveForward(s)),//W - 97 => Some(InputInstruction::MoveLeft(s)),//A - 115 => Some(InputInstruction::MoveBack(s)),//S - 100 => Some(InputInstruction::MoveRight(s)),//D - 101 => Some(InputInstruction::MoveUp(s)),//E - 113 => Some(InputInstruction::MoveDown(s)),//Q - 32 => Some(InputInstruction::Jump(s)),//Space - 122 => Some(InputInstruction::Zoom(s)),//Z - 114 => if s{Some(InputInstruction::Reset)}else{None},//R + if let Some(input_instruction)=match keycode { + 17 => Some(InputInstruction::MoveForward(s)),//W + 30 => Some(InputInstruction::MoveLeft(s)),//A + 31 => Some(InputInstruction::MoveBack(s)),//S + 32 => Some(InputInstruction::MoveRight(s)),//D + 18 => Some(InputInstruction::MoveUp(s)),//E + 16 => Some(InputInstruction::MoveDown(s)),//Q + 57 => Some(InputInstruction::Jump(s)),//Space + 44 => Some(InputInstruction::Zoom(s)),//Z + 19 => if s{Some(InputInstruction::Reset)}else{None},//R _ => None, } { - self.physics.queue_input_instruction(TimedInstruction{ + self.physics.process_instruction(TimedInstruction{ time, - instruction, + instruction:PhysicsInstruction::Input(input_instruction), }) } }, winit::event::DeviceEvent::MouseMotion { delta,//these (f64,f64) are integers on my machine } => { - self.physics.queue_input_instruction(TimedInstruction{ + self.physics.process_instruction(TimedInstruction{ time, - instruction:InputInstruction::MoveMouse(glam::ivec2(delta.0 as i32,delta.1 as i32)) + instruction:PhysicsInstruction::Input(InputInstruction::MoveMouse(glam::ivec2(delta.0 as i32,delta.1 as i32))), }) }, winit::event::DeviceEvent::MouseWheel { - delta,//these (f64,f64) are integers on my machine + delta, } => { - if self.physics.use_scroll{ - self.physics.queue_input_instruction(TimedInstruction{ + println!("mousewheel{:?}",delta); + if true{//self.physics.use_scroll + self.physics.process_instruction(TimedInstruction{ time, - instruction:InputInstruction::Jump(true)//activates the immediate jump path, but the style modifier prevents controls&CONTROL_JUMP bit from being set to auto jump + instruction:PhysicsInstruction::Input(InputInstruction::Jump(true)),//activates the immediate jump path, but the style modifier prevents controls&CONTROL_JUMP bit from being set to auto jump }) } } + _=>(), } - self.physics.queue_input_instruction() } fn resize( @@ -868,7 +856,7 @@ impl strafe_client::framework::Example for GraphicsData { ) { self.depth_view = Self::create_depth_texture(config, device); self.screen_size = (config.width, config.height); - self.physics.camera.fov_x=self.physics.camera.fov_y*(config.width as f32)/(config.height as f32); + self.physics.camera.set_fov_aspect(1.0,(config.width as f32)/(config.height as f32)); } fn render( @@ -886,7 +874,7 @@ impl strafe_client::framework::Example for GraphicsData { device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); // update rotation - let camera_uniforms = to_uniform_data(self.camera,self.physics.body.extrapolated_position(time)); + let camera_uniforms = to_uniform_data(&self.physics.camera,self.physics.body.extrapolated_position(time)); self.staging_belt .write_buffer( &mut encoder,