|
|
|
|
@@ -174,8 +174,7 @@ fn ground_things(walk_settings:&gameplay_style::WalkSettings,contact:&ContactCol
|
|
|
|
|
let gravity=touching.base_acceleration(models,style,camera,input_state);
|
|
|
|
|
let control_dir=style.get_y_control_dir(camera,input_state.controls);
|
|
|
|
|
let target_velocity=walk_settings.get_walk_target_velocity(control_dir,normal);
|
|
|
|
|
let contacts=touching.contacts(models,hitbox_mesh);
|
|
|
|
|
let target_velocity_clipped=touching.constrain_velocity(&contacts,target_velocity).velocity;
|
|
|
|
|
let target_velocity_clipped=touching.constrain_velocity(models,hitbox_mesh,target_velocity);
|
|
|
|
|
(gravity,target_velocity_clipped)
|
|
|
|
|
}
|
|
|
|
|
fn ladder_things(ladder_settings:&gameplay_style::LadderSettings,contact:&ContactCollision,touching:&TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,style:&StyleModifiers,camera:&PhysicsCamera,input_state:&InputState)->(Planar64Vec3,Planar64Vec3){
|
|
|
|
|
@@ -183,8 +182,7 @@ fn ladder_things(ladder_settings:&gameplay_style::LadderSettings,contact:&Contac
|
|
|
|
|
let gravity=touching.base_acceleration(models,style,camera,input_state);
|
|
|
|
|
let control_dir=style.get_y_control_dir(camera,input_state.controls);
|
|
|
|
|
let target_velocity=ladder_settings.get_ladder_target_velocity(control_dir,normal);
|
|
|
|
|
let contacts=touching.contacts(models,hitbox_mesh);
|
|
|
|
|
let target_velocity_clipped=touching.constrain_velocity(&contacts,target_velocity).velocity;
|
|
|
|
|
let target_velocity_clipped=touching.constrain_velocity(models,hitbox_mesh,target_velocity);
|
|
|
|
|
(gravity,target_velocity_clipped)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -503,6 +501,27 @@ impl StyleHelper for StyleModifiers{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[derive(Clone,Debug)]
|
|
|
|
|
struct StrafeTickState{
|
|
|
|
|
tick_number:u64,
|
|
|
|
|
}
|
|
|
|
|
impl StrafeTickState{
|
|
|
|
|
fn new(time:Time,settings:&gameplay_style::StrafeSettings)->Self{
|
|
|
|
|
// let time=n*seconds/ticks;
|
|
|
|
|
let time=time.nanos() as i128;
|
|
|
|
|
let ticks=settings.tick_rate.num() as i128;
|
|
|
|
|
let seconds=settings.tick_rate.den() as i128;
|
|
|
|
|
let tick_number=(time*ticks/seconds) as u64;
|
|
|
|
|
StrafeTickState{tick_number}
|
|
|
|
|
}
|
|
|
|
|
fn next_tick(&self,settings:&gameplay_style::StrafeSettings)->Time{
|
|
|
|
|
let n=self.tick_number as i128;
|
|
|
|
|
let ticks=settings.tick_rate.num() as i128;
|
|
|
|
|
let seconds=settings.tick_rate.den() as i128;
|
|
|
|
|
let time=n*seconds/ticks;
|
|
|
|
|
Time::from_nanos(time as i64)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[derive(Clone,Debug)]
|
|
|
|
|
enum MoveState{
|
|
|
|
|
Air,
|
|
|
|
|
Walk(ContactMoveState),
|
|
|
|
|
@@ -522,8 +541,7 @@ impl MoveState{
|
|
|
|
|
// calculate base acceleration
|
|
|
|
|
let base_acceleration=touching.base_acceleration(models,style,camera,input_state);
|
|
|
|
|
// constrain_acceleration clips according to contacts
|
|
|
|
|
let contacts=touching.contacts(models,hitbox_mesh);
|
|
|
|
|
touching.constrain_acceleration(&contacts,base_acceleration).acceleration
|
|
|
|
|
touching.constrain_acceleration(models,hitbox_mesh,base_acceleration)
|
|
|
|
|
},
|
|
|
|
|
MoveState::Walk(walk_state)
|
|
|
|
|
|MoveState::Ladder(walk_state)
|
|
|
|
|
@@ -581,7 +599,7 @@ impl MoveState{
|
|
|
|
|
=>None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn next_move_instruction(&self,strafe:&Option<gameplay_style::StrafeSettings>,time:Time)->Option<TimedInstruction<InternalInstruction,Time>>{
|
|
|
|
|
fn next_move_instruction(&self)->Option<TimedInstruction<InternalInstruction,Time>>{
|
|
|
|
|
//check if you have a valid walk state and create an instruction
|
|
|
|
|
match self{
|
|
|
|
|
MoveState::Walk(walk_state)|MoveState::Ladder(walk_state)=>match &walk_state.target{
|
|
|
|
|
@@ -593,13 +611,7 @@ impl MoveState{
|
|
|
|
|
|TransientAcceleration::Reached
|
|
|
|
|
=>None,
|
|
|
|
|
}
|
|
|
|
|
MoveState::Air=>strafe.as_ref().map(|strafe|{
|
|
|
|
|
TimedInstruction{
|
|
|
|
|
time:strafe.next_tick(time),
|
|
|
|
|
//only poll the physics if there is a before and after mouse event
|
|
|
|
|
instruction:InternalInstruction::StrafeTick
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
MoveState::Air=>None,
|
|
|
|
|
MoveState::Water=>None,//TODO
|
|
|
|
|
MoveState::Fly=>None,
|
|
|
|
|
}
|
|
|
|
|
@@ -756,18 +768,6 @@ impl Collision{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
struct Contacts<'a>{
|
|
|
|
|
contacts:Vec<crate::push_solve::Contact>,
|
|
|
|
|
lifetime:core::marker::PhantomData<&'a ()>,
|
|
|
|
|
}
|
|
|
|
|
struct ConstrainedVelocity<'a>{
|
|
|
|
|
velocity:Planar64Vec3,
|
|
|
|
|
constraints:crate::push_solve::Conts<'a>,
|
|
|
|
|
}
|
|
|
|
|
struct ConstrainedAcceleration<'a>{
|
|
|
|
|
acceleration:Planar64Vec3,
|
|
|
|
|
constraints:crate::push_solve::Conts<'a>,
|
|
|
|
|
}
|
|
|
|
|
#[derive(Clone,Debug,Default)]
|
|
|
|
|
struct TouchingState{
|
|
|
|
|
// This is kind of jank, it's a ContactCollision
|
|
|
|
|
@@ -823,8 +823,8 @@ impl TouchingState{
|
|
|
|
|
//TODO: add water
|
|
|
|
|
a
|
|
|
|
|
}
|
|
|
|
|
fn contacts<'a>(&'a self,models:&PhysicsModels,hitbox_mesh:&HitboxMesh)->Contacts<'a>{
|
|
|
|
|
let contacts=self.contacts.iter().map(|(convex_mesh_id,face_id)|{
|
|
|
|
|
fn constrain_velocity(&self,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,velocity:Planar64Vec3)->Planar64Vec3{
|
|
|
|
|
let contacts:Vec<_>=self.contacts.iter().map(|(convex_mesh_id,face_id)|{
|
|
|
|
|
let n=contact_normal(models,hitbox_mesh,convex_mesh_id,*face_id);
|
|
|
|
|
crate::push_solve::Contact{
|
|
|
|
|
position:vec3::zero(),
|
|
|
|
|
@@ -832,24 +832,18 @@ impl TouchingState{
|
|
|
|
|
normal:n,
|
|
|
|
|
}
|
|
|
|
|
}).collect();
|
|
|
|
|
Contacts{
|
|
|
|
|
contacts,
|
|
|
|
|
lifetime:core::marker::PhantomData,
|
|
|
|
|
}
|
|
|
|
|
crate::push_solve::push_solve(&contacts,velocity).0
|
|
|
|
|
}
|
|
|
|
|
fn constrain_velocity<'a>(&self,contacts:&'a Contacts<'_>,velocity:Planar64Vec3)->ConstrainedVelocity<'a>{
|
|
|
|
|
let (velocity,constraints)=crate::push_solve::push_solve(&contacts.contacts,velocity);
|
|
|
|
|
ConstrainedVelocity{
|
|
|
|
|
velocity,
|
|
|
|
|
constraints
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn constrain_acceleration<'a>(&self,contacts:&'a Contacts<'_>,acceleration:Planar64Vec3)->ConstrainedAcceleration<'a>{
|
|
|
|
|
let (acceleration,constraints)=crate::push_solve::push_solve(&contacts.contacts,acceleration);
|
|
|
|
|
ConstrainedAcceleration{
|
|
|
|
|
acceleration,
|
|
|
|
|
constraints
|
|
|
|
|
}
|
|
|
|
|
fn constrain_acceleration(&self,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,acceleration:Planar64Vec3)->Planar64Vec3{
|
|
|
|
|
let contacts:Vec<_>=self.contacts.iter().map(|(convex_mesh_id,face_id)|{
|
|
|
|
|
let n=contact_normal(models,hitbox_mesh,convex_mesh_id,*face_id);
|
|
|
|
|
crate::push_solve::Contact{
|
|
|
|
|
position:vec3::zero(),
|
|
|
|
|
velocity:n,
|
|
|
|
|
normal:n,
|
|
|
|
|
}
|
|
|
|
|
}).collect();
|
|
|
|
|
crate::push_solve::push_solve(&contacts,acceleration).0
|
|
|
|
|
}
|
|
|
|
|
fn predict_collision_end(&self,collector:&mut instruction::InstructionCollector<InternalInstruction,Time>,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,trajectory:&Trajectory,start_time:Time){
|
|
|
|
|
// let relative_body=body.relative_to(&Body::ZERO);
|
|
|
|
|
@@ -899,6 +893,7 @@ pub struct PhysicsState{
|
|
|
|
|
//gameplay_state
|
|
|
|
|
mode_state:ModeState,
|
|
|
|
|
move_state:MoveState,
|
|
|
|
|
strafe_tick_state:StrafeTickState,
|
|
|
|
|
//run is non optional: when you spawn in a run is created
|
|
|
|
|
//the run cannot be finished unless you start it by visiting
|
|
|
|
|
//a start zone. If you change mode, a new run is created.
|
|
|
|
|
@@ -917,6 +912,7 @@ impl Default for PhysicsState{
|
|
|
|
|
input_state:InputState::default(),
|
|
|
|
|
_world:WorldState{},
|
|
|
|
|
mode_state:ModeState::default(),
|
|
|
|
|
strafe_tick_state:StrafeTickState::new(Time::ZERO,&StyleModifiers::default().strafe.unwrap()),
|
|
|
|
|
run:run::Run::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -963,7 +959,7 @@ impl PhysicsState{
|
|
|
|
|
*self=Self::default();
|
|
|
|
|
}
|
|
|
|
|
fn next_move_instruction(&self)->Option<TimedInstruction<InternalInstruction,Time>>{
|
|
|
|
|
self.move_state.next_move_instruction(&self.style.strafe,self.time)
|
|
|
|
|
self.move_state.next_move_instruction()
|
|
|
|
|
}
|
|
|
|
|
fn set_move_state(&mut self,data:&PhysicsData,move_state:MoveState){
|
|
|
|
|
self.move_state.set_move_state(move_state,&mut self.body,&self.touching,&data.models,&data.hitbox_mesh,&self.style,&self.camera,&self.input_state);
|
|
|
|
|
@@ -1221,8 +1217,18 @@ fn next_instruction_internal(state:&PhysicsState,data:&PhysicsData,time_limit:Ti
|
|
|
|
|
//JUST POLLING!!! NO MUTATION
|
|
|
|
|
let mut collector=instruction::InstructionCollector::new(time_limit);
|
|
|
|
|
|
|
|
|
|
// walking
|
|
|
|
|
collector.collect(state.next_move_instruction());
|
|
|
|
|
|
|
|
|
|
// strafe tick
|
|
|
|
|
collector.collect(state.style.strafe.as_ref().map(|strafe|{
|
|
|
|
|
TimedInstruction{
|
|
|
|
|
time:state.strafe_tick_state.next_tick(strafe),
|
|
|
|
|
//only poll the physics if there is a before and after mouse event
|
|
|
|
|
instruction:InternalInstruction::StrafeTick
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
let trajectory=state.body.with_acceleration(state.acceleration(data));
|
|
|
|
|
//check for collision ends
|
|
|
|
|
state.touching.predict_collision_end(&mut collector,&data.models,&data.hitbox_mesh,&trajectory,state.time);
|
|
|
|
|
@@ -1348,50 +1354,22 @@ fn set_position(
|
|
|
|
|
recalculate_touching(move_state,body,touching,run,mode_state,mode,models,hitbox_mesh,bvh,style,camera,input_state,time);
|
|
|
|
|
point
|
|
|
|
|
}
|
|
|
|
|
/// Returns true when a contact was removed
|
|
|
|
|
fn set_velocity_cull(body:&mut Body,touching:&mut TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,v:Planar64Vec3)->bool{
|
|
|
|
|
let contacts=touching.contacts(models,hitbox_mesh);
|
|
|
|
|
let ConstrainedVelocity{velocity,mut constraints}=touching.constrain_velocity(&contacts,v);
|
|
|
|
|
|
|
|
|
|
// prep list for drain
|
|
|
|
|
constraints.sort_by_key(|&cont|{
|
|
|
|
|
let cont_ptr:*const crate::push_solve::Contact=cont;
|
|
|
|
|
contacts.contacts.len()-(cont_ptr as usize-contacts.contacts.as_ptr() as usize)
|
|
|
|
|
});
|
|
|
|
|
// create a list of indices to retain
|
|
|
|
|
let mut indices:arrayvec::ArrayVec<_,4>=constraints.iter().map(|&cont|{
|
|
|
|
|
let cont_ptr:*const crate::push_solve::Contact=cont;
|
|
|
|
|
cont_ptr as usize-contacts.contacts.as_ptr() as usize
|
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
|
|
drop(constraints);
|
|
|
|
|
|
|
|
|
|
let mut is_contact_removed=false;
|
|
|
|
|
|
|
|
|
|
// Delete contacts which do not constrain the velocity
|
|
|
|
|
let mut i=0;
|
|
|
|
|
touching.contacts.retain(|_,_|{
|
|
|
|
|
if let Some(&next_i)=indices.last(){
|
|
|
|
|
let is_active=i==next_i;
|
|
|
|
|
if is_active{
|
|
|
|
|
indices.pop();
|
|
|
|
|
}else{
|
|
|
|
|
is_contact_removed=true;
|
|
|
|
|
}
|
|
|
|
|
i+=1;
|
|
|
|
|
return is_active
|
|
|
|
|
//This is not correct but is better than what I have
|
|
|
|
|
let mut culled=false;
|
|
|
|
|
touching.contacts.retain(|convex_mesh_id,face_id|{
|
|
|
|
|
let n=contact_normal(models,hitbox_mesh,convex_mesh_id,*face_id);
|
|
|
|
|
let r=n.dot(v).is_positive();
|
|
|
|
|
if r{
|
|
|
|
|
culled=true;
|
|
|
|
|
}
|
|
|
|
|
is_contact_removed=true;
|
|
|
|
|
false
|
|
|
|
|
!r
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
body.velocity=velocity;
|
|
|
|
|
|
|
|
|
|
is_contact_removed
|
|
|
|
|
set_velocity(body,touching,models,hitbox_mesh,v);
|
|
|
|
|
culled
|
|
|
|
|
}
|
|
|
|
|
fn set_velocity(body:&mut Body,touching:&TouchingState,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,v:Planar64Vec3){
|
|
|
|
|
let contacts=touching.contacts(models,hitbox_mesh);
|
|
|
|
|
body.velocity=touching.constrain_velocity(&contacts,v).velocity;
|
|
|
|
|
body.velocity=touching.constrain_velocity(models,hitbox_mesh,v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn teleport(
|
|
|
|
|
@@ -1831,6 +1809,7 @@ fn atomic_internal_instruction(state:&mut PhysicsState,data:&PhysicsData,ins:Tim
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
InternalInstruction::StrafeTick=>{
|
|
|
|
|
state.strafe_tick_state.tick_number+=1;
|
|
|
|
|
//TODO make this less huge
|
|
|
|
|
if let Some(strafe_settings)=&state.style.strafe{
|
|
|
|
|
let controls=state.input_state.controls;
|
|
|
|
|
|