From e8d23f3b3f4ec58d1b4d0cbde079c340f836476e Mon Sep 17 00:00:00 2001 From: William Moberg Date: Mon, 7 Dec 2015 15:59:41 +0100 Subject: [PATCH] Added AABBVsAABB function. --- include/Engine/Core/Collision.h | 2 ++ src/Engine/Core/Collision.cpp | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/Engine/Core/Collision.h b/include/Engine/Core/Collision.h index 67a9e808..2e90b8bf 100644 --- a/include/Engine/Core/Collision.h +++ b/include/Engine/Core/Collision.h @@ -11,6 +11,8 @@ bool RayAABBIntr(const Ray& ray, const AABB& box); bool RayVsAABB(const Ray& ray, const AABB& box); bool RayVsAABB(const Ray& ray, const AABB& box, float& outDistance); +bool AABBVsAABB(const AABB& a, const AABB& b); +bool AABBVsAABBTst(const AABB& a, const AABB& b); } #endif \ No newline at end of file diff --git a/src/Engine/Core/Collision.cpp b/src/Engine/Core/Collision.cpp index fda895a4..07e37085 100644 --- a/src/Engine/Core/Collision.cpp +++ b/src/Engine/Core/Collision.cpp @@ -58,4 +58,20 @@ bool RayVsAABB(const Ray& ray, const AABB& box, float& outDistance) return true; } +bool AABBVsAABB(const AABB& a, const AABB& b) +{ + const glm::vec3& aCenter = a.Center(); + const glm::vec3& bCenter = b.Center(); + const glm::vec3& aHSize = a.HalfSize(); + const glm::vec3& bHSize = b.HalfSize(); + //Test will probably exit because of the X and Z axes more often, so test them first. + if (abs(aCenter[0] - bCenter[0]) > (aHSize[0] + bHSize[0])) { + return false; + } + if (abs(aCenter[2] - bCenter[2]) > (aHSize[2] + bHSize[2])) { + return false; + } + return (abs(aCenter[1] - bCenter[1]) <= (aHSize[1] + bHSize[1])); +} + } \ No newline at end of file