yrryr567yry567
This commit is contained in:
@@ -2,11 +2,13 @@
|
|||||||
#define Collision_h__
|
#define Collision_h__
|
||||||
|
|
||||||
#include "../GLM.h"
|
#include "../GLM.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace Collision
|
namespace Collision
|
||||||
{
|
{
|
||||||
struct Ray
|
struct Ray
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
glm::vec3 Origin;
|
glm::vec3 Origin;
|
||||||
glm::vec3 Direction;
|
glm::vec3 Direction;
|
||||||
};
|
};
|
||||||
@@ -29,6 +31,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
bool RayAABBIntr(const Ray& ray, const AABB& box);
|
bool RayAABBIntr(const Ray& ray, const AABB& box);
|
||||||
|
bool RayVsAABB(const Ray& ray, const AABB& box);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "Core\Collision.h"
|
#include "Core\Collision.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace Collision
|
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);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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()
|
||||||
|
|
||||||
Reference in New Issue
Block a user