Explorar o código

Test quad collisions.

Sam Jaffe %!s(int64=6) %!d(string=hai) anos
pai
achega
8a52e2be49
Modificáronse 2 ficheiros con 37 adicións e 3 borrados
  1. 3 1
      math/src/common.cpp
  2. 34 2
      math/test/common_test.cxx

+ 3 - 1
math/src/common.cpp

@@ -127,7 +127,9 @@ namespace math {
     return true;
   }
 
-  bool intersects(dim2::quad const & lhs, dim2::quad const & rhs) {
+  // TODO (sjaffe): This does not cause intersection when the edges brush
+  //                against one another, is this good?
+ 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};

+ 34 - 2
math/test/common_test.cxx

@@ -261,14 +261,46 @@ TEST(TriangleTest, DoesNotIntersectOffset) {
   math::dim2::point const clock_bc{{-1, -1}};
   math::dim2::point const clock_ca{{-1, 2}};
 
+  // Each of these expectations fails one of the check-edges calls
+  // in the intersection algorithm, which proves that each point of
+  // the triangle is not within (or on) the bounds of the other triangle
+  // Each of these tests indicates one edge of the triangle 'lhs', and
+  // tests for whether it orients around any of the points of the other
+  // 'triangle'.
+  // Tests are done for each edge vs. the the opposite triangle,
+  // starting with edges for the lhs, and then the edges on the rhs.
   EXPECT_FALSE(math::intersects(lhs, tri(clock_ab)));
-  EXPECT_FALSE(math::intersects(tri(clock_ab), lhs));
   EXPECT_FALSE(math::intersects(lhs, tri(clock_bc)));
-  EXPECT_FALSE(math::intersects(tri(clock_bc), lhs));
   EXPECT_FALSE(math::intersects(lhs, tri(clock_ca)));
+  EXPECT_FALSE(math::intersects(tri(clock_ab), lhs));
+  EXPECT_FALSE(math::intersects(tri(clock_bc), lhs));
   EXPECT_FALSE(math::intersects(tri(clock_ca), lhs));
 }
 
+TEST(QuadTest, IntersectsContain) {
+  quad const lhs = square{{{-0.5f, -0.5f}}, 1};
+  quad const rhs = square{{{-.25f, -.25f}}, 0.5};
+  EXPECT_TRUE(math::intersects(lhs, rhs));
+}
+
+TEST(QuadTest, NoIntersectionAtEdge) {
+  quad const lhs = square{{{-0.5f, -0.5f}}, 1};
+  quad const rhs = square{{{ 0.0f,  0.5f}}, 1};
+  EXPECT_FALSE(math::intersects(lhs, rhs));
+}
+
+TEST(QuadTest, NoIntersectionAtCorner) {
+  quad const lhs = square{{{-0.5f, -0.5f}}, 1};
+  quad const rhs = square{{{ 0.5f,  0.5f}}, 1};
+  EXPECT_FALSE(math::intersects(lhs, rhs));
+}
+
+TEST(QuadTest, NoIntersection) {
+  quad const lhs = square{{{-1.0f, -0.5f}}, 1};
+  quad const rhs = square{{{ 0.5f,  0.5f}}, 1};
+  EXPECT_FALSE(math::intersects(lhs, rhs));
+}
+
 TEST(RotateTest, RotatingSquareAroundOrigin) {
   math::degree degrees{90.0};
   // A square with a side-length of 2 and a center at the origin