Ensure collisions are prioritised by model id instead of psuedo-random hashmap ordering #20

Merged
Quaternions merged 2 commits from aidan9382/strafe-project:consistent-collision-order into master 2025-11-18 19:42:14 +00:00

View File

@@ -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<ModelId> for ContactModelId{
ModelId::new(self.get())
}
}
impl From<ModelId> 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<ModelId> for IntersectModelId{
@@ -678,6 +683,11 @@ impl Into<ModelId> for IntersectModelId{
ModelId::new(self.get())
}
}
impl From<ModelId> for IntersectModelId{
fn from(other: ModelId)->Self{
Self::new(other.get())
}
}
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
enum PhysicsModelId{
Contact(ContactModelId),
@@ -978,6 +988,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::<CollisionAttributesId,PhysicsAttributesId>::new();
let mut used_meshes=Vec::new();
@@ -1033,18 +1045,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 +1071,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)|{