Skip to content

Commit

Permalink
Fix collision between fixed entities
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed Sep 11, 2024
1 parent ebe7c7b commit 52b9d34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 16 additions & 2 deletions src/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ pub(crate) fn resolve_collision(
}

if overlap_y > overlap_x {
let overlap_x = overlap_x.max(overlap_y * 0.5);
let overlap_x = if overlap_x == 0.0 {
let a_bounds = a.bounds();
let b_bounds = b.bounds();
((a_bounds.max.x - a_bounds.min.x).min(b_bounds.max.x - b_bounds.min.x) * 0.5)
.min(overlap_y * 0.5)
} else {
overlap_x
};
if a.pos.x < b.pos.x {
entities_separate_on_x_axis(eng, a, b, a_move, b_move, overlap_x);
eng.collide(a.ent_ref, Vec2::new(-1.0, 0.0), None);
Expand All @@ -84,7 +91,14 @@ pub(crate) fn resolve_collision(
eng.collide(b.ent_ref, Vec2::new(-1.0, 0.0), None);
}
} else if a.pos.y < b.pos.y {
let overlap_y = overlap_y.max(overlap_x * 0.5);
let overlap_y = if overlap_y == 0.0 {
let a_bounds = a.bounds();
let b_bounds = b.bounds();
((a_bounds.max.y - a_bounds.min.y).min(b_bounds.max.y - b_bounds.min.y) * 0.5)
.min(overlap_x * 0.5)
} else {
overlap_y
};
entities_separate_on_y_axis(eng, a, b, a_move, b_move, overlap_y, eng.tick);
eng.collide(a.ent_ref, Vec2::new(0.0, -1.0), None);
eng.collide(b.ent_ref, Vec2::new(0.0, 1.0), None);
Expand Down
8 changes: 4 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ pub struct Rect {

impl Rect {
pub(crate) fn is_touching(&self, other: &Self) -> bool {
!(self.min.x > other.max.x
|| self.max.x < other.min.x
|| self.min.y > other.max.y
|| self.max.y < other.min.y)
!(self.min.x >= other.max.x
|| self.max.x <= other.min.x
|| self.min.y >= other.max.y
|| self.max.y <= other.min.y)
}
}

Expand Down

0 comments on commit 52b9d34

Please sign in to comment.