Skip to content

Commit

Permalink
Use ChunkLayout stored in Graph for collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
patowen committed Oct 18, 2023
1 parent 605ebb3 commit 5d25d37
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 0 additions & 2 deletions common/src/character_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use tracing::{error, warn};
use crate::{
graph::Graph,
graph_collision, math,
node::ChunkLayout,
proto::{CharacterInput, Position},
sanitize_motion_input, SimConfig,
};
Expand Down Expand Up @@ -142,7 +141,6 @@ impl CharacterControllerPass<'_> {
let cast_hit = graph_collision::sphere_cast(
self.cfg.character_radius,
self.graph,
&ChunkLayout::new(self.cfg.chunk_size as usize),
self.position,
&ray,
tanh_distance,
Expand Down
5 changes: 5 additions & 0 deletions common/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ impl Graph {
}
}

#[inline]
pub fn layout(&self) -> &ChunkLayout {
&self.layout
}

#[inline]
pub fn len(&self) -> u32 {
self.nodes.len() as u32
Expand Down
22 changes: 9 additions & 13 deletions common/src/graph_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
dodeca::{self, Vertex},
graph::Graph,
math,
node::{Chunk, ChunkId, ChunkLayout},
node::{Chunk, ChunkId},
proto::Position,
};

Expand All @@ -23,7 +23,6 @@ use crate::{
pub fn sphere_cast(
collider_radius: f32,
graph: &Graph,
layout: &ChunkLayout,
position: &Position,
ray: &Ray,
tanh_distance: f32,
Expand Down Expand Up @@ -74,7 +73,7 @@ pub fn sphere_cast(
hit = chunk_sphere_cast(
collider_radius,
voxel_data,
layout,
graph.layout(),
&local_ray,
current_tanh_distance,
)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -289,19 +287,20 @@ mod tests {
|transform: na::Matrix4<f32>, side| transform * side.reflection().cast::<f32>(),
) * 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;
Expand All @@ -320,7 +319,6 @@ mod tests {
let hit = sphere_cast(
self.collider_radius,
&graph,
&layout,
&Position::origin(),
&ray,
tanh_distance,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -565,7 +562,6 @@ mod tests {
let hit = sphere_cast(
sphere_radius,
&graph,
&layout,
&Position::origin(),
&ray,
distance.tanh(),
Expand Down

0 comments on commit 5d25d37

Please sign in to comment.