From 86b614865c46d987ca6d15cb07f92b1b66b6262d Mon Sep 17 00:00:00 2001 From: Patrick Owen Date: Sat, 14 Oct 2023 22:10:46 -0400 Subject: [PATCH] Use ChunkLayout stored in Graph for collisions --- common/src/character_controller/collision.rs | 4 +--- common/src/character_controller/mod.rs | 2 -- common/src/graph.rs | 5 +++++ common/src/graph_collision.rs | 22 ++++++++------------ 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/common/src/character_controller/collision.rs b/common/src/character_controller/collision.rs index 61795959..3ac3bb63 100644 --- a/common/src/character_controller/collision.rs +++ b/common/src/character_controller/collision.rs @@ -2,7 +2,7 @@ use tracing::error; -use crate::{graph::Graph, graph_collision, math, node::ChunkLayout, proto::Position}; +use crate::{graph::Graph, graph_collision, math, proto::Position}; /// Checks for collisions when a character moves with a character-relative displacement vector of `relative_displacement`. pub fn check_collision( @@ -28,7 +28,6 @@ pub fn check_collision( let cast_hit = graph_collision::sphere_cast( collision_context.radius, collision_context.graph, - &collision_context.chunk_layout, position, &ray, tanh_distance, @@ -68,7 +67,6 @@ pub fn check_collision( /// Contains information about the character and the world that is only relevant for collision checking pub struct CollisionContext<'a> { pub graph: &'a Graph, - pub chunk_layout: ChunkLayout, pub radius: f32, } diff --git a/common/src/character_controller/mod.rs b/common/src/character_controller/mod.rs index 3756d076..9b56098f 100644 --- a/common/src/character_controller/mod.rs +++ b/common/src/character_controller/mod.rs @@ -12,7 +12,6 @@ use crate::{ }, graph::Graph, math, - node::ChunkLayout, proto::{CharacterInput, Position}, sanitize_motion_input, sim_config::CharacterConfig, @@ -33,7 +32,6 @@ pub fn run_character_step( cfg: &sim_config.character, collision_context: CollisionContext { graph, - chunk_layout: ChunkLayout::new(sim_config.chunk_size as usize), radius: sim_config.character.character_radius, }, up: graph.get_relative_up(position).unwrap(), diff --git a/common/src/graph.rs b/common/src/graph.rs index 8a6db2a7..7a84523b 100644 --- a/common/src/graph.rs +++ b/common/src/graph.rs @@ -32,6 +32,11 @@ impl Graph { } } + #[inline] + pub fn layout(&self) -> &ChunkLayout { + &self.layout + } + #[inline] pub fn len(&self) -> u32 { self.nodes.len() as u32 diff --git a/common/src/graph_collision.rs b/common/src/graph_collision.rs index e3407fa9..fc6cb0f0 100644 --- a/common/src/graph_collision.rs +++ b/common/src/graph_collision.rs @@ -7,7 +7,7 @@ use crate::{ dodeca::{self, Vertex}, graph::Graph, math, - node::{Chunk, ChunkId, ChunkLayout}, + node::{Chunk, ChunkId}, proto::Position, }; @@ -23,7 +23,6 @@ use crate::{ pub fn sphere_cast( collider_radius: f32, graph: &Graph, - layout: &ChunkLayout, position: &Position, ray: &Ray, tanh_distance: f32, @@ -74,7 +73,7 @@ pub fn sphere_cast( hit = chunk_sphere_cast( collider_radius, voxel_data, - layout, + graph.layout(), &local_ray, current_tanh_distance, ) @@ -260,7 +259,6 @@ mod tests { impl SphereCastExampleTestCase<'_> { fn execute(self) { let dimension: usize = 12; - let layout = ChunkLayout::new(dimension); let mut graph = Graph::new(dimension); let graph_radius = 3.0; @@ -289,19 +287,20 @@ mod tests { |transform: na::Matrix4, side| transform * side.reflection().cast::(), ) * self.chosen_voxel.vertex.dual_to_node().cast(); + let dual_to_grid_factor = graph.layout().dual_to_grid_factor(); let ray_target = chosen_chunk_transform * math::lorentz_normalize(&na::Vector4::new( - self.chosen_chunk_relative_grid_ray_end[0] / layout.dual_to_grid_factor(), - self.chosen_chunk_relative_grid_ray_end[1] / layout.dual_to_grid_factor(), - self.chosen_chunk_relative_grid_ray_end[2] / layout.dual_to_grid_factor(), + self.chosen_chunk_relative_grid_ray_end[0] / dual_to_grid_factor, + self.chosen_chunk_relative_grid_ray_end[1] / dual_to_grid_factor, + self.chosen_chunk_relative_grid_ray_end[2] / dual_to_grid_factor, 1.0, )); let ray_position = Vertex::A.dual_to_node().cast() * math::lorentz_normalize(&na::Vector4::new( - self.start_chunk_relative_grid_ray_start[0] / layout.dual_to_grid_factor(), - self.start_chunk_relative_grid_ray_start[1] / layout.dual_to_grid_factor(), - self.start_chunk_relative_grid_ray_start[2] / layout.dual_to_grid_factor(), + self.start_chunk_relative_grid_ray_start[0] / dual_to_grid_factor, + self.start_chunk_relative_grid_ray_start[1] / dual_to_grid_factor, + self.start_chunk_relative_grid_ray_start[2] / dual_to_grid_factor, 1.0, )); let ray_direction = ray_target - ray_position; @@ -320,7 +319,6 @@ mod tests { let hit = sphere_cast( self.collider_radius, &graph, - &layout, &Position::origin(), &ray, tanh_distance, @@ -513,7 +511,6 @@ mod tests { #[test] fn sphere_cast_near_unloaded_chunk() { let dimension: usize = 12; - let layout = ChunkLayout::new(dimension); let mut graph = Graph::new(dimension); let sides = Vertex::A.canonical_sides(); @@ -565,7 +562,6 @@ mod tests { let hit = sphere_cast( sphere_radius, &graph, - &layout, &Position::origin(), &ray, distance.tanh(),