Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Fix issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubelotix committed Nov 13, 2023
1 parent aeb7963 commit f34d1f9
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions minecraft-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,15 @@ fn is_inside(shape: &CollisionShape, point: Point) -> bool {
}

fn translation_limit_y(shape: &CollisionShape, translation: &Translation, point: &Point) -> Option<f32> {
if translation.y == 0.0 {
return None;
}
let y = if translation.y < 0.0 { shape.y1 } else { shape.y2 };
let translated_ratio = (point.y - y) / translation.y;
if !(0.0..=1.0).contains(&translated_ratio) {
if translated_ratio >= 1.0 {
return None;
} else if translated_ratio <= 0.0 {
return Some(0.0)
}
let translated_x1 = shape.x1 + translation.x * translated_ratio;
let translated_x2 = shape.x2 + translation.x * translated_ratio;
Expand All @@ -146,10 +151,15 @@ fn translation_limit_y(shape: &CollisionShape, translation: &Translation, point:
}

fn translation_limit_x(shape: &CollisionShape, translation: &Translation, point: &Point) -> Option<f32> {
if translation.x == 0.0 {
return None;
}
let x = if translation.x < 0.0 { shape.x1 } else { shape.x2 };
let translated_ratio = (point.x - x) / translation.x;
if !(0.0..=1.0).contains(&translated_ratio) {
if translated_ratio >= 1.0 {
return None;
} else if translated_ratio <= 0.0 {
return Some(0.0)
}
let translated_y1 = shape.y1 + translation.y * translated_ratio;
let translated_y2 = shape.y2 + translation.y * translated_ratio;
Expand All @@ -163,10 +173,15 @@ fn translation_limit_x(shape: &CollisionShape, translation: &Translation, point:
}

fn translation_limit_z(shape: &CollisionShape, translation: &Translation, point: &Point) -> Option<f32> {
if translation.z == 0.0 {
return None;
}
let z = if translation.z < 0.0 { shape.z1 } else { shape.z2 };
let translated_ratio = (point.z - z) / translation.z;
if !(0.0..=1.0).contains(&translated_ratio) {
if translated_ratio >= 1.0 {
return None;
} else if translated_ratio <= 0.0 {
return Some(0.0)
}
let translated_x1 = shape.x1 + translation.x * translated_ratio;
let translated_x2 = shape.x2 + translation.x * translated_ratio;
Expand All @@ -191,7 +206,7 @@ fn collide(translating: &CollisionShape, translation: &Translation, obstacle: &C
let mut limit = None;

for point in obstacle.points() {
limit = min_options2(limit, translation_limit(&translating, &translation, &point));
limit = min_options2(limit, translation_limit(translating, translation, &point));
if limit.map(|l| l==0.0).unwrap_or(false) {
break;
}
Expand Down Expand Up @@ -251,9 +266,9 @@ fn test() {
assert_eq!(collide(&shape2, &translation, &shape1), Some(Translation { x: -1.0, y: 0.0, z: 0.0 }));

// Colliding when already inside
//let shape2 = shape1.clone() + Translation { x: 0.5, y: 0.5, z: 0.5 };
//let translation = Translation { x: -0.5, y: -0.5, z: -0.5 };
//assert_eq!(collide(&shape2, &translation, &shape1), Some(Translation { x: 0.0, y: 0.0, z: 0.0 }));
let shape2 = shape1.clone() + Translation { x: 0.5, y: 0.5, z: 0.5 };
let translation = Translation { x: -0.5, y: -0.5, z: -0.5 };
assert_eq!(collide(&shape2, &translation, &shape1), Some(Translation { x: 0.0, y: 0.0, z: 0.0 }));
}

fn ray_cast(position: (f32, f32, f32), movement: (f32, f32, f32)) -> Vec<(isize, isize, isize)> {
Expand Down

0 comments on commit f34d1f9

Please sign in to comment.