forked from StrafesNET/strafe-client
calculate vertex extents for accurate mesh aabb hitboxes
This commit is contained in:
parent
ed712933e5
commit
ac4ba19ed3
26
src/body.rs
26
src/body.rs
@ -284,7 +284,7 @@ pub enum AabbFace{
|
|||||||
Bottom,
|
Bottom,
|
||||||
Front,
|
Front,
|
||||||
}
|
}
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Aabb {
|
pub struct Aabb {
|
||||||
min: glam::Vec3,
|
min: glam::Vec3,
|
||||||
max: glam::Vec3,
|
max: glam::Vec3,
|
||||||
@ -387,28 +387,30 @@ type TreyMesh = Aabb;
|
|||||||
pub struct ModelPhysics {
|
pub struct ModelPhysics {
|
||||||
//A model is a thing that has a hitbox. can be represented by a list of TreyMesh-es
|
//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.
|
//in this iteration, all it needs is extents.
|
||||||
model_transform: glam::Affine3A,
|
mesh: TreyMesh,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModelPhysics {
|
impl ModelPhysics {
|
||||||
pub fn new(model_transform:glam::Affine3A) -> Self {
|
pub fn from_model(model:&crate::model::IndexedModel,model_transform:glam::Affine3A) -> Self {
|
||||||
Self{model_transform}
|
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] {
|
pub fn unit_vertices(&self) -> [glam::Vec3;8] {
|
||||||
Aabb::unit_vertices()
|
Aabb::unit_vertices()
|
||||||
}
|
}
|
||||||
pub fn mesh(&self) -> TreyMesh {
|
pub fn mesh(&self) -> &TreyMesh {
|
||||||
let mut aabb=Aabb::new();
|
return &self.mesh;
|
||||||
for &vertex in self.unit_vertices().iter() {
|
|
||||||
aabb.grow(self.model_transform.transform_point3(vertex));
|
|
||||||
}
|
|
||||||
return aabb;
|
|
||||||
}
|
}
|
||||||
pub fn unit_face_vertices(&self,face:TreyMeshFace) -> [glam::Vec3;4] {
|
pub fn unit_face_vertices(&self,face:TreyMeshFace) -> [glam::Vec3;4] {
|
||||||
Aabb::unit_face_vertices(face)
|
Aabb::unit_face_vertices(face)
|
||||||
}
|
}
|
||||||
pub fn face_mesh(&self,face:TreyMeshFace) -> TreyMesh {
|
pub fn face_mesh(&self,face:TreyMeshFace) -> TreyMesh {
|
||||||
let mut aabb=self.mesh();
|
let mut aabb=self.mesh.clone();
|
||||||
//in this implementation face = worldspace aabb face
|
//in this implementation face = worldspace aabb face
|
||||||
match face {
|
match face {
|
||||||
AabbFace::Right => aabb.min.x=aabb.max.x,
|
AabbFace::Right => aabb.min.x=aabb.max.x,
|
||||||
@ -435,7 +437,7 @@ pub struct RelativeCollision {
|
|||||||
|
|
||||||
impl RelativeCollision {
|
impl RelativeCollision {
|
||||||
pub fn mesh(&self,models:&Vec<ModelPhysics>) -> TreyMesh {
|
pub fn mesh(&self,models:&Vec<ModelPhysics>) -> TreyMesh {
|
||||||
return models.get(self.model as usize).unwrap().face_mesh(self.face)
|
return models.get(self.model as usize).unwrap().face_mesh(self.face).clone()
|
||||||
}
|
}
|
||||||
pub fn normal(&self,models:&Vec<ModelPhysics>) -> glam::Vec3 {
|
pub fn normal(&self,models:&Vec<ModelPhysics>) -> glam::Vec3 {
|
||||||
return models.get(self.model as usize).unwrap().face_normal(self.face)
|
return models.get(self.model as usize).unwrap().face_normal(self.face)
|
||||||
|
10
src/main.rs
10
src/main.rs
@ -83,11 +83,13 @@ impl GraphicsData {
|
|||||||
depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
|
depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_model_physics(&mut self,modeldatas:&Vec<ModelData>){
|
fn generate_model_physics(&mut self,indexed_models:&model::IndexedModelInstances){
|
||||||
self.physics.models.append(&mut modeldatas.iter().map(|m|
|
for model in &indexed_models.models{
|
||||||
//make aabb and run vertices to get realistic bounds
|
//make aabb and run vertices to get realistic bounds
|
||||||
m.instances.iter().map(|t|body::ModelPhysics::new(t.model_transform))
|
for model_instance in &model.instances{
|
||||||
).flatten().collect());
|
self.physics.models.push(body::ModelPhysics::from_model(&model,model_instance.model_transform));
|
||||||
|
}
|
||||||
|
}
|
||||||
println!("Physics Objects: {}",self.physics.models.len());
|
println!("Physics Objects: {}",self.physics.models.len());
|
||||||
}
|
}
|
||||||
fn generate_model_graphics(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,mut indexed_models:model::IndexedModelInstances){
|
fn generate_model_graphics(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,mut indexed_models:model::IndexedModelInstances){
|
||||||
|
Loading…
Reference in New Issue
Block a user