diff --git a/src/model_physics.rs b/src/model_physics.rs index 4a0595ab..a14226b7 100644 --- a/src/model_physics.rs +++ b/src/model_physics.rs @@ -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{ + 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, - } + }) } } diff --git a/src/physics.rs b/src/physics.rs index b02e48ad..af26ecd2 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -1012,10 +1012,18 @@ impl PhysicsContext{ mesh_id }else{ match map.meshes.get(model.mesh.get() as usize).and_then(|mesh|{ - let mesh_id=PhysicsMeshId::new(used_meshes.len() as u32); - used_meshes.push(PhysicsMesh::from(mesh)); - physics_mesh_id_from_model_mesh_id.insert(model.mesh,mesh_id); - Some(mesh_id) + match PhysicsMesh::try_from(mesh){ + Ok(physics_mesh)=>{ + let mesh_id=PhysicsMeshId::new(used_meshes.len() as u32); + 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,