aboutsummaryrefslogtreecommitdiff
path: root/azalea-physics/src
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-06-02 19:24:39 -1300
committermat <git@matdoes.dev>2025-06-03 02:41:25 -0600
commitcc3e64a3151398046408a7b97c339d32700cc541 (patch)
treef2ea5a27ae52b1dbb69997f61daf3a5f00369e94 /azalea-physics/src
parent61443fa481aeb6f9b488bf9475c512554a58473d (diff)
downloadazalea-drasl-cc3e64a3151398046408a7b97c339d32700cc541.tar.xz
fix collisions bugs
Diffstat (limited to 'azalea-physics/src')
-rw-r--r--azalea-physics/src/collision/shape.rs53
1 files changed, 36 insertions, 17 deletions
diff --git a/azalea-physics/src/collision/shape.rs b/azalea-physics/src/collision/shape.rs
index e27e4c2a..902ae20c 100644
--- a/azalea-physics/src/collision/shape.rs
+++ b/azalea-physics/src/collision/shape.rs
@@ -399,17 +399,8 @@ impl VoxelShape {
}
pub fn find_index(&self, axis: Axis, coord: f64) -> i32 {
- // let r = binary_search(0, (self.shape().size(axis) + 1) as i32, &|t| {
- // coord < self.get(axis, t as usize)
- // }) - 1;
- // r
- match self {
- VoxelShape::Cube(s) => s.find_index(axis, coord),
- _ => {
- let upper_limit = (self.shape().size(axis) + 1) as i32;
- binary_search(0, upper_limit, |t| coord < self.get(axis, t as usize)) - 1
- }
- }
+ let upper_limit = (self.shape().size(axis) + 1) as i32;
+ binary_search(0, upper_limit, |t| coord < self.get(axis, t as usize)) - 1
}
pub fn clip(&self, from: &Vec3, to: &Vec3, block_pos: &BlockPos) -> Option<BlockHitResult> {
@@ -420,7 +411,7 @@ impl VoxelShape {
if vector.length_squared() < EPSILON {
return None;
}
- let right_after_start = from + &(vector * 0.0001);
+ let right_after_start = from + &(vector * 0.001);
if self.shape().is_full_wide(
self.find_index(Axis::X, right_after_start.x - block_pos.x as f64),
@@ -645,7 +636,6 @@ impl ArrayVoxelShape {
impl CubeVoxelShape {
pub fn new(shape: DiscreteVoxelShape) -> Self {
- // pre-calculate the coor
let x_coords = Self::calculate_coords(&shape, Axis::X);
let y_coords = Self::calculate_coords(&shape, Axis::Y);
let z_coords = Self::calculate_coords(&shape, Axis::Z);
@@ -679,10 +669,11 @@ impl CubeVoxelShape {
axis.choose(&self.x_coords, &self.y_coords, &self.z_coords)
}
- fn find_index(&self, axis: Axis, coord: f64) -> i32 {
- let n = self.shape().size(axis);
- (f64::clamp(coord * (n as f64), -1f64, n as f64)) as i32
- }
+ // unused
+ // fn find_index(&self, axis: Axis, coord: f64) -> i32 {
+ // let n = self.shape().size(axis);
+ // (f64::clamp(coord * (n as f64), -1f64, n as f64)) as i32
+ // }
}
#[derive(Debug)]
@@ -752,4 +743,32 @@ mod tests {
let joined = Shapes::matches_anywhere(&shape, &shape2, |a, b| a && b);
assert!(joined, "Shapes should intersect");
}
+
+ #[test]
+ fn clip_in_front_of_block() {
+ let block_shape = &*BLOCK_SHAPE;
+ let block_hit_result = block_shape
+ .clip(
+ &Vec3::new(-0.3, 0.5, 0.),
+ &Vec3::new(5.3, 0.5, 0.),
+ &BlockPos::new(0, 0, 0),
+ )
+ .unwrap();
+
+ assert_eq!(
+ block_hit_result,
+ BlockHitResult {
+ location: Vec3 {
+ x: 0.0,
+ y: 0.5,
+ z: 0.0
+ },
+ direction: Direction::West,
+ block_pos: BlockPos { x: 0, y: 0, z: 0 },
+ inside: false,
+ world_border: false,
+ miss: false
+ }
+ );
+ }
}