From 255bed480343b15148b67e06247bd022b748211c Mon Sep 17 00:00:00 2001 From: 9382 Date: Tue, 18 Nov 2025 18:49:44 +0000 Subject: [PATCH 1/2] Ensure the PhysicsData's bvh respects the original model ordering There's no importance in worrying about the core HashMap ordering since it's not used as an iterator except for outside of this very function for bvh purposes --- engine/physics/src/physics.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/engine/physics/src/physics.rs b/engine/physics/src/physics.rs index 1c28e341..82c2c7f1 100644 --- a/engine/physics/src/physics.rs +++ b/engine/physics/src/physics.rs @@ -978,6 +978,8 @@ impl PhysicsData{ let mut contact_models=HashMap::new(); let mut intersect_models=HashMap::new(); + let mut contacts_in_order=Vec::new(); + let mut intersects_in_order=Vec::new(); let mut physics_attr_id_from_model_attr_id=HashMap::::new(); let mut used_meshes=Vec::new(); @@ -1033,18 +1035,22 @@ impl PhysicsData{ let transform=PhysicsMeshTransform::new(model.transform); match attr_id{ PhysicsAttributesId::Contact(attr_id)=>{ - contact_models.insert(ContactModelId::new(model_id as u32),ContactModel{ + let contact_model_id=ContactModelId::new(model_id as u32); + contact_models.insert(contact_model_id,ContactModel{ mesh_id, attr_id, transform, }); + contacts_in_order.push(contact_model_id); }, PhysicsAttributesId::Intersect(attr_id)=>{ - intersect_models.insert(IntersectModelId::new(model_id as u32),IntersectModel{ + let intersect_model_id=IntersectModelId::new(model_id as u32); + intersect_models.insert(intersect_model_id,IntersectModel{ mesh_id, attr_id, transform, }); + intersects_in_order.push(intersect_model_id); }, } } @@ -1055,11 +1061,13 @@ impl PhysicsData{ ).collect(); let convex_mesh_aabb_list= //map the two lists into a single type so they can be processed with one closure - contact_models.iter().map(|(&model_id,model)| - (PhysicsModelId::Contact(model_id),&model.mesh_id,&model.transform) - ).chain(intersect_models.iter().map(|(&model_id,model)| - (PhysicsModelId::Intersect(model_id),&model.mesh_id,&model.transform) - )) + contacts_in_order.iter().map(|model_id|{ + let model=contact_models.get(model_id).unwrap(); + (PhysicsModelId::Contact(*model_id),&model.mesh_id,&model.transform) + }).chain(intersects_in_order.iter().map(|model_id|{ + let model=intersect_models.get(model_id).unwrap(); + (PhysicsModelId::Intersect(*model_id),&model.mesh_id,&model.transform) + })) .flat_map(|(model_id,mesh_id,transform)|{ meshes[mesh_id].submesh_views() .enumerate().map(move|(submesh_id,view)|{ -- 2.49.1 From 49c0c16e3562f03cb1869995951584cd9e687a41 Mon Sep 17 00:00:00 2001 From: 9382 Date: Tue, 18 Nov 2025 18:58:42 +0000 Subject: [PATCH 2/2] Use a From implementation instead of manual conversion If the contacts and intersects map ever change in the future to not be 1:1 with gaps but instead something else, this guarantees that this implicit use of the relationship will flag at a compiler level --- engine/physics/src/physics.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/engine/physics/src/physics.rs b/engine/physics/src/physics.rs index 82c2c7f1..56e07149 100644 --- a/engine/physics/src/physics.rs +++ b/engine/physics/src/physics.rs @@ -226,9 +226,9 @@ impl PhysicsModels{ } fn get_model_transform(&self,model_id:ModelId)->Option<&PhysicsMeshTransform>{ //ModelId can possibly be a decoration - match self.contact_models.get(&ContactModelId::new(model_id.get())){ + match self.contact_models.get(&model_id.into()){ Some(model)=>Some(&model.transform), - None=>self.intersect_models.get(&IntersectModelId::new(model_id.get())) + None=>self.intersect_models.get(&model_id.into()) .map(|model|&model.transform), } } @@ -671,6 +671,11 @@ impl Into for ContactModelId{ ModelId::new(self.get()) } } +impl From for ContactModelId{ + fn from(other: ModelId)->Self{ + Self::new(other.get()) + } +} #[derive(Debug,Clone,Copy,Hash,id::Id,Eq,PartialEq)] struct IntersectModelId(u32); impl Into for IntersectModelId{ @@ -678,6 +683,11 @@ impl Into for IntersectModelId{ ModelId::new(self.get()) } } +impl From for IntersectModelId{ + fn from(other: ModelId)->Self{ + Self::new(other.get()) + } +} #[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)] enum PhysicsModelId{ Contact(ContactModelId), -- 2.49.1