From 127b205401e93826f1ce964adc209f6bb8ac1802 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 12 Oct 2023 19:44:46 -0700 Subject: [PATCH] implement MoveState + TouchingState --- src/physics.rs | 270 ++++++++++++++++++++++++++++--------------------- 1 file changed, 157 insertions(+), 113 deletions(-) diff --git a/src/physics.rs b/src/physics.rs index 7c38ce1..05f7e5a 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -57,12 +57,6 @@ pub struct Body { time:Time,//nanoseconds x xxxxD! } -pub enum MoveRestriction { - Air, - Water, - Ground, - Ladder,//multiple ladders how -} /* enum InputInstruction { @@ -283,21 +277,25 @@ impl StyleModifiers{ } } +enum MoveState{ + Air, + Walk(WalkState), + Water, + Ladder(WalkState), +} + pub struct PhysicsState{ pub time:Time, pub body:Body, pub world:WorldState,//currently there is only one state the world can be in pub game:GameMechanicsState, pub style:StyleModifiers, - pub contacts:std::collections::HashMap::, - pub intersects:std::collections::HashMap::, - //pub intersections: Vec, + touching:TouchingState, //camera must exist in state because wormholes modify the camera, also camera punch pub camera:PhysicsCamera, pub next_mouse:MouseState,//Where is the mouse headed next pub controls:u32, - pub walk:WalkState, - pub grounded:bool, + pub move_state:MoveState, //all models pub models:Vec, pub bvh:crate::bvh::BvhNode, @@ -395,6 +393,55 @@ impl RelativeCollision { } } +struct TouchingState{ + contacts:std::collections::HashMap::, + intersects:std::collections::HashMap::, +} +impl TouchingState{ + fn clear(&mut self){ + self.contacts.clear(); + self.intersects.clear(); + } + fn insert_contact(&mut self,model_id:u32,collision:RelativeCollision)->Option{ + self.contacts.insert(model_id,collision) + } + fn remove_contact(&mut self,model_id:u32)->Option{ + self.contacts.remove(&model_id) + } + fn insert_intersect(&mut self,model_id:u32,collision:RelativeCollision)->Option{ + self.intersects.insert(model_id,collision) + } + fn remove_intersect(&mut self,model_id:u32)->Option{ + self.intersects.remove(&model_id) + } + fn constrain_velocity(&self,models:&Vec,velocity:&mut Planar64Vec3){ + for (_,contact) in &self.contacts { + let n=contact.normal(models); + let d=velocity.dot(n); + if d,acceleration:&mut Planar64Vec3){ + for (_,contact) in &self.contacts { + let n=contact.normal(models); + let d=acceleration.dot(n); + if d Self { + Self{ + contacts: std::collections::HashMap::new(), + intersects: std::collections::HashMap::new(), + } + } +} + impl Body { pub fn with_pva(position:Planar64Vec3,velocity:Planar64Vec3,acceleration:Planar64Vec3) -> Self { Self{ @@ -431,12 +478,10 @@ impl Default for PhysicsState{ body: Body::with_pva(Planar64Vec3::int(0,50,0),Planar64Vec3::int(0,0,0),Planar64Vec3::int(0,-100,0)), time: Time::ZERO, style:StyleModifiers::default(), - grounded: false, - contacts: std::collections::HashMap::new(), - intersects: std::collections::HashMap::new(), + touching:TouchingState::default(), models: Vec::new(), bvh:crate::bvh::BvhNode::default(), - walk: WalkState::new(), + move_state: MoveState::Air, camera: PhysicsCamera::from_offset(Planar64Vec3::int(0,2,0)),//4.5-2.5=2 next_mouse: MouseState::default(), controls: 0, @@ -452,8 +497,7 @@ impl PhysicsState { pub fn clear(&mut self){ self.models.clear(); self.modes.clear(); - self.contacts.clear(); - self.intersects.clear(); + self.touching.clear(); } pub fn into_worker(mut self)->crate::worker::CompatWorker,PhysicsOutputState,Box)->PhysicsOutputState>>{ @@ -653,30 +697,22 @@ impl PhysicsState { self.controls=if state{self.controls|control}else{self.controls&!control}; } fn jump(&mut self){ - self.grounded=false;//do I need this? - let mut v=self.body.velocity+self.style.get_jump_power(); - self.contact_constrain_velocity(&mut v); - self.body.velocity=v; + match &self.move_state{ + MoveState::Air=>(), + MoveState::Walk(walk_state)=>{ + let mut v=self.body.velocity+self.style.get_jump_power(); + self.touching.constrain_velocity(&self.models,&mut v); + self.body.velocity=v; + }, + MoveState::Water=>(), + MoveState::Ladder(walk_state)=>{ + let mut v=self.body.velocity+self.style.get_jump_power(); + self.touching.constrain_velocity(&self.models,&mut v); + self.body.velocity=v; + }, + } } - fn contact_constrain_velocity(&self,velocity:&mut Planar64Vec3){ - for (_,contact) in &self.contacts { - let n=contact.normal(&self.models); - let d=velocity.dot(n); - if d Option> { return Some(TimedInstruction{ time:Time::from_nanos(self.style.strafe_tick_rate.rhs_div_int(self.style.strafe_tick_rate.mul_int(self.time.nanos())+1)), @@ -718,43 +754,48 @@ 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; - //remove normal component - target_diff-=Planar64Vec3::Y*target_diff.y(); - if target_diff==Planar64Vec3::ZERO{ - let mut a=Planar64Vec3::ZERO; - self.contact_constrain_acceleration(&mut a); - self.body.acceleration=a; - self.walk.state=WalkEnum::Reached; - }else{ - //normal friction acceleration is clippedAcceleration.dot(normal)*friction - let accel=self.style.walk_accel.min(self.style.gravity.dot(Planar64Vec3::NEG_Y)*self.style.friction); - let time_delta=target_diff.length()/accel; - let mut a=target_diff.with_length(accel); - self.contact_constrain_acceleration(&mut a); - self.body.acceleration=a; - self.walk.target_time=self.body.time+Time::from(time_delta); - self.walk.state=WalkEnum::Transient; - } - }else{ - self.walk.state=WalkEnum::Reached;//there is no walk target while not grounded + match &mut self.move_state{ + MoveState::Air=>(), + MoveState::Walk(walk_state)=>{ + let mut v=walk_state.target_velocity; + self.touching.constrain_velocity(&self.models,&mut v); + let mut target_diff=v-self.body.velocity; + //remove normal component + target_diff-=Planar64Vec3::Y*target_diff.y(); + if target_diff==Planar64Vec3::ZERO{ + let mut a=Planar64Vec3::ZERO; + self.touching.constrain_acceleration(&self.models,&mut a); + self.body.acceleration=a; + walk_state.state=WalkEnum::Reached; + }else{ + //normal friction acceleration is clippedAcceleration.dot(normal)*friction + let accel=self.style.walk_accel.min(self.style.gravity.dot(Planar64Vec3::NEG_Y)*self.style.friction); + let time_delta=target_diff.length()/accel; + let mut a=target_diff.with_length(accel); + self.touching.constrain_acceleration(&self.models,&mut a); + self.body.acceleration=a; + walk_state.target_time=self.body.time+Time::from(time_delta); + walk_state.state=WalkEnum::Transient; + } + }, + MoveState::Water=>(), + MoveState::Ladder(walk_state)=>{ + // + }, } } - fn next_walk_instruction(&self) -> Option> { + fn next_move_instruction(&self)->Option>{ //check if you have a valid walk state and create an instruction - if self.grounded{ - match self.walk.state{ + match &self.move_state{ + MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>match &walk_state.state{ WalkEnum::Transient=>Some(TimedInstruction{ - time:self.walk.target_time, + time:walk_state.target_time, instruction:PhysicsInstruction::ReachWalkTargetVelocity }), WalkEnum::Reached=>None, } - }else{ - return None; + MoveState::Air=>self.next_strafe_instruction(), + MoveState::Water=>None,//TODO } } fn mesh(&self) -> TreyMesh { @@ -1044,7 +1085,7 @@ impl crate::instruction::InstructionEmitter for PhysicsState //JUST POLLING!!! NO MUTATION let mut collector = crate::instruction::InstructionCollector::new(time_limit); //check for collision stop instructions with curent contacts - for (_,collision_data) in &self.contacts { + for (_,collision_data) in &self.touching.contacts { collector.collect(self.predict_collision_end(self.time,time_limit,collision_data)); } // for collision_data in &self.intersects{ @@ -1056,17 +1097,11 @@ impl crate::instruction::InstructionEmitter for PhysicsState aabb.grow(self.body.extrapolated_position(time_limit)); aabb.inflate(self.style.hitbox_halfsize); self.bvh.the_tester(&aabb,&mut |id|{ - if !(self.contacts.contains_key(&id)||self.intersects.contains_key(&id)){ + if !(self.touching.contacts.contains_key(&id)||self.touching.intersects.contains_key(&id)){ collector.collect(self.predict_collision_start(self.time,time_limit,id)); } }); - if self.grounded { - //walk maintenance - collector.collect(self.next_walk_instruction()); - }else{ - //check to see when the next strafe tick is - collector.collect(self.next_strafe_instruction()); - } + collector.collect(self.next_move_instruction()); collector.instruction() } } @@ -1099,13 +1134,13 @@ impl crate::instruction::InstructionConsumer for PhysicsStat None=>match &c.face { TreyMeshFace::Top => { //ground - self.grounded=true; + self.move_state=MoveState::Walk(WalkState::new()) }, _ => (), }, } //check ground - self.contacts.insert(c.model,c); + self.touching.insert_contact(c.model,c); match &general.teleport_behaviour{ Some(crate::model::TeleportBehaviour::StageElement(stage_element))=>{ if stage_element.force||self.game.stage_id for PhysicsStat if let Some(model)=self.models.get(spawn as usize){ self.body.position=model.transform.transform_point3(Planar64Vec3::Y)+Planar64Vec3::Y*(self.style.hitbox_halfsize.y()+Planar64::ONE/16); //manual clear //for c in self.contacts{process_instruction(CollisionEnd(c))} - self.contacts.clear(); - self.intersects.clear(); + self.touching.clear(); self.body.acceleration=self.style.gravity; - self.walk.state=WalkEnum::Reached; - self.grounded=false; + self.move_state=MoveState::Air;//TODO: calculate contacts and determine the actual state }else{println!("bad1");} }else{println!("bad2");} }else{println!("bad3");} @@ -1140,23 +1173,23 @@ impl crate::instruction::InstructionConsumer for PhysicsStat } //flatten v let mut v=self.body.velocity; - self.contact_constrain_velocity(&mut v); + self.touching.constrain_velocity(&self.models,&mut v); match &general.booster{ Some(booster)=>{ v+=booster.velocity; - self.contact_constrain_velocity(&mut v); + self.touching.constrain_velocity(&self.models,&mut v); }, None=>(), } self.body.velocity=v; - if self.grounded&&self.style.get_control(StyleModifiers::CONTROL_JUMP,self.controls){ + if self.style.get_control(StyleModifiers::CONTROL_JUMP,self.controls){ self.jump(); } self.refresh_walk_target(); }, PhysicsCollisionAttributes::Intersect{intersecting,general}=>{ //I think that setting the velocity to 0 was preventing surface contacts from entering an infinite loop - self.intersects.insert(c.model,c); + self.touching.insert_intersect(c.model,c); match &general.teleport_behaviour{ Some(crate::model::TeleportBehaviour::StageElement(stage_element))=>{ if stage_element.force||self.game.stage_id for PhysicsStat if let Some(model)=self.models.get(spawn as usize){ self.body.position=model.transform.transform_point3(Planar64Vec3::Y)+Planar64Vec3::Y*(self.style.hitbox_halfsize.y()+Planar64::ONE/16); //manual clear //for c in self.contacts{process_instruction(CollisionEnd(c))} - self.contacts.clear(); - self.intersects.clear(); + self.touching.clear(); self.body.acceleration=self.style.gravity; - self.walk.state=WalkEnum::Reached; - self.grounded=false; + self.move_state=MoveState::Air;//TODO: calculate contacts and determine the actual state }else{println!("bad1");} }else{println!("bad2");} }else{println!("bad3");} @@ -1196,21 +1227,21 @@ impl crate::instruction::InstructionConsumer for PhysicsStat let model=c.model(&self.models).unwrap(); match &model.attributes{ PhysicsCollisionAttributes::Contact{contacting,general}=>{ - self.contacts.remove(&c.model);//remove contact before calling contact_constrain_acceleration + self.touching.remove_contact(c.model);//remove contact before calling contact_constrain_acceleration let mut a=self.style.gravity; - self.contact_constrain_acceleration(&mut a); + self.touching.constrain_acceleration(&self.models,&mut a); self.body.acceleration=a; //check ground match &c.face { TreyMeshFace::Top => { - self.grounded=false; + self.move_state=MoveState::Air; }, _ => (), } self.refresh_walk_target(); }, PhysicsCollisionAttributes::Intersect{intersecting,general}=>{ - self.intersects.remove(&c.model); + self.touching.remove_intersect(c.model); }, } }, @@ -1220,19 +1251,24 @@ impl crate::instruction::InstructionConsumer for PhysicsStat let d=self.body.velocity.dot(control_dir); if d { - //precisely set velocity - let mut a=Planar64Vec3::ZERO; - self.contact_constrain_acceleration(&mut a); - self.body.acceleration=a; - let mut v=self.walk.target_velocity; - self.contact_constrain_velocity(&mut v); - self.body.velocity=v; - self.walk.state=WalkEnum::Reached; + match &mut self.move_state{ + MoveState::Air|MoveState::Water=>(), + MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>{ + //precisely set velocity + let mut a=self.style.gravity; + self.touching.constrain_acceleration(&self.models,&mut a); + self.body.acceleration=a; + let mut v=walk_state.target_velocity; + self.touching.constrain_velocity(&self.models,&mut v); + self.body.velocity=v; + walk_state.state=WalkEnum::Reached; + } + } }, PhysicsInstruction::Input(input_instruction) => { let mut refresh_walk_target=true; @@ -1254,9 +1290,7 @@ impl crate::instruction::InstructionConsumer for PhysicsStat PhysicsInputInstruction::SetMoveDown(s) => self.set_control(StyleModifiers::CONTROL_MOVEDOWN,s), PhysicsInputInstruction::SetJump(s) => { self.set_control(StyleModifiers::CONTROL_JUMP,s); - if self.grounded{ - self.jump(); - } + self.jump(); refresh_walk_target_velocity=false; }, PhysicsInputInstruction::SetZoom(s) => { @@ -1268,10 +1302,9 @@ impl crate::instruction::InstructionConsumer for PhysicsStat self.body.position=self.spawn_point; self.body.velocity=Planar64Vec3::ZERO; //manual clear //for c in self.contacts{process_instruction(CollisionEnd(c))} - self.contacts.clear(); + self.touching.clear(); self.body.acceleration=self.style.gravity; - self.walk.state=WalkEnum::Reached; - self.grounded=false; + self.move_state=MoveState::Air; refresh_walk_target=false; }, PhysicsInputInstruction::Idle => {refresh_walk_target=false;},//literally idle! @@ -1279,9 +1312,20 @@ impl crate::instruction::InstructionConsumer for PhysicsStat if refresh_walk_target{ //calculate walk target velocity if refresh_walk_target_velocity{ - let camera_mat=self.camera.simulate_move_rotation_y(self.camera.mouse.lerp(&self.next_mouse,self.time).x); - let control_dir=camera_mat*self.style.get_control_dir(self.controls); - self.walk.target_velocity=control_dir*self.style.walkspeed; + match &mut self.move_state{ + MoveState::Walk(walk_state)=>{ + let camera_mat=self.camera.simulate_move_rotation_y(self.camera.mouse.lerp(&self.next_mouse,self.time).x); + let control_dir=camera_mat*self.style.get_control_dir(self.controls); + walk_state.target_velocity=control_dir*self.style.walkspeed; + }, + MoveState::Ladder(walk_state)=>{ + // let camera_mat=self.camera.simulate_move_rotation(self.camera.mouse.lerp(&self.next_mouse,self.time)); + // let control_dir=camera_mat*self.style.get_control_dir(self.controls); + // walk_state.target_velocity=control_dir*self.style.walkspeed; + }, + MoveState::Water=>(), + MoveState::Air=>(), + } } self.refresh_walk_target(); }