From: Daniel Karbach Date: Thu, 11 Jun 2015 13:29:35 +0000 (+0200) Subject: fix ray/obb intersection test X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=7d3ab0dcce7abb6d5f766315685424e82478212f;p=blank.git fix ray/obb intersection test again, unit tests \o/ also: careful with the signs ;) --- diff --git a/src/model/geometry.cpp b/src/model/geometry.cpp index 416d930..ed9b4b7 100644 --- a/src/model/geometry.cpp +++ b/src/model/geometry.cpp @@ -35,7 +35,7 @@ bool Intersection( return false; } } else { - if (aabb.min[i] - e < 0.0f || -aabb.max[i] - e > 0.0f) { + if (aabb.min[i] - e > 0.0f || aabb.max[i] - e < 0.0f) { return false; } } diff --git a/tst/model/GeometryTest.cpp b/tst/model/GeometryTest.cpp new file mode 100644 index 0000000..3776da1 --- /dev/null +++ b/tst/model/GeometryTest.cpp @@ -0,0 +1,41 @@ +#include "GeometryTest.hpp" + +#include "model/geometry.hpp" + +#include + +CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::GeometryTest); + + +namespace blank { +namespace test { + +void GeometryTest::setUp() { +} + +void GeometryTest::tearDown() { +} + + +void GeometryTest::testRayAABBIntersection() { + Ray ray{ { 0, 0, 0 }, { 1, 0, 0 } }; // at origin, pointing right + AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin + glm::mat4 M(1); // no transformation + + const float delta = std::numeric_limits::epsilon(); + + float distance = 0; + glm::vec3 normal(0); + + CPPUNIT_ASSERT_MESSAGE( + "ray at origin not intersecting box at origin", + Intersection(ray, box, M, &distance, &normal) + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "intersection distance way off", + 0.0f, distance, delta + ); +} + +} +} diff --git a/tst/model/GeometryTest.hpp b/tst/model/GeometryTest.hpp new file mode 100644 index 0000000..2ee85b1 --- /dev/null +++ b/tst/model/GeometryTest.hpp @@ -0,0 +1,30 @@ +#ifndef BLANK_TEST_MODEL_GEOMETRYTEST_H_ +#define BLANK_TEST_MODEL_GEOMETRYTEST_H_ + +#include + + +namespace blank { +namespace test { + +class GeometryTest +: public CppUnit::TestFixture { + +CPPUNIT_TEST_SUITE(GeometryTest); + +CPPUNIT_TEST(testRayAABBIntersection); + +CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void testRayAABBIntersection(); + +}; + +} +} + +#endif