diff --git a/src/model_physics.rs b/src/model_physics.rs index 8ea1400..53cebbc 100644 --- a/src/model_physics.rs +++ b/src/model_physics.rs @@ -368,46 +368,33 @@ impl MeshQuery for PhysicsMes } } -pub struct PhysicsMeshTransform<'a>{ - vertex:&'a integer::Planar64Affine3, - normal:&'a integer::Planar64Mat3, +pub struct PhysicsMeshTransform{ + vertex:integer::Planar64Affine3, + normal:integer::Planar64Mat3, det:Planar64, } -impl PhysicsMeshTransform<'_>{ - pub fn new<'a>( - vertex:&'a integer::Planar64Affine3, - normal:&'a integer::Planar64Mat3, - det:Planar64 - )->PhysicsMeshTransform<'a>{ - PhysicsMeshTransform{ - vertex, - normal, - det, +impl PhysicsMeshTransform{ + pub const fn new(transform:integer::Planar64Affine3)->Self{ + Self{ + normal:transform.matrix3.inverse_times_det().transpose(), + det:transform.matrix3.determinant(), + vertex:transform, } } } + pub struct TransformedMesh<'a>{ view:PhysicsMeshView<'a>, - transform:PhysicsMeshTransform<'a>, + transform:&'a PhysicsMeshTransform, } impl TransformedMesh<'_>{ pub fn new<'a>( - mesh_data:&'a PhysicsMeshData, - topology:&'a PhysicsMeshTopology, - vertex:&'a integer::Planar64Affine3, - normal:&'a integer::Planar64Mat3, - det:Planar64, - )->TransformedMesh<'a>{ + view:PhysicsMeshView<'a>, + transform:&'a PhysicsMeshTransform, + )->TransformedMesh<'a>{ TransformedMesh{ - view:PhysicsMeshView{ - data: mesh_data, - topology, - }, - transform:PhysicsMeshTransform{ - vertex, - normal, - det, - } + view, + transform, } } fn farthest_vert(&self,dir:Planar64Vec3)->SubmeshVertId{ @@ -429,7 +416,7 @@ impl TransformedMesh<'_>{ impl MeshQuery for TransformedMesh<'_>{ fn face_nd(&self,face_id:SubmeshFaceId)->(Planar64Vec3,Planar64){ let (n,d)=self.view.face_nd(face_id); - let transformed_n=*self.transform.normal*n; + let transformed_n=self.transform.normal*n; let transformed_d=d+transformed_n.dot(self.transform.vertex.translation)/self.transform.det; (transformed_n/self.transform.det,transformed_d) } diff --git a/src/physics.rs b/src/physics.rs index c876ef4..18a8b49 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::collections::HashSet; -use crate::model_physics::{self,PhysicsMesh,TransformedMesh,MeshQuery,PhysicsMeshId,PhysicsSubmeshId}; +use crate::model_physics::{self,PhysicsMesh,PhysicsMeshTransform,TransformedMesh,MeshQuery,PhysicsMeshId,PhysicsSubmeshId}; use strafesnet_common::bvh; use strafesnet_common::map; use strafesnet_common::aabb; @@ -172,10 +172,8 @@ impl PhysicsModels{ fn mesh(&self,convex_mesh_id:ConvexMeshId)->TransformedMesh{ let model_idx=convex_mesh_id.model_id.get() as usize; TransformedMesh::new( - &self.meshes[model_idx].submesh_view(convex_mesh_id.submesh_id), - &self.models[idx].transform, - &self.models[idx].normal_transform, - self.models[idx].transform_det, + self.meshes[model_idx].submesh_view(convex_mesh_id.submesh_id), + &self.models[model_idx].transform ) } fn model(&self,model_id:PhysicsModelId)->&PhysicsModel{ @@ -284,24 +282,24 @@ struct WorldState{} struct HitboxMesh{ halfsize:Planar64Vec3, mesh:PhysicsMesh, - transform:PhysicsModelTransform, + transform:PhysicsMeshTransform, } impl HitboxMesh{ - fn new(mesh:PhysicsMesh,vertex_transform:integer::Planar64Affine3)->Self{ + fn new(mesh:PhysicsMesh,transform:integer::Planar64Affine3)->Self{ //calculate extents let mut aabb=aabb::Aabb::default(); for vert in mesh.complete_mesh_view().verts(){ - aabb.grow(vertex_transform.transform_point3(vert)); + aabb.grow(transform.transform_point3(vert)); } Self{ halfsize:aabb.size()/2, mesh, - transform:PhysicsModelTransform::new(vertex_transform) + transform:PhysicsMeshTransform::new(transform) } } #[inline] fn transformed_mesh(&self)->TransformedMesh{ - TransformedMesh::new(&self.mesh,&self.transform,&self.normal_transform,self.transform_det) + TransformedMesh::new(&self.mesh.complete_mesh_view(),&self.transform) } } @@ -495,38 +493,24 @@ impl From for PhysicsModelId{ Self::new(value.get()) } } -pub struct PhysicsModelTransform{ - vertex:integer::Planar64Affine3, - normal:integer::Planar64Mat3, - det:Planar64, -} -impl PhysicsModelTransform{ - pub const fn new(vertex_transform:integer::Planar64Affine3)->Self{ - Self{ - normal:vertex_transform.matrix3.inverse_times_det().transpose(), - det:vertex_transform.matrix3.determinant(), - vertex:vertex_transform, - } - } -} pub struct PhysicsModel{ //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. mesh_id:PhysicsMeshId, //put these up on the Model (data normalization) attr_id:PhysicsAttributesId, - transform:PhysicsModelTransform, + transform:PhysicsMeshTransform, } impl PhysicsModel{ - pub const fn new(mesh_id:PhysicsMeshId,attr_id:PhysicsAttributesId,transform:PhysicsModelTransform)->Self{ + pub const fn new(mesh_id:PhysicsMeshId,attr_id:PhysicsAttributesId,transform:PhysicsMeshTransform)->Self{ Self{ mesh_id, attr_id, transform, } } - const fn transform(&self)->&PhysicsModelTransform{ + const fn transform(&self)->&PhysicsMeshTransform{ &self.transform } }