Move cubes to [0.0, 1.0] to fix raycasting

This commit is contained in:
Sijmen 2021-05-30 02:08:31 +02:00
parent f0dace2266
commit b30ee9ec3b
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE
2 changed files with 32 additions and 36 deletions

View file

@ -60,31 +60,29 @@ impl Chunk {
}
pub fn raycast(&self, origin: Vector3<f32>, direction: Vector3<f32>) -> Option<Vector3<usize>> {
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<i32> = 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));
}
}

View file

@ -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]