From 621d935d7aced969cc920bb32acbaada49db4baf Mon Sep 17 00:00:00 2001 From: William Moberg Date: Mon, 18 Jan 2016 18:24:11 +0100 Subject: [PATCH] Octree returns correct boxes when testing along an axis by searching through the correct child subtrees. --- src/Engine/Core/AABB.cpp | 2 ++ src/Engine/Core/Octree.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Engine/Core/AABB.cpp b/src/Engine/Core/AABB.cpp index 22272104..55b362fe 100644 --- a/src/Engine/Core/AABB.cpp +++ b/src/Engine/Core/AABB.cpp @@ -15,6 +15,8 @@ AABB::AABB(const glm::vec3& minPos, const glm::vec3& maxPos) m_MinCorner.y = glm::min(m_MaxCorner.y, m_MinCorner.y); m_MaxCorner.z = glm::max(m_MaxCorner.z, m_MinCorner.z); m_MinCorner.z = glm::min(m_MaxCorner.z, m_MinCorner.z); + m_Origin = 0.5f * (m_MaxCorner + m_MinCorner); + m_HalfSize = 0.5f * (m_MaxCorner - m_MinCorner); } } diff --git a/src/Engine/Core/Octree.cpp b/src/Engine/Core/Octree.cpp index 47d06add..58501117 100644 --- a/src/Engine/Core/Octree.cpp +++ b/src/Engine/Core/Octree.cpp @@ -352,9 +352,13 @@ std::vector Octree::Child::childIndicesContainingBox(const AABB& box) const //the dimensions they are responsible for (which octant). bits.flip(); //At this point the bits necessarily have exactly one bit set. + //Check the same bit in the minInd as the one set in bits. + int setOrUnset = (bits.to_ulong() & minInd); for (int c = 0; c < 8; ++c) { - //If the child index have the same bit set as the bits, add box to it. - if (bits.to_ulong() & c) { + //Check the same bit in the child index as the one set in bits. + //Enter here if both c and minInd have the bit set, or if neither have it set. + //I.e, if they are on the same side (+ or -) in the dimension marked by the bit in bits. + if (!((bits.to_ulong() & c) ^ setOrUnset)) { ret.push_back(c); } }