Move cubes to [0.0, 1.0] to fix raycasting
This commit is contained in:
parent
f0dace2266
commit
b30ee9ec3b
2 changed files with 32 additions and 36 deletions
20
src/chunk.rs
20
src/chunk.rs
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
48
src/cube.rs
48
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]
|
||||
|
|
Loading…
Reference in a new issue