From b0d8f2e09aacff2ecec85d51e829fe9270bdd939 Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Thu, 22 Feb 2024 00:57:09 -0800
Subject: [PATCH] not-working

---
 src/physics.rs | 78 ++++++++++++++++++++++++++++----------------------
 1 file changed, 44 insertions(+), 34 deletions(-)

diff --git a/src/physics.rs b/src/physics.rs
index 6679486..8775c3e 100644
--- a/src/physics.rs
+++ b/src/physics.rs
@@ -500,8 +500,6 @@ impl MoveState{
 			},
 			_=>(),
 		}
-		//this fixes falling through the ground when walking
-		self.apply_to_body(body,touching,models,hitbox_mesh,style,camera,input_state);
 	}
 	//function to coerce &mut self into &self
 	fn apply_to_body(&self,body:&mut Body,touching:&TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,style:&StyleModifiers,camera:&PhysicsCamera,input_state:&InputState){
@@ -523,8 +521,8 @@ impl MoveState{
 			},
 		}
 	}
-	/// changes the move state and then applies it to body
-	fn apply_input(&mut self,body:&mut Body,touching:&TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,style:&StyleModifiers,camera:&PhysicsCamera,input_state:&InputState){
+	/// changes the move state
+	fn apply_input(&mut self,body:&Body,touching:&TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,style:&StyleModifiers,camera:&PhysicsCamera,input_state:&InputState){
 		match self{
 			MoveState::Fly
 			|MoveState::Air
@@ -546,8 +544,6 @@ impl MoveState{
 				}
 			},
 		}
-		//precalculate gravity???
-		self.apply_to_body(body,touching,models,hitbox_mesh,style,camera,input_state);
 	}
 	fn get_walk_state(&self)->Option<&ContactMoveState>{
 		match self{
@@ -957,10 +953,37 @@ impl PhysicsState {
 	fn next_move_instruction(&self)->Option<TimedInstruction<PhysicsInstruction>>{
 		self.move_state.next_move_instruction(&self.style.strafe,self.time)
 	}
+	//lmao idk this is convenient
+	fn apply_enum_and_input_and_body(&mut self,data:&PhysicsData){
+		self.move_state.apply_enum(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
+		self.move_state.apply_input(&self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
+		self.move_state.apply_to_body(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
+	}
+	fn apply_enum_and_body(&mut self,data:&PhysicsData){
+		self.move_state.apply_enum(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
+		self.move_state.apply_to_body(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
+	}
+	fn apply_input_and_body(&mut self,data:&PhysicsData){
+		self.move_state.apply_input(&self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
+		self.move_state.apply_to_body(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
+	}
 	fn set_move_state(&mut self,data:&PhysicsData,move_state:MoveState){
-		move_state.apply_enum(&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
+		self.apply_enum_and_body(data);
 		self.move_state=move_state;
 	}
+	fn cull_velocity(&mut self,data:&PhysicsData,velocity:Planar64Vec3){
+		//TODO: be more precise about contacts
+		if set_velocity_cull(&mut self.body,&mut self.touching,&data.models,&data.hitbox_mesh,velocity){
+			//TODO do better
+			match self.move_state.get_walk_state(){
+				//did you stop touching the thing you were walking on?
+				Some(walk_state)=>if !self.touching.contacts.contains(&walk_state.contact){
+					self.set_move_state(&data,MoveState::Air);
+				},
+				None=>self.apply_enum_and_body(data),
+			}
+		}
+	}
 
 	//state mutated on collision:
 	//Accelerator
@@ -1351,10 +1374,7 @@ fn run_teleport_behaviour(wormhole:&Option<gameplay_attributes::Wormhole>,models
 									//&gameplay_attributes::Booster::Affine(transform)=>v=transform.transform_point3(v),
 									&gameplay_attributes::Booster::Velocity(velocity)=>{
 										let boosted_velocity=state.body.velocity+velocity;
-										if set_velocity_cull(&mut state.body,&mut state.touching,&data.models,&data.hitbox_mesh,boosted_velocity){
-											//wrong!!!
-											state.set_move_state(&data,MoveState::Air);
-										}
+										state.cull_velocity(data,boosted_velocity);
 									},
 									&gameplay_attributes::Booster::Energy{direction: _,energy: _}=>todo!(),
 								}
@@ -1365,10 +1385,7 @@ fn run_teleport_behaviour(wormhole:&Option<gameplay_attributes::Wormhole>,models
 							if let (Some(jump_settings),Some(walk_state))=(&state.style.jump,state.move_state.get_walk_state()){
 								let jump_dir=walk_state.jump_direction.direction(&data.models,&data.hitbox_mesh,&walk_state.contact);
 								let jumped_velocity=jump_settings.jumped_velocity(&state.style,jump_dir,state.body.velocity);
-								if set_velocity_cull(&mut state.body,&mut state.touching,&data.models,&data.hitbox_mesh,jumped_velocity){
-									//wrong!!!
-									state.set_move_state(&data,MoveState::Air);
-								}
+								state.cull_velocity(data,jumped_velocity);
 							}
 						}
 						match &general.trajectory{
@@ -1379,18 +1396,16 @@ fn run_teleport_behaviour(wormhole:&Option<gameplay_attributes::Wormhole>,models
 									gameplay_attributes::SetTrajectory::TargetPointTime { target_point: _, time: _ }=>todo!(),
 									gameplay_attributes::SetTrajectory::TargetPointSpeed { target_point: _, speed: _, trajectory_choice: _ }=>todo!(),
 									&gameplay_attributes::SetTrajectory::Velocity(velocity)=>{
-										if set_velocity_cull(&mut state.body,&mut state.touching,&data.models,&data.hitbox_mesh,velocity){
-											//wrong!!!
-											state.set_move_state(&data,MoveState::Air);
-										}
+										state.cull_velocity(data,velocity);
 									},
 									gameplay_attributes::SetTrajectory::DotVelocity { direction: _, dot: _ }=>todo!(),
 								}
 							},
 							None=>(),
 						}
-						//just doing this to set the acceleration when surfing
-						state.move_state.apply_enum(&mut state.body,&state.touching,&data.models,&data.hitbox_mesh,&state.style,&state.camera,&state.input_state);
+						//doing enum to set the acceleration when surfing
+						//doing input_and_body to refresh the walk state if you hit a wall while accelerating
+						state.apply_enum_and_input_and_body(data);
 					},
 					(PhysicsCollisionAttributes::Intersect{intersecting: _,general},Collision::Intersect(intersect))=>{
 						//I think that setting the velocity to 0 was preventing surface contacts from entering an infinite loop
@@ -1407,13 +1422,14 @@ fn run_teleport_behaviour(wormhole:&Option<gameplay_attributes::Wormhole>,models
 					(PhysicsCollisionAttributes::Contact{contacting:_,general:_},&Collision::Contact(contact))=>{
 						state.touching.remove(&collision);//remove contact before calling contact_constrain_acceleration
 						//check ground
-						//TODO lol
+						//TODO do better
+						//this is inner code from state.cull_velocity
 						match state.move_state.get_walk_state(){
 							//did you stop touching the thing you were walking on?
 							Some(walk_state)=>if walk_state.contact==contact{
 								state.set_move_state(&data,MoveState::Air);
 							},
-							None=>(),
+							None=>state.apply_enum_and_body(data),
 						}
 					},
 					(PhysicsCollisionAttributes::Intersect{intersecting: _,general:_},Collision::Intersect(_))=>{
@@ -1431,13 +1447,10 @@ fn run_teleport_behaviour(wormhole:&Option<gameplay_attributes::Wormhole>,models
 						let control_dir=state.style.get_control_dir(masked_controls);
 						if control_dir!=Planar64Vec3::ZERO{
 							let camera_mat=state.camera.simulate_move_rotation_y(state.input_state.lerp_delta(state.time).x);
-							if let Some(v)=strafe_settings.tick_velocity(state.body.velocity,(camera_mat*control_dir).with_length(Planar64::ONE)){
+							if let Some(ticked_velocity)=strafe_settings.tick_velocity(state.body.velocity,(camera_mat*control_dir).with_length(Planar64::ONE)){
 								//this is wrong but will work ig
 								//need to note which push planes activate in push solve and keep those
-								if set_velocity_cull(&mut state.body,&mut state.touching,&data.models,&data.hitbox_mesh,v){
-									//wrong!!!
-									state.set_move_state(&data,MoveState::Air);
-								}
+								state.cull_velocity(data,ticked_velocity);
 							}
 						}
 					}
@@ -1492,11 +1505,7 @@ fn run_teleport_behaviour(wormhole:&Option<gameplay_attributes::Wormhole>,models
 							if let Some(jump_settings)=&state.style.jump{
 								let jump_dir=walk_state.jump_direction.direction(&data.models,&data.hitbox_mesh,&walk_state.contact);
 								let jumped_velocity=jump_settings.jumped_velocity(&state.style,jump_dir,state.body.velocity);
-								//TODO: be more precise about contacts
-								if set_velocity_cull(&mut state.body,&mut state.touching,&data.models,&data.hitbox_mesh,jumped_velocity){
-									//wrong!!!
-									state.set_move_state(&data,MoveState::Air);
-								}
+								state.cull_velocity(&data,jumped_velocity);
 							}
 						}
 					},
@@ -1531,7 +1540,8 @@ fn run_teleport_behaviour(wormhole:&Option<gameplay_attributes::Wormhole>,models
 					PhysicsInputInstruction::Idle=>{b_refresh_walk_target=false;},//literally idle!
 				}
 				if b_refresh_walk_target{
-					state.move_state.apply_input(&mut state.body,&state.touching,&data.models,&data.hitbox_mesh,&state.style,&state.camera,&state.input_state);
+					state.apply_input_and_body(data);
+					state.cull_velocity(data,state.body.velocity);
 				}
 			},
 		}