yrryr567yry567

This commit is contained in:
verysecrethero
2015-12-02 15:03:55 +01:00
parent c698149bdd
commit fd99562c7a
3 changed files with 44 additions and 0 deletions
+3
View File
@@ -2,11 +2,13 @@
#define Collision_h__
#include "../GLM.h"
#include <algorithm>
namespace Collision
{
struct Ray
{
public:
glm::vec3 Origin;
glm::vec3 Direction;
};
@@ -29,6 +31,7 @@ private:
};
bool RayAABBIntr(const Ray& ray, const AABB& box);
bool RayVsAABB(const Ray& ray, const AABB& box);
}
#endif
+23
View File
@@ -1,4 +1,5 @@
#include "Core\Collision.h"
#include <algorithm>
namespace Collision
{
@@ -36,4 +37,26 @@ bool RayAABBIntr(const Ray& ray, const AABB& box)
return !(abs(c.x*w.y - c.y*w.x) > half.x*v.y + half.y*v.x);
}
bool RayVsAABB(const Ray& ray, const AABB& box)
{
glm::vec3 invdir = 1.0f / ray.Direction;
glm::vec3 bMin = box.MinCorner();
glm::vec3 bMax = box.MaxCorner();
float t1 = (bMin.x - ray.Origin.x)*invdir.x;
float t2 = (bMax.x - ray.Origin.x)*invdir.x;
float t3 = (bMin.y - ray.Origin.y)*invdir.y;
float t4 = (bMax.y - ray.Origin.y)*invdir.y;
float t5 = (bMin.z - ray.Origin.z)*invdir.z;
float t6 = (bMax.z - ray.Origin.z)*invdir.z;
float tmin = std::max(std::max(std::min(t1, t2), std::min(t3, t4)), std::min(t5, t6));
float tmax = std::min(std::min(std::max(t1, t2), std::max(t3, t4)), std::max(t5, t6));
if (tmax < 0 || tmin > tmax)
return false;
return true;
}
}
+18
View File
@@ -0,0 +1,18 @@
#include <boost/test/unit_test.hpp>
using boost::unit_test_framework::test_suite;
using boost::unit_test_framework::test_case;
#include <Engine\Core\Collision.h>
BOOST_AUTO_TEST_SUITE(collisionTests)
BOOST_AUTO_TEST_CASE(collisionTest)
{
Collision::Ray ray;
ray.Origin = glm::vec3(0, 0, 0);
ray.Direction = glm::vec3(0, 0, 0);
auto someAABB = Collision::AABB(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0));
bool z = Collision::RayVsAABB(ray, someAABB);
}
BOOST_AUTO_TEST_SUITE_END()