OctTree can check if a box collides with a box in the OctTree.
This commit is contained in:
@@ -20,17 +20,21 @@ public:
|
|||||||
void ClearBoxes();
|
void ClearBoxes();
|
||||||
//Returns true if the ray collides with something in the tree. Result is written to [data].
|
//Returns true if the ray collides with something in the tree. Result is written to [data].
|
||||||
bool RayCollides(const Ray& ray, Output& data) const;
|
bool RayCollides(const Ray& ray, Output& data) const;
|
||||||
|
//Returns true if the box collides with something in the tree.
|
||||||
|
//On collision with a box, that box is written to [outBoxIntersected].
|
||||||
|
bool BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OctTree* m_Children[8];
|
OctTree* m_Children[8];
|
||||||
std::vector<AABB> m_ContainingBoxes;
|
|
||||||
//TODO: Do derived class from AABB with a bool Tested, falsify at
|
//TODO: Do derived class from AABB with a bool Tested, falsify at
|
||||||
//start of Collision test, set on check, don't check if set already. Solves duplicate boxes in tree.
|
//start of Collision test, set on check, don't check if set already. Solves duplicate boxes in tree.
|
||||||
|
//TODO: Boxes collide with themselves? Fix somehow, maybe float epsilon stuff.
|
||||||
|
std::vector<AABB> m_ContainingBoxes;
|
||||||
AABB m_Box;
|
AABB m_Box;
|
||||||
|
|
||||||
bool rayCollides(const Ray& ray, Output& data) const;
|
|
||||||
inline bool hasChildren() const;
|
inline bool hasChildren() const;
|
||||||
int childIndexContainingPoint(const glm::vec3& point) const;
|
int childIndexContainingPoint(const glm::vec3& point) const;
|
||||||
|
std::vector<int> childIndicesContainingBox(const AABB& box) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+58
-40
@@ -48,7 +48,7 @@ OctTree::OctTree(const AABB& octTreeBounds, int subDivisions)
|
|||||||
minPos.x = parentMin.x;
|
minPos.x = parentMin.x;
|
||||||
maxPos.x = parentCenter.x;
|
maxPos.x = parentCenter.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
//If child is 2,3,6,7
|
//If child is 2,3,6,7
|
||||||
if (bits.test(1)) {
|
if (bits.test(1)) {
|
||||||
minPos.y = parentCenter.y;
|
minPos.y = parentCenter.y;
|
||||||
@@ -80,14 +80,25 @@ OctTree::~OctTree()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OctTree::RayCollides(const Ray& ray, Output& data) const
|
bool OctTree::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const
|
||||||
{
|
{
|
||||||
data.CollideDistance = -1;
|
if (hasChildren()) {
|
||||||
return rayCollides(ray, data);
|
for (int i : childIndicesContainingBox(boxToTest)) {
|
||||||
|
if (m_Children[i]->BoxCollides(boxToTest, outBoxIntersected))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (const auto& objBox : m_ContainingBoxes) {
|
||||||
|
if (Collision::AABBVsAABB(boxToTest, objBox)) {
|
||||||
|
outBoxIntersected = objBox;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Currently all Nodes must have exactly 0 or 8 children, and objectdata should only exist in the last bottom nodes.
|
bool OctTree::RayCollides(const Ray& ray, Output& data) const
|
||||||
bool OctTree::rayCollides(const Ray& ray, Output& data) const
|
|
||||||
{
|
{
|
||||||
//If the node AABB is missed, everything it contains is missed.
|
//If the node AABB is missed, everything it contains is missed.
|
||||||
if (Collision::RayAABBIntr(ray, m_Box)) {
|
if (Collision::RayAABBIntr(ray, m_Box)) {
|
||||||
@@ -102,7 +113,7 @@ bool OctTree::rayCollides(const Ray& ray, Output& data) const
|
|||||||
std::sort(childInfos.begin(), childInfos.end(), isFirstLower);
|
std::sort(childInfos.begin(), childInfos.end(), isFirstLower);
|
||||||
//Loop through the children, starting with the one closest to the ray origin. I.e the first to be hit.
|
//Loop through the children, starting with the one closest to the ray origin. I.e the first to be hit.
|
||||||
for (const ChildInfo& info : childInfos) {
|
for (const ChildInfo& info : childInfos) {
|
||||||
if (m_Children[info.Index]->rayCollides(ray, data)) {
|
if (m_Children[info.Index]->RayCollides(ray, data)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,39 +138,8 @@ bool OctTree::rayCollides(const Ray& ray, Output& data) const
|
|||||||
void OctTree::AddBox(const AABB& box)
|
void OctTree::AddBox(const AABB& box)
|
||||||
{
|
{
|
||||||
if (hasChildren()) {
|
if (hasChildren()) {
|
||||||
int minInd = childIndexContainingPoint(box.MinCorner());
|
for (auto i : childIndicesContainingBox(box)) {
|
||||||
int maxInd = childIndexContainingPoint(box.MaxCorner());
|
m_Children[i]->AddBox(box);
|
||||||
//Because of the predictable ordering of the child indices,
|
|
||||||
//the number of bits set when xor:ing the indices will determine the number of children containing the box.
|
|
||||||
std::bitset<3> bits(minInd ^ maxInd);
|
|
||||||
switch (bits.count()) {
|
|
||||||
case 0: //Box contained completely in one child.
|
|
||||||
m_Children[minInd]->AddBox(box);
|
|
||||||
break;
|
|
||||||
case 1: //Two children.
|
|
||||||
m_Children[minInd]->AddBox(box);
|
|
||||||
m_Children[maxInd]->AddBox(box);
|
|
||||||
break;
|
|
||||||
case 2: //Four children.
|
|
||||||
//Bit-hax to calculate the right 4 cildren containing the box.
|
|
||||||
//This works because of the childrens index determine what part of
|
|
||||||
//the dimensions they are responsible for (which octant).
|
|
||||||
bits.flip();
|
|
||||||
//At this point the bits necessarily have exactly one bit set.
|
|
||||||
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) {
|
|
||||||
m_Children[c]->AddBox(box);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 3: //Eight children.
|
|
||||||
for (OctTree*& c : m_Children) {
|
|
||||||
c->AddBox(box);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
m_ContainingBoxes.push_back(box);
|
m_ContainingBoxes.push_back(box);
|
||||||
@@ -196,6 +176,44 @@ int OctTree::childIndexContainingPoint(const glm::vec3& point) const
|
|||||||
return (1 << 2) * (point.x >= c.x) | (1 << 1) * (point.y >= c.y) | (point.z >= c.z);
|
return (1 << 2) * (point.x >= c.x) | (1 << 1) * (point.y >= c.y) | (point.z >= c.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<int> OctTree::childIndicesContainingBox(const AABB& box) const
|
||||||
|
{
|
||||||
|
int minInd = childIndexContainingPoint(box.MinCorner());
|
||||||
|
int maxInd = childIndexContainingPoint(box.MaxCorner());
|
||||||
|
//Because of the predictable ordering of the child indices,
|
||||||
|
//the number of bits set when xor:ing the indices will determine the number of children containing the box.
|
||||||
|
std::bitset<3> bits(minInd ^ maxInd);
|
||||||
|
switch (bits.count()) {
|
||||||
|
//Box contained completely in one child.
|
||||||
|
case 0:
|
||||||
|
return{ minInd };
|
||||||
|
//Two children.
|
||||||
|
case 1:
|
||||||
|
return{ minInd, maxInd };
|
||||||
|
//Four children.
|
||||||
|
case 2:
|
||||||
|
{
|
||||||
|
std::vector<int> ret;
|
||||||
|
//Bit-hax to calculate the right 4 cildren containing the box.
|
||||||
|
//This works because of the childrens index determine what part of
|
||||||
|
//the dimensions they are responsible for (which octant).
|
||||||
|
bits.flip();
|
||||||
|
//At this point the bits necessarily have exactly one bit set.
|
||||||
|
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) {
|
||||||
|
ret.push_back(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
case 3: //Eight children.
|
||||||
|
return{ 0,1,2,3,4,5,6,7 };
|
||||||
|
default:
|
||||||
|
return std::vector<int>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
inline bool OctTree::hasChildren() const
|
inline bool OctTree::hasChildren() const
|
||||||
{
|
{
|
||||||
return m_Children[0] != nullptr;
|
return m_Children[0] != nullptr;
|
||||||
|
|||||||
Reference in New Issue
Block a user