Преглед на файлове

Adding quad-collision detection, and triangle collision detection.

Sam Jaffe преди 6 години
родител
ревизия
d5a5eaa43d
променени са 2 файла, в които са добавени 27 реда и са изтрити 1 реда
  1. 4 0
      math/include/game/math/shape.hpp
  2. 23 1
      math/src/common.cpp

+ 4 - 0
math/include/game/math/shape.hpp

@@ -25,6 +25,10 @@ namespace math { namespace dim2 {
     float radius;
   };
 
+  struct triangle {
+    point a, b, c;
+  };
+
   struct quad {
     point ll, lr, ur, ul;
   };

+ 23 - 1
math/src/common.cpp

@@ -109,7 +109,29 @@ namespace math {
            contains(lhs, rhs.center);
   }
 
-  bool intersects(dim2::quad const & lhs, dim2::quad const & rhs);
+  bool check_edges(dim2::line const & seg, dim2::triangle const & tri) {
+    return orient(seg, tri.a) == clockwise && orient(seg, tri.b) == clockwise &&
+           orient(seg, tri.c) == clockwise;
+  }
+
+  bool intersects(dim2::triangle const & lhs, dim2::triangle const & rhs) {
+    if (check_edges({lhs.a, lhs.b}, rhs)) return false;
+    if (check_edges({lhs.b, lhs.c}, rhs)) return false;
+    if (check_edges({lhs.c, lhs.a}, rhs)) return false;
+    if (check_edges({rhs.a, rhs.b}, lhs)) return false;
+    if (check_edges({rhs.b, rhs.c}, lhs)) return false;
+    if (check_edges({rhs.c, rhs.a}, lhs)) return false;
+    return true;
+  }
+
+  bool intersects(dim2::quad const & lhs, dim2::quad const & rhs) {
+    dim2::triangle l1{lhs.ll, lhs.lr, lhs.ul};
+    dim2::triangle l2{lhs.ul, lhs.ur, lhs.ll};
+    dim2::triangle r1{rhs.ll, rhs.lr, rhs.ul};
+    dim2::triangle r2{rhs.ul, rhs.ur, rhs.ll};
+    return intersects(l1, r1) || intersects(l2, r2) || intersects(l1, r2) ||
+           intersects(l2, r1);
+  }
 
   bool intersects(dim2::circle const & lhs, dim2::circle const & rhs) {
     vec2 const delta = rhs.center - lhs.center;