From 531473ef8313cdd4b8fff6e4d99c05157d691635 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 28 Sep 2023 16:15:12 -0700 Subject: [PATCH] calculate vertex extents for accurate mesh aabb hitboxes --- src/body.rs | 20 +++++++++++--------- src/main.rs | 12 ++++++++---- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/body.rs b/src/body.rs index 10f1def..1d2ea38 100644 --- a/src/body.rs +++ b/src/body.rs @@ -387,28 +387,30 @@ type TreyMesh = Aabb; pub struct ModelPhysics { //A model is a thing that has a hitbox. can be represented by a list of TreyMesh-es //in this iteration, all it needs is extents. - model_transform: glam::Affine3A, + mesh: TreyMesh, } impl ModelPhysics { - pub fn new(model_transform:glam::Affine3A) -> Self { - Self{model_transform} + pub fn from_model(model:&crate::model::IndexedModel,model_transform:glam::Affine3A) -> Self { + let mut aabb=Aabb::new(); + for &indexed_vertex in &model.unique_vertices { + aabb.grow(model_transform.transform_point3(glam::Vec3::from_array(model.unique_pos[indexed_vertex.pos as usize]))); + } + Self{ + mesh:aabb, + } } pub fn unit_vertices(&self) -> [glam::Vec3;8] { Aabb::unit_vertices() } pub fn mesh(&self) -> TreyMesh { - let mut aabb=Aabb::new(); - for &vertex in self.unit_vertices().iter() { - aabb.grow(self.model_transform.transform_point3(vertex)); - } - return aabb; + return self.mesh; } pub fn unit_face_vertices(&self,face:TreyMeshFace) -> [glam::Vec3;4] { Aabb::unit_face_vertices(face) } pub fn face_mesh(&self,face:TreyMeshFace) -> TreyMesh { - let mut aabb=self.mesh(); + let mut aabb=self.mesh; //in this implementation face = worldspace aabb face match face { AabbFace::Right => aabb.min.x=aabb.max.x, diff --git a/src/main.rs b/src/main.rs index 203cdfb..4474f87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,11 +87,15 @@ impl GraphicsData { depth_texture.create_view(&wgpu::TextureViewDescriptor::default()) } - fn generate_model_physics(&mut self,modeldatas:&Vec){ - self.physics.models.append(&mut modeldatas.iter().map(|m| + fn generate_model_physics(&mut self,indexed_models:&model::IndexedModelInstances){ + for instance in indexed_models.instances{ //make aabb and run vertices to get realistic bounds - m.instances.iter().map(|t|body::ModelPhysics::new(t.model_transform)) - ).flatten().collect()); + if let Some(model)=indexed_models.models.get(instance.model as usize){ + self.physics.models.push(body::ModelPhysics::from_model(model,instance.model_transform)); + }else{ + println!("oh no model missing id={}",instance.model); + } + } println!("Physics Objects: {}",self.physics.models.len()); } fn generate_model_graphics(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,mut indexed_models:model::IndexedModelInstances){