+void IntersectionTest::testSpherePlane() {
+ const float delta = std::numeric_limits<float>::epsilon();
+
+ // unit sphere at origin
+ Sphere sphere{{ 0.0f, 0.0f, 0.0f }, 1.0f};
+ // horizontal plane at origin (the XZ plane)
+ Plane plane{{ 0.0f, 1.0f, 0.0f }, 0.0f };
+ float depth;
+ glm::vec3 normal;
+
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere at origin does not intersect plane at origin",
+ Intersection(sphere, plane, depth, normal)
+ );
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+ "bad penetration depth of sphere with plane",
+ 1.0f, depth, delta
+ );
+ // normal is actually undefined in this case, but either will work
+ CPPUNIT_ASSERT_MESSAGE(
+ "bad contact normal of sphere intersecting plane",
+ normal == glm::vec3(0.0f, 1.0f, 0.0f) || normal == glm::vec3(0.0f, -1.0f, 0.0f)
+ );
+
+ // center above, but still intersecting
+ sphere.origin.y = 0.5f;
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere does not intersect plane",
+ Intersection(sphere, plane, depth, normal)
+ );
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+ "bad penetration depth of sphere with plane",
+ 0.5f, depth, delta
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "bad contact normal of sphere intersecting plane",
+ glm::vec3(0.0f, 1.0f, 0.0f), normal
+ );
+
+ // center below, but still intersecting
+ sphere.origin.y = -0.5f;
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere does not intersect plane",
+ Intersection(sphere, plane, depth, normal)
+ );
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+ "bad penetration depth of sphere with plane",
+ 0.5f, depth, delta
+ );
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ "bad contact normal of sphere intersecting plane",
+ glm::vec3(0.0f, -1.0f, 0.0f), normal
+ );
+
+ // sphere completely above
+ sphere.origin.y = 1.5f;
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere above plane intersects",
+ !Intersection(sphere, plane, depth, normal)
+ );
+
+ // sphere completely below
+ sphere.origin.y = -1.5f;
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere below plane intersects",
+ !Intersection(sphere, plane, depth, normal)
+ );
+}
+
+void IntersectionTest::testSphereHalfSpace() {
+ const float delta = std::numeric_limits<float>::epsilon();
+
+ // unit sphere at origin
+ Sphere sphere{{ 0.0f, 0.0f, 0.0f }, 1.0f};
+ // horizontal plane at origin (the XZ plane)
+ Plane plane{{ 0.0f, 1.0f, 0.0f }, 0.0f };
+ float depth;
+
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere at origin does not intersect half space to origin",
+ Intersection(sphere, plane, depth)
+ );
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+ "bad penetration depth of sphere with half space",
+ 1.0f, depth, delta
+ );
+
+ sphere.origin.y = 0.5f;
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere does not intersect half space",
+ Intersection(sphere, plane, depth)
+ );
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+ "bad penetration depth of sphere with half space",
+ 0.5f, depth, delta
+ );
+
+ sphere.origin.y = -0.5f;
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere does not intersect half space",
+ Intersection(sphere, plane, depth)
+ );
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+ "bad penetration depth of sphere with half space",
+ 1.5f, depth, delta
+ );
+
+ sphere.origin.y = -1.5f;
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere inside half space does not intersect",
+ Intersection(sphere, plane, depth)
+ );
+ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+ "bad penetration depth of sphere with half space",
+ 2.5f, depth, delta
+ );
+
+ sphere.origin.y = 1.5f;
+ CPPUNIT_ASSERT_MESSAGE(
+ "sphere outside half space intersects",
+ !Intersection(sphere, plane, depth)
+ );
+}
+