Only collide with walls from the front
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 14 Feb 2021 19:55:16 +0000 (20:55 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 14 Feb 2021 19:55:16 +0000 (20:55 +0100)
src/core/level/mod.rs
src/geometry.rs

index 4d828de..bdc6cb0 100644 (file)
@@ -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<f64>,
 }
 
+impl WallEdge {
+    fn point_is_in_front(&self, p: Point<f64>) -> bool {
+       let cross = (self.p2 - self.p1).cross_product(p - self.p1);
+       cross > 0.0
+    }
+}
+
 ////////// WALL ////////////////////////////////////////////////////////////////
 
 pub struct Wall {
index 2d5a70c..7ddc3ec 100644 (file)
@@ -38,6 +38,10 @@ impl Point<f64> {
            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 {