Added Ray, AABB structs, and basic rayVsAABB test function.

This commit is contained in:
William Moberg
2015-12-02 14:24:21 +01:00
parent fa0ace8c68
commit c698149bdd
3 changed files with 58 additions and 22 deletions
+26 -1
View File
@@ -1,9 +1,34 @@
#ifndef Collision_h__
#define Collision_h__
#include "../GLM.h"
namespace Collision
{
//bool RayAABBIntr(float3 origin, float3 direction, float3 center, float3 h);
struct Ray
{
glm::vec3 Origin;
glm::vec3 Direction;
};
class AABB
{
public:
AABB() = default;
AABB(const glm::vec3& minPos, const glm::vec3& maxPos);
const glm::vec3& MinCorner() const { return m_MinCorner; }
const glm::vec3& MaxCorner() const { return m_MaxCorner; }
const glm::vec3& Center() const { return m_Center; }
const glm::vec3& HalfSize() const { return m_HalfSize; }
private:
glm::vec3 m_MinCorner;
glm::vec3 m_MaxCorner;
glm::vec3 m_Center;
glm::vec3 m_HalfSize;
};
bool RayAABBIntr(const Ray& ray, const AABB& box);
}
#endif
+31 -19
View File
@@ -1,27 +1,39 @@
#include "Core\Collision.h"
namespace Collision
{
/*bool RayAABBIntr(float3 origin, float3 direction, float3 center, float3 h)
AABB::AABB(const glm::vec3& minPos, const glm::vec3& maxPos)
: m_MinCorner(minPos)
, m_MaxCorner(maxPos)
, m_Center(0.5f * (maxPos + minPos))
, m_HalfSize(0.5f * (maxPos - minPos))
{ }
bool RayAABBIntr(const Ray& ray, const AABB& box)
{
float3 w = 75.0f * direction;
float3 v = abs(w);
float3 c = origin - center + w;
glm::vec3 w = 75.0f * ray.Direction;
glm::vec3 v = glm::abs(w);
glm::vec3 c = ray.Origin - box.Center() + w;
glm::vec3 half = box.HalfSize();
if (abs(c.x) > v.x + half.x) {
return false;
}
if (abs(c.y) > v.y + half.y) {
return false;
}
if (abs(c.z) > v.z + half.z) {
return false;
}
//This is better if we hit often, do comparisons simultanously.
//if (any(abs(c) > v + h))
// return false;
//
//return !(any(abs(c.yxx*w.zzy - c.zzy*w.yxx) > h.yxx*v.zzy + h.zzy*v.yxx));
//This is better if we miss often, reject test and exit early.
if (abs(c.x) > v.x + h.x) return false;
if (abs(c.y) > v.y + h.y) return false;
if (abs(c.z) > v.z + h.z) return false;
if (abs(c.y*w.z - c.z*w.y) > h.y*v.z + h.z*v.y) return false;
if (abs(c.x*w.z - c.z*w.x) > h.x*v.z + h.z*v.x) return false;
return !(abs(c.x*w.y - c.y*w.x) > h.x*v.y + h.y*v.x);
}*/
if (abs(c.y*w.z - c.z*w.y) > half.y*v.z + half.z*v.y) {
return false;
}
if (abs(c.x*w.z - c.z*w.x) > half.x*v.z + half.z*v.x) {
return false;
}
return !(abs(c.x*w.y - c.y*w.x) > half.x*v.y + half.y*v.x);
}
}
+1 -2
View File
@@ -86,8 +86,7 @@ T* ClassType::PublicMemberFunction(bool value)
if (m_PrivateMember2->PublicMember == 1) {
return new T();
}
else {
} else {
return nullptr;
}
}