From b30ee9ec3b3b64e8b9a2af768bc989717d28af33 Mon Sep 17 00:00:00 2001 From: Vijfhoek Date: Sun, 30 May 2021 02:08:31 +0200 Subject: [PATCH] Move cubes to [0.0, 1.0] to fix raycasting --- src/chunk.rs | 20 ++++++++------------ src/cube.rs | 48 ++++++++++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/chunk.rs b/src/chunk.rs index 066f1b5..a27d0f3 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -60,31 +60,29 @@ impl Chunk { } pub fn raycast(&self, origin: Vector3, direction: Vector3) -> Option> { - let origin = origin.map(|field| field.trunc()); - let dir = direction.normalize(); - let scale = Vector3::new( Self::calc_scale(direction, direction.x), Self::calc_scale(direction, direction.y), Self::calc_scale(direction, direction.z), ); + let direction = direction.normalize(); let mut position = origin.map(|x| x as i32); - let step: Vector3 = dir.map(|x| x.signum() as i32); + let step = direction.map(|x| x.signum() as i32); // Truncate the origin let mut lengths = Vector3 { - x: if dir.x < 0.0 { + x: if direction.x < 0.0 { (origin.x - position.x as f32) * scale.x } else { (position.x as f32 + 1.0 - origin.x) * scale.x }, - y: if dir.y < 0.0 { + y: if direction.y < 0.0 { (origin.y - position.y as f32) * scale.y } else { (position.y as f32 + 1.0 - origin.y) * scale.y }, - z: if dir.z < 0.0 { + z: if direction.z < 0.0 { (origin.z - position.z as f32) * scale.z } else { (position.z as f32 + 1.0 - origin.z) * scale.z @@ -105,14 +103,12 @@ impl Chunk { return None; } - let block = self.get_block( + if let Some(_) = self.get_block( position.x as usize, position.y as usize, position.z as usize, - ); - - if let Some(_) = block { - // Intersected with a block, round position to coordinates and return it. + ) { + // Intersection occurred return Some(position.map(|x| x as usize)); } } diff --git a/src/cube.rs b/src/cube.rs index 8ca9b89..60aacb4 100644 --- a/src/cube.rs +++ b/src/cube.rs @@ -3,40 +3,40 @@ use crate::vertex::Vertex; #[rustfmt::skip] pub const VERTICES: &[Vertex] = &[ // Left - Vertex { position: [-0.5, -0.5, -0.5], texture_coordinates: [1.0, 1.0], normal: [-1.0, 0.0, 0.0] }, - Vertex { position: [-0.5, -0.5, 0.5], texture_coordinates: [0.0, 1.0], normal: [-1.0, 0.0, 0.0] }, - Vertex { position: [-0.5, 0.5, 0.5], texture_coordinates: [0.0, 0.0], normal: [-1.0, 0.0, 0.0] }, - Vertex { position: [-0.5, 0.5, -0.5], texture_coordinates: [1.0, 0.0], normal: [-1.0, 0.0, 0.0] }, + Vertex { position: [0.0, 0.0, 0.0], texture_coordinates: [1.0, 1.0], normal: [-1.0, 0.0, 0.0] }, + Vertex { position: [0.0, 0.0, 1.0], texture_coordinates: [0.0, 1.0], normal: [-1.0, 0.0, 0.0] }, + Vertex { position: [0.0, 1.0, 1.0], texture_coordinates: [0.0, 0.0], normal: [-1.0, 0.0, 0.0] }, + Vertex { position: [0.0, 1.0, 0.0], texture_coordinates: [1.0, 0.0], normal: [-1.0, 0.0, 0.0] }, // Right - Vertex { position: [ 0.5, -0.5, -0.5], texture_coordinates: [0.0, 1.0], normal: [ 1.0, 0.0, 0.0] }, - Vertex { position: [ 0.5, -0.5, 0.5], texture_coordinates: [1.0, 1.0], normal: [ 1.0, 0.0, 0.0] }, - Vertex { position: [ 0.5, 0.5, 0.5], texture_coordinates: [1.0, 0.0], normal: [ 1.0, 0.0, 0.0] }, - Vertex { position: [ 0.5, 0.5, -0.5], texture_coordinates: [0.0, 0.0], normal: [ 1.0, 0.0, 0.0] }, + Vertex { position: [1.0, 0.0, 0.0], texture_coordinates: [0.0, 1.0], normal: [ 1.0, 0.0, 0.0] }, + Vertex { position: [1.0, 0.0, 1.0], texture_coordinates: [1.0, 1.0], normal: [ 1.0, 0.0, 0.0] }, + Vertex { position: [1.0, 1.0, 1.0], texture_coordinates: [1.0, 0.0], normal: [ 1.0, 0.0, 0.0] }, + Vertex { position: [1.0, 1.0, 0.0], texture_coordinates: [0.0, 0.0], normal: [ 1.0, 0.0, 0.0] }, // Back - Vertex { position: [-0.5, -0.5, -0.5], texture_coordinates: [1.0, 1.0], normal: [ 0.0, 0.0, -1.0] }, - Vertex { position: [-0.5, 0.5, -0.5], texture_coordinates: [1.0, 0.0], normal: [ 0.0, 0.0, -1.0] }, - Vertex { position: [ 0.5, 0.5, -0.5], texture_coordinates: [0.0, 0.0], normal: [ 0.0, 0.0, -1.0] }, - Vertex { position: [ 0.5, -0.5, -0.5], texture_coordinates: [0.0, 1.0], normal: [ 0.0, 0.0, -1.0] }, + Vertex { position: [0.0, 0.0, 0.0], texture_coordinates: [1.0, 1.0], normal: [ 0.0, 0.0, -1.0] }, + Vertex { position: [0.0, 1.0, 0.0], texture_coordinates: [1.0, 0.0], normal: [ 0.0, 0.0, -1.0] }, + Vertex { position: [1.0, 1.0, 0.0], texture_coordinates: [0.0, 0.0], normal: [ 0.0, 0.0, -1.0] }, + Vertex { position: [1.0, 0.0, 0.0], texture_coordinates: [0.0, 1.0], normal: [ 0.0, 0.0, -1.0] }, // Front - Vertex { position: [-0.5, -0.5, 0.5], texture_coordinates: [0.0, 1.0], normal: [ 0.0, 0.0, 1.0] }, - Vertex { position: [-0.5, 0.5, 0.5], texture_coordinates: [0.0, 0.0], normal: [ 0.0, 0.0, 1.0] }, - Vertex { position: [ 0.5, 0.5, 0.5], texture_coordinates: [1.0, 0.0], normal: [ 0.0, 0.0, 1.0] }, - Vertex { position: [ 0.5, -0.5, 0.5], texture_coordinates: [1.0, 1.0], normal: [ 0.0, 0.0, 1.0] }, + Vertex { position: [0.0, 0.0, 1.0], texture_coordinates: [0.0, 1.0], normal: [ 0.0, 0.0, 1.0] }, + Vertex { position: [0.0, 1.0, 1.0], texture_coordinates: [0.0, 0.0], normal: [ 0.0, 0.0, 1.0] }, + Vertex { position: [1.0, 1.0, 1.0], texture_coordinates: [1.0, 0.0], normal: [ 0.0, 0.0, 1.0] }, + Vertex { position: [1.0, 0.0, 1.0], texture_coordinates: [1.0, 1.0], normal: [ 0.0, 0.0, 1.0] }, // Bottom - Vertex { position: [-0.5, -0.5, -0.5], texture_coordinates: [1.0, 0.0], normal: [ 0.0, -1.0, 0.0] }, - Vertex { position: [-0.5, -0.5, 0.5], texture_coordinates: [1.0, 1.0], normal: [ 0.0, -1.0, 0.0] }, - Vertex { position: [ 0.5, -0.5, 0.5], texture_coordinates: [0.0, 1.0], normal: [ 0.0, -1.0, 0.0] }, - Vertex { position: [ 0.5, -0.5, -0.5], texture_coordinates: [0.0, 0.0], normal: [ 0.0, -1.0, 0.0] }, + Vertex { position: [0.0, 0.0, 0.0], texture_coordinates: [1.0, 0.0], normal: [ 0.0, -1.0, 0.0] }, + Vertex { position: [0.0, 0.0, 1.0], texture_coordinates: [1.0, 1.0], normal: [ 0.0, -1.0, 0.0] }, + Vertex { position: [1.0, 0.0, 1.0], texture_coordinates: [0.0, 1.0], normal: [ 0.0, -1.0, 0.0] }, + Vertex { position: [1.0, 0.0, 0.0], texture_coordinates: [0.0, 0.0], normal: [ 0.0, -1.0, 0.0] }, // Top - Vertex { position: [-0.5, 0.5, -0.5], texture_coordinates: [0.0, 0.0], normal: [ 0.0, 1.0, 0.0] }, - Vertex { position: [-0.5, 0.5, 0.5], texture_coordinates: [0.0, 1.0], normal: [ 0.0, 1.0, 0.0] }, - Vertex { position: [ 0.5, 0.5, 0.5], texture_coordinates: [1.0, 1.0], normal: [ 0.0, 1.0, 0.0] }, - Vertex { position: [ 0.5, 0.5, -0.5], texture_coordinates: [1.0, 0.0], normal: [ 0.0, 1.0, 0.0] }, + Vertex { position: [0.0, 1.0, 0.0], texture_coordinates: [0.0, 0.0], normal: [ 0.0, 1.0, 0.0] }, + Vertex { position: [0.0, 1.0, 1.0], texture_coordinates: [0.0, 1.0], normal: [ 0.0, 1.0, 0.0] }, + Vertex { position: [1.0, 1.0, 1.0], texture_coordinates: [1.0, 1.0], normal: [ 0.0, 1.0, 0.0] }, + Vertex { position: [1.0, 1.0, 0.0], texture_coordinates: [1.0, 0.0], normal: [ 0.0, 1.0, 0.0] }, ]; #[rustfmt::skip]