Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
improvement on breaking / placing blocks, need to figure out placing,…
Browse files Browse the repository at this point in the history
… and raycasting with more accuracy
  • Loading branch information
Adamkob12 committed Sep 25, 2023
1 parent 466aeb9 commit 55e9372
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 37 deletions.
85 changes: 54 additions & 31 deletions src/add_break_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::{one_d_cords, three_d_cords, *};
use bevy::prelude::*;
use bevy_meshem::prelude::*;

const RAY_FORWARD_STEP: f32 = 0.1;
const RAY_FORWARD_STEP: f32 = 0.03;
const NANO_STEP_FACTOR: f32 = 15.0;
const REACH_DISTANCE: u8 = 4;

#[derive(Event)]
Expand All @@ -25,7 +26,6 @@ pub fn add_break_detector(
let pos = tran.translation;

if buttons.just_pressed(MouseButton::Left) {
println!("Heya");
block_change_event_writer.send(BlockChange {
blocks: blocks_in_the_way(pos, forward, REACH_DISTANCE)
.iter()
Expand All @@ -39,10 +39,11 @@ pub fn add_break_detector(
change: VoxelChange::Added,
blocks: blocks_in_the_way(pos, forward, REACH_DISTANCE)
.iter()
.rev()
.map(|&(x, y, z)| {
let tmp = one_d_cords(y, CHUNK_DIMS);
if let Some(block) = get_neighbor(tmp, z, CHUNK_DIMS) {
dbg!((x, block));
// dbg!((x, block));
(x, block)
} else {
match z {
Expand All @@ -66,6 +67,8 @@ pub fn add_break_detector(
}

fn blocks_in_the_way(pos: Vec3, forward: Vec3, distance: u8) -> Vec<([i32; 2], [usize; 3], Face)> {
println!("\n----\nposition: {}", pos);
println!("forward vector: {}", forward);
let step = forward * RAY_FORWARD_STEP;
let mut point = pos;
let mut current_block = [
Expand All @@ -83,12 +86,13 @@ fn blocks_in_the_way(pos: Vec3, forward: Vec3, distance: u8) -> Vec<([i32; 2], [
point.z.floor() + 0.5,
];
if tmp != current_block {
println!("Encountered block {:?}", tmp);
current_block = tmp;
let face = {
let mut r: Face = Top;
let mut p = point - step;
let nano_step = step / 20.0;
for _ in 1..21 {
let nano_step = step / NANO_STEP_FACTOR;
for _ in 1..NANO_STEP_FACTOR as usize {
p += nano_step;
let tmp = [p.x.floor() + 0.5, p.y.floor() + 0.5, p.z.floor() + 0.5];
if tmp == current_block {
Expand All @@ -99,38 +103,57 @@ fn blocks_in_the_way(pos: Vec3, forward: Vec3, distance: u8) -> Vec<([i32; 2], [
r
};
let block_pos = position_to_chunk_position(point, CHUNK_DIMS);
dbg!(block_pos, face as usize);
to_return.push((block_pos.0, block_pos.1, face));
}
}
to_return
}

//
// fn closest_face(p: Vec3) -> Face {
// let mut min = f32::MAX;
// let mut face = Top;
//
// if (p.x.floor() - p.x).abs() < min {
// min = (p.x.floor() - p.x).abs();
// face = Left;
// }
// if (p.x.ceil() - p.x).abs() < min {
// min = (p.x.ceil() - p.x).abs();
// face = Right;
// }
// if (p.z.floor() - p.z).abs() < min {
// min = (p.z.floor() - p.z).abs();
// face = Forward;
// }
// if (p.z.ceil() - p.z).abs() < min {
// min = (p.z.ceil() - p.z).abs();
// face = Back;
// }
// if (p.y.floor() - p.y).abs() < min {
// min = (p.y.floor() - p.y).abs();
// face = Bottom;
// }
// if (p.y.ceil() - p.y).abs() < min {
// face = Top;
// }
// return face;
// }
fn closest_face(p: Vec3) -> Face {
let mut min = f32::MAX;
let mut face = Top;
let faces = [
((p.x.floor() - p.x).abs(), Face::Left),
((p.x.ceil() - p.x).abs(), Face::Right),
((p.y.floor() - p.y).abs(), Face::Bottom),
((p.y.ceil() - p.y).abs(), Face::Top),
((p.z.floor() - p.z).abs(), Face::Forward),
((p.z.ceil() - p.z).abs(), Face::Back),
];

if (p.x.floor() - p.x).abs() < min {
min = (p.x.floor() - p.x).abs();
face = Left;
}
if (p.x.ceil() - p.x).abs() < min {
min = (p.x.ceil() - p.x).abs();
face = Right;
}
if (p.z.floor() - p.z).abs() < min {
min = (p.z.floor() - p.z).abs();
face = Forward;
}
if (p.z.ceil() - p.z).abs() < min {
min = (p.z.ceil() - p.z).abs();
face = Back;
}
if (p.y.floor() - p.y).abs() < min {
min = (p.y.floor() - p.y).abs();
face = Bottom;
}
if (p.y.ceil() - p.y).abs() < min {
face = Top;
}
return face;
faces
.iter()
.cloned()
.min_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal))
.map(|(_, face)| face)
.unwrap_or(Face::Top) // Default face if somehow comparison fails.
}
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,16 @@ fn handle_block_break_place(
mut chunk_query: Query<(Entity, &mut Chunk), With<ChunkCloseToPlayer>>,
mut commands: Commands,
) {
'outer: for event in block_change.iter() {
for &(chunk, block) in event.blocks.iter() {
for event in block_change.iter() {
'outer: for &(chunk, block) in event.blocks.iter() {
let ent = chunk_map.get_ent(chunk).expect(
"Chunk should be loaded into internal data structure `ChunkMap` but it isn't.",
);
for (e, mut c) in chunk_query.iter_mut() {
if e != ent {
continue;
}
println!("hey1");
assert_eq!(c.cords, chunk);
let tmp_neighbors: Vec<Option<Block>> = vec![None; 6];
let mut neighboring_voxels: [Option<Block>; 6] = [None; 6];
Expand All @@ -273,6 +274,7 @@ fn handle_block_break_place(
let vox = c.grid[block];

if vox == AIR && matches!(event.change, VoxelChange::Broken) {
println!("hey2");
break;
}
if vox != AIR && matches!(event.change, VoxelChange::Added) {
Expand All @@ -296,6 +298,7 @@ fn handle_block_break_place(
VoxelChange::Broken => c.grid[block] = AIR,
}

println!("hey3");
commands.entity(e).insert(ToUpdate);
break 'outer;
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub fn position_to_chunk_position(
chunk_dims: (usize, usize, usize),
) -> ([i32; 2], [usize; 3], bool) {
let chunk_width = chunk_dims.0;
let chunk_length = chunk_dims.1;
let chunk_height = chunk_dims.2;
let chunk_length = chunk_dims.2;
let chunk_height = chunk_dims.1;
let chunk = position_to_chunk(pos, chunk_dims);

let chunk_pos = [
Expand All @@ -41,12 +41,12 @@ pub fn three_d_cords(oned: usize, dims: (usize, usize, usize)) -> (usize, usize,
assert!(h < height, "Out of bounds to convert into 3d coordinate.");
assert!(l < length, "Out of bounds to convert into 3d coordinate.");

(w, l, h)
(w, h, l)
}

pub fn one_d_cords(threed: [usize; 3], dims: (usize, usize, usize)) -> usize {
assert!(threed[0] < dims.0, "3d coordinate out of dimension bounds.");
assert!(threed[1] < dims.1, "3d coordinate out of dimension bounds.");
assert!(threed[2] < dims.2, "3d coordinate out of dimension bounds.");
threed[2] * (dims.0 * dims.1) + threed[1] * dims.0 + threed[0]
threed[1] * (dims.0 * dims.1) + threed[2] * dims.0 + threed[0]
}

0 comments on commit 55e9372

Please sign in to comment.