]> git.localhorst.tv Git - blobs.git/blobdiff - tst/math/IntersectTest.cpp
use matrix translations in ray/sphere test
[blobs.git] / tst / math / IntersectTest.cpp
index cd5084574d8d3928fae03a50dd04f45314c47a04..904d682325d89915c8a08d042fa68d82879d75eb 100644 (file)
@@ -243,6 +243,35 @@ void IntersectTest::testRaySphereIntersection() {
                glm::dvec3(1.0, 0.0, 0.0), normal
        );
 
+       ray.Origin({ 0.5, 0.0, 0.0 }); // a tad to the right
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray does not intersect sphere at origin",
+               Intersect(ray, sphere, normal, dist)
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "distance along ray to unit sphere at origin is not 0.5",
+               0.5, dist, epsilon
+       );
+       AssertEqual(
+               "bad intersection normal",
+               glm::dvec3(1.0, 0.0, 0.0), normal
+       );
+
+       // corner case: ray origin exactly on sphere (should "intersect")
+       ray = glm::translate(glm::dvec3(0.5, 0, 0)) * ray;
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray touching sphere does not intersect it",
+               Intersect(ray, sphere, normal, dist)
+       );
+       CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+               "distance along ray touching unit sphere is not 0.0",
+               0.0, dist, epsilon
+       );
+       AssertEqual(
+               "bad intersection normal",
+               glm::dvec3(1.0, 0.0, 0.0), normal
+       );
+
        ray.Origin({ 2.0, 0.0, 0.0 }); // move outside
        CPPUNIT_ASSERT_MESSAGE(
                "ray pointing away from sphere intersects it for some reason",
@@ -267,6 +296,7 @@ void IntersectTest::testRaySphereIntersection() {
        // should be 5 units apart, minus one for the radius
        ray.Origin({ 0.0, 4.0, 0.0 });
        ray.Direction(glm::normalize(glm::dvec3(3.0, -4.0, 0.0)));
+       sphere = glm::translate(glm::dvec3(3.0, 0, 0)) * sphere;
        sphere.origin = { 3.0, 0.0, 0.0 };
        CPPUNIT_ASSERT_MESSAGE(
                "diagonal ray does not intersect sphere",