change PhysicsMesh::from to PhysicsMesh::try_from

This commit is contained in:
Quaternions 2024-02-13 23:30:05 -08:00
parent 3b3ccefebb
commit 39924db94d
2 changed files with 32 additions and 9 deletions

View File

@ -280,9 +280,24 @@ impl EdgePool{
(&mut unsafe{self.edge_guys.get_unchecked_mut(edge_id.get() as usize)}.1,edge_id)
}
}
impl From<&model::Mesh> for PhysicsMesh{
fn from(mesh:&model::Mesh)->Self{
assert!(mesh.unique_pos.len()!=0,"Mesh cannot have 0 vertices");
#[derive(Debug)]
pub enum PhysicsMeshError{
ZeroVertices,
}
impl std::fmt::Display for PhysicsMeshError{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,"{self:?}")
}
}
impl std::error::Error for PhysicsMeshError{}
impl TryFrom<&model::Mesh> for PhysicsMesh{
type Error=PhysicsMeshError;
fn try_from(mesh:&model::Mesh)->Result<Self,PhysicsMeshError>{
if mesh.unique_pos.len()==0{
return Err(PhysicsMeshError::ZeroVertices);
}
let verts=mesh.unique_pos.iter().copied().map(Vert).collect();
//TODO: fix submeshes
//flat map mesh.physics_groups[$1].groups.polys()[$2] as face_id
@ -384,14 +399,14 @@ impl From<&model::Mesh> for PhysicsMesh{
).collect(),
}
}).collect();
Self{
Ok(Self{
data:PhysicsMeshData{
faces,
verts,
},
complete_mesh:mesh_topologies.pop().unwrap(),
submeshes:mesh_topologies,
}
})
}
}

View File

@ -1012,10 +1012,18 @@ impl PhysicsContext{
mesh_id
}else{
match map.meshes.get(model.mesh.get() as usize).and_then(|mesh|{
match PhysicsMesh::try_from(mesh){
Ok(physics_mesh)=>{
let mesh_id=PhysicsMeshId::new(used_meshes.len() as u32);
used_meshes.push(PhysicsMesh::from(mesh));
used_meshes.push(physics_mesh);
physics_mesh_id_from_model_mesh_id.insert(model.mesh,mesh_id);
Some(mesh_id)
},
Err(e)=>{
println!("Failed to build PhysicsMesh: {e}");
None
}
}
}){
Some(mesh_id)=>mesh_id,
None=>return None,