From fc91d644e65d63e683171378588174deb9bff799 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 5 Oct 2023 23:52:04 -0700 Subject: [PATCH] use bvh --- src/physics.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/physics.rs b/src/physics.rs index 45bc4f7c..70810728 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -297,6 +297,7 @@ pub struct PhysicsState{ pub grounded:bool, //all models pub models:Vec, + pub bvh:crate::bvh::BvhNode, pub modes:Vec, pub mode_from_mode_id:std::collections::HashMap::, @@ -428,6 +429,7 @@ impl Default for PhysicsState{ contacts: std::collections::HashMap::new(), intersects: std::collections::HashMap::new(), models: Vec::new(), + bvh:crate::bvh::BvhNode::default(), walk: WalkState::new(), camera: PhysicsCamera::from_offset(glam::vec3(0.0,4.5-2.5,0.0)), next_mouse: MouseState::default(), @@ -564,6 +566,7 @@ impl PhysicsState { } } } + self.bvh=crate::bvh::generate_bvh(self.models.iter().map(|m|m.mesh().clone()).collect()); //I don't wanna write structs for temporary structures //this code builds ModeDescriptions from the unsorted lists at the top of the function starts.sort_by_key(|tup|tup.0); @@ -1036,13 +1039,15 @@ impl crate::instruction::InstructionEmitter for PhysicsState // collector.collect(self.predict_collision_end2(self.time,time_limit,collision_data)); // } //check for collision start instructions (against every part in the game with no optimization!!) - for i in 0..self.models.len() { - let i=i as u32; - if self.contacts.contains_key(&i)||self.intersects.contains_key(&i){ - continue; + let mut aabb=crate::aabb::Aabb::new(); + aabb.grow(self.body.extrapolated_position(self.time)); + aabb.grow(self.body.extrapolated_position(time_limit)); + aabb.inflate(self.style.hitbox_halfsize); + self.bvh.the_tester(&aabb,&mut |id|{ + if !(self.contacts.contains_key(&id)||self.intersects.contains_key(&id)){ + collector.collect(self.predict_collision_start(self.time,time_limit,id)); } - collector.collect(self.predict_collision_start(self.time,time_limit,i)); - } + }); if self.grounded { //walk maintenance collector.collect(self.next_walk_instruction());