From b1075e661c2ce2f107350b8d4f5f9a92e047b93e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sun, 14 Feb 2021 20:55:16 +0100 Subject: [PATCH] Only collide with walls from the front --- src/core/level/mod.rs | 19 ++++++++++++++----- src/geometry.rs | 4 ++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index 4d828de..bdc6cb0 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -135,11 +135,13 @@ impl Level { for c in self.wall_grid.grid_coordinates_on_line(p1, p2) { for w in &self.wall_grid.cells[c.x][c.y] { if let Intersection::Point(p) = Intersection::lines(p1, p2, w.p1, w.p2) { - let wall = Wall { - region: Rc::clone(&self.walls[w.region]), - edge: Rc::clone(w), - }; - return IntersectResult::Intersection(wall, p) + if w.point_is_in_front(p1) { + let wall = Wall { + region: Rc::clone(&self.walls[w.region]), + edge: Rc::clone(w), + }; + return IntersectResult::Intersection(wall, p) + } } } } @@ -243,6 +245,13 @@ struct WallEdge { pub p2: Point, } +impl WallEdge { + fn point_is_in_front(&self, p: Point) -> bool { + let cross = (self.p2 - self.p1).cross_product(p - self.p1); + cross > 0.0 + } +} + ////////// WALL //////////////////////////////////////////////////////////////// pub struct Wall { diff --git a/src/geometry.rs b/src/geometry.rs index 2d5a70c..7ddc3ec 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -38,6 +38,10 @@ impl Point { y: self.y as i32, } } + + pub fn cross_product(&self, p: Self) -> f64 { + return self.x * p.y - self.y * p.x; + } } macro_rules! impl_point_op { -- 2.11.0