push_solve infallible type signature

This commit is contained in:
Quaternions 2025-01-09 05:56:11 -08:00
parent 121c9c5258
commit 67f8569178
2 changed files with 13 additions and 22 deletions

View File

@ -773,10 +773,7 @@ impl TouchingState{
normal:n, normal:n,
} }
}).collect(); }).collect();
match crate::push_solve::push_solve(&contacts,*velocity){ *velocity=crate::push_solve::push_solve(&contacts,*velocity);
Some(new_velocity)=>*velocity=new_velocity,
None=>println!("Algorithm silently failing :)"),
}
} }
fn constrain_acceleration(&self,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,acceleration:&mut Planar64Vec3){ fn constrain_acceleration(&self,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,acceleration:&mut Planar64Vec3){
let contacts=self.contacts.iter().map(|contact|{ let contacts=self.contacts.iter().map(|contact|{
@ -787,10 +784,7 @@ impl TouchingState{
normal:n, normal:n,
} }
}).collect(); }).collect();
match crate::push_solve::push_solve(&contacts,*acceleration){ *acceleration=crate::push_solve::push_solve(&contacts,*acceleration);
Some(new_acceleration)=>*acceleration=new_acceleration,
None=>println!("Algorithm silently failing :)"),
}
} }
fn predict_collision_end(&self,collector:&mut instruction::InstructionCollector<PhysicsInternalInstruction,TimeInner>,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,body:&Body,time:Time){ fn predict_collision_end(&self,collector:&mut instruction::InstructionCollector<PhysicsInternalInstruction,TimeInner>,models:&PhysicsModels,hitbox_mesh:&HitboxMesh,body:&Body,time:Time){
let relative_body=crate::body::VirtualBody::relative(&Body::ZERO,body).body(time); let relative_body=crate::body::VirtualBody::relative(&Body::ZERO,body).body(time);

View File

@ -164,8 +164,8 @@ fn is_space_enclosed_4(
||is_space_enclosed_3(b,c,d) ||is_space_enclosed_3(b,c,d)
} }
const fn get_push_ray_0(point:Planar64Vec3)->Option<Ray>{ const fn get_push_ray_0(point:Planar64Vec3)->Ray{
Some(Ray{origin:point,direction:vec3::ZERO}) Ray{origin:point,direction:vec3::ZERO}
} }
fn get_push_ray_1(point:Planar64Vec3,c0:&Contact)->Option<Ray>{ fn get_push_ray_1(point:Planar64Vec3,c0:&Contact)->Option<Ray>{
let direction=solve1(c0)?.divide().fix_1(); let direction=solve1(c0)?.divide().fix_1();
@ -204,11 +204,8 @@ fn get_push_ray_3(point:Planar64Vec3,c0:&Contact,c1:&Contact,c2:&Contact)->Optio
Some(Ray{origin,direction}) Some(Ray{origin,direction})
} }
const fn get_best_push_ray_and_conts_0<'a>(point:Planar64Vec3)->Option<(Ray,Conts<'a>)>{ const fn get_best_push_ray_and_conts_0<'a>(point:Planar64Vec3)->(Ray,Conts<'a>){
match get_push_ray_0(point){ (get_push_ray_0(point),Conts::new_const())
Some(ray)=>Some((ray,Conts::new_const())),
None=>None,
}
} }
fn get_best_push_ray_and_conts_1(point:Planar64Vec3,c0:&Contact)->Option<(Ray,Conts)>{ fn get_best_push_ray_and_conts_1(point:Planar64Vec3,c0:&Contact)->Option<(Ray,Conts)>{
get_push_ray_1(point,c0) get_push_ray_1(point,c0)
@ -287,7 +284,7 @@ fn get_best_push_ray_and_conts<'a>(
&[c0,c1,c2]=>get_best_push_ray_and_conts_3(point,c0,c1,c2), &[c0,c1,c2]=>get_best_push_ray_and_conts_3(point,c0,c1,c2),
&[c0,c1]=>get_best_push_ray_and_conts_2(point,c0,c1), &[c0,c1]=>get_best_push_ray_and_conts_2(point,c0,c1),
&[c0]=>get_best_push_ray_and_conts_1(point,c0), &[c0]=>get_best_push_ray_and_conts_1(point,c0),
&[]=>get_best_push_ray_and_conts_0(point), &[]=>Some(get_best_push_ray_and_conts_0(point)),
_=>unreachable!(), _=>unreachable!(),
} }
} }
@ -302,17 +299,17 @@ fn get_first_touch<'a>(contacts:&'a Vec<Contact>,ray:&Ray,conts:&Conts)->Option<
.min_by_key(|&(t,_)|t) .min_by_key(|&(t,_)|t)
} }
pub fn push_solve(contacts:&Vec<Contact>,point:Planar64Vec3)->Option<Planar64Vec3>{ pub fn push_solve(contacts:&Vec<Contact>,point:Planar64Vec3)->Planar64Vec3{
const ZERO:Ratio<Fixed<1,32>,Fixed<1,32>>=Ratio::new(Fixed::ZERO,Fixed::EPSILON); const ZERO:Ratio<Fixed<1,32>,Fixed<1,32>>=Ratio::new(Fixed::ZERO,Fixed::EPSILON);
let (mut ray,mut conts)=get_best_push_ray_and_conts_0(point)?; let (mut ray,mut conts)=get_best_push_ray_and_conts_0(point);
loop{ loop{
let (next_t,next_cont)=match get_first_touch(contacts,&ray,&conts){ let (next_t,next_cont)=match get_first_touch(contacts,&ray,&conts){
Some((t,conts))=>(t,conts), Some((t,conts))=>(t,conts),
None=>return Some(ray.origin), None=>return ray.origin,
}; };
if ZERO.le_ratio(next_t){ if ZERO.le_ratio(next_t){
return Some(ray.origin); return ray.origin;
} }
//push_front //push_front
@ -328,7 +325,7 @@ pub fn push_solve(contacts:&Vec<Contact>,point:Planar64Vec3)->Option<Planar64Vec
let meet_point=ray.extrapolate(next_t); let meet_point=ray.extrapolate(next_t);
match get_best_push_ray_and_conts(meet_point,conts){ match get_best_push_ray_and_conts(meet_point,conts){
Some((new_ray,new_conts))=>(ray,conts)=(new_ray,new_conts), Some((new_ray,new_conts))=>(ray,conts)=(new_ray,new_conts),
None=>return Some(meet_point), None=>return meet_point,
} }
} }
} }
@ -346,7 +343,7 @@ mod tests{
} }
]; ];
assert_eq!( assert_eq!(
Some(vec3::ZERO), vec3::ZERO,
push_solve(&contacts,vec3::NEG_Y) push_solve(&contacts,vec3::NEG_Y)
); );
} }