From 82b3201b0aa35d137916bf0ec1aa99d6867acfb5 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 1 Dec 2023 20:00:23 -0800 Subject: [PATCH] optimize face_nd: precalculate det --- src/model_physics.rs | 5 ++++- src/physics.rs | 25 ++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/model_physics.rs b/src/model_physics.rs index 452ebcfd..a8d18229 100644 --- a/src/model_physics.rs +++ b/src/model_physics.rs @@ -238,17 +238,20 @@ pub struct TransformedMesh<'a>{ mesh:&'a PhysicsMesh, transform:&'a crate::integer::Planar64Affine3, normal_transform:&'a crate::integer::Planar64Mat3, + transform_det:Planar64, } impl TransformedMesh<'_>{ pub fn new<'a>( mesh:&'a PhysicsMesh, transform:&'a crate::integer::Planar64Affine3, normal_transform:&'a crate::integer::Planar64Mat3, + transform_det:Planar64, )->TransformedMesh<'a>{ TransformedMesh{ mesh, transform, normal_transform, + transform_det, } } fn farthest_vert(&self,dir:Planar64Vec3)->VertId{ @@ -269,7 +272,7 @@ impl MeshQuery for TransformedMesh<'_>{ fn face_nd(&self,face_id:FaceId)->(Planar64Vec3,Planar64){ let (n,d)=self.mesh.face_nd(face_id); let transformed_n=*self.normal_transform*n; - let transformed_d=Planar64::raw(((transformed_n.dot128(self.transform.matrix3*n)<<32)/n.dot128(n)) as i64)*d+transformed_n.dot(self.transform.translation); + let transformed_d=self.transform_det*d+transformed_n.dot(self.transform.translation); (transformed_n,transformed_d) } fn vert(&self,vert_id:VertId)->Planar64Vec3{ diff --git a/src/physics.rs b/src/physics.rs index dc69c434..a41d7fa0 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -202,6 +202,7 @@ impl PhysicsModels{ &self.meshes[self.models[model_id].mesh_id], &self.models[model_id].transform, &self.models[model_id].normal_transform, + self.models[model_id].transform_det, ) } fn model(&self,model_id:usize)->&PhysicsModel{ @@ -343,11 +344,11 @@ struct Hitbox{ mesh:PhysicsMesh, transform:crate::integer::Planar64Affine3, normal_transform:Planar64Mat3, + transform_det:Planar64, } impl Hitbox{ fn new(mesh:PhysicsMesh,transform:crate::integer::Planar64Affine3)->Self{ //calculate extents - let normal_transform=transform.matrix3.inverse_times_det().transpose(); let mut aabb=crate::aabb::Aabb::default(); for vert in mesh.verts(){ aabb.grow(transform.transform_point3(vert)); @@ -356,23 +357,28 @@ impl Hitbox{ halfsize:aabb.size()/2, mesh, transform, - normal_transform, + normal_transform:transform.matrix3.inverse_times_det().transpose(), + transform_det:transform.matrix3.determinant(), } } fn from_mesh_scale(mesh:PhysicsMesh,scale:Planar64Vec3)->Self{ + let matrix3=Planar64Mat3::from_diagonal(scale); Self{ halfsize:scale, mesh, - transform:crate::integer::Planar64Affine3::new(Planar64Mat3::from_diagonal(scale),Planar64Vec3::ZERO), - normal_transform:Planar64Mat3::from_diagonal(scale).inverse_times_det().transpose(), + normal_transform:matrix3.inverse_times_det().transpose(), + transform:crate::integer::Planar64Affine3::new(matrix3,Planar64Vec3::ZERO), + transform_det:matrix3.determinant(),//scale.x*scale.y*scale.z but whatever } } fn from_mesh_scale_offset(mesh:PhysicsMesh,scale:Planar64Vec3,offset:Planar64Vec3)->Self{ + let matrix3=Planar64Mat3::from_diagonal(scale); Self{ halfsize:scale, mesh, - transform:crate::integer::Planar64Affine3::new(Planar64Mat3::from_diagonal(scale),offset), - normal_transform:Planar64Mat3::from_diagonal(scale).inverse_times_det().transpose(), + normal_transform:matrix3.inverse_times_det().transpose(), + transform:crate::integer::Planar64Affine3::new(matrix3,offset), + transform_det:matrix3.determinant(), } } fn roblox()->Self{ @@ -383,7 +389,7 @@ impl Hitbox{ } #[inline] fn transformed_mesh(&self)->TransformedMesh{ - TransformedMesh::new(&self.mesh,&self.transform,&self.normal_transform) + TransformedMesh::new(&self.mesh,&self.transform,&self.normal_transform,self.transform_det) } } @@ -779,16 +785,17 @@ pub struct PhysicsModel{ attr_id:usize, transform:crate::integer::Planar64Affine3, normal_transform:crate::integer::Planar64Mat3, + transform_det:Planar64, } impl PhysicsModel{ pub fn new(mesh_id:usize,attr_id:usize,transform:crate::integer::Planar64Affine3)->Self{ - let normal_transform=transform.matrix3.inverse_times_det().transpose(); Self{ mesh_id, attr_id, transform, - normal_transform, + normal_transform:transform.matrix3.inverse_times_det().transpose(), + transform_det:transform.matrix3.determinant(), } } }