Clean up DDA a bit

This commit is contained in:
Sijmen 2021-05-29 19:26:55 +02:00
parent 17fbd875e2
commit ff0fc6cdee
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE
2 changed files with 18 additions and 24 deletions

View file

@ -70,30 +70,26 @@ impl Chunk {
pub fn dda(&self, position: Vector3<f32>, direction: Vector3<f32>) -> Option<Vector3<usize>> {
assert!(f32::abs(direction.magnitude() - 1.0) < f32::EPSILON);
let scale_x = Self::calc_scale(direction, direction.x);
let scale_y = Self::calc_scale(direction, direction.y);
let scale_z = Self::calc_scale(direction, direction.z);
dbg!(direction, scale_x, scale_y, scale_z);
let scale = Vector3::new(
Self::calc_scale(direction, direction.x),
Self::calc_scale(direction, direction.y),
Self::calc_scale(direction, direction.z),
);
let mut new_position = position;
let mut x_length = 0.0;
let mut y_length = 0.0;
let mut z_length = 0.0;
let mut lengths = Vector3::new(0.0, 0.0, 0.0);
loop {
let new_x_length = x_length + scale_x;
let new_y_length = y_length + scale_y;
let new_z_length = z_length + scale_z;
let new_lengths = lengths + scale;
if new_x_length < f32::min(new_y_length, new_z_length) {
x_length = new_x_length;
new_position += direction * scale_x;
} else if new_y_length < f32::min(new_x_length, new_z_length) {
y_length = new_y_length;
new_position += direction * scale_y;
} else if new_z_length < f32::min(new_x_length, new_y_length) {
z_length = new_z_length;
new_position += direction * scale_z;
if new_lengths.x < f32::min(new_lengths.y, new_lengths.z) {
lengths.x = new_lengths.x;
new_position += direction * scale.x;
} else if new_lengths.y < f32::min(new_lengths.x, new_lengths.z) {
lengths.y = new_lengths.y;
new_position += direction * scale.y;
} else if new_lengths.z < f32::min(new_lengths.x, new_lengths.y) {
lengths.z = new_lengths.z;
new_position += direction * scale.z;
}
let pos_usize = new_position.map(|field| field.round() as usize);

View file

@ -428,13 +428,11 @@ impl State {
let (camera, projection) = Self::create_camera(&swap_chain_descriptor);
let dda_start = Instant::now();
let coll_pos = chunk
let pointy_at = chunk
.dda(camera.position.to_vec(), camera.direction())
.unwrap();
dbg!(dda_start.elapsed());
chunk.blocks[coll_pos.y][coll_pos.z][coll_pos.x] = Some(Block {
chunk.blocks[pointy_at.y][pointy_at.z][pointy_at.x] = Some(Block {
block_type: BlockType::Cobblestone,
});