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 25, 2023
1 parent 1450462 commit 1cbae73
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
4 changes: 1 addition & 3 deletions common/src/character_controller/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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,
}

Expand Down
2 changes: 0 additions & 2 deletions common/src/character_controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::{
},
graph::Graph,
math,
node::ChunkLayout,
proto::{CharacterInput, Position},
sanitize_motion_input,
sim_config::CharacterConfig,
Expand All @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions common/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 1cbae73

Please sign in to comment.