OctTree differentiates between static/dynamic objects now. Added a general BoxesInSameRegion method.

This commit is contained in:
William Moberg
2015-12-09 13:27:00 +01:00
parent a7fb77d998
commit bad42fae1c
3 changed files with 92 additions and 27 deletions
+21 -6
View File
@@ -16,8 +16,21 @@ public:
~OctTree(); ~OctTree();
//For the root OctTree, [octTreeBounds] should be a box containing the entire level. //For the root OctTree, [octTreeBounds] should be a box containing the entire level.
OctTree(const AABB& octTreeBounds, int subDivisions); OctTree(const AABB& octTreeBounds, int subDivisions);
void AddBox(const AABB& box);
void ClearBoxes(); //We should only ever need one OctTree in the game, and it should not need to be copied.
//Define these if the OctTree suddenly needs to be copied, think of the children OctTree* ptrs.
OctTree(const OctTree& other) = delete;
OctTree(const OctTree&& other) = delete;
OctTree& operator= (const OctTree& other) = delete;
void AddDynamicObject(const AABB& box);
void AddStaticObject(const AABB& box);
void BoxesInSameRegion(const AABB& box, std::vector<AABB>& outBoxes) const;
void ClearObjects();
void ClearDynamicObjects();
//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. //Returns true if the box collides with something in the tree.
@@ -26,10 +39,12 @@ public:
private: private:
OctTree* m_Children[8]; OctTree* m_Children[8];
//TODO: Do derived class from AABB with a bool Tested, falsify at //WTODO: Do -derived class from AABB- struct containing 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. //Store indices in the struct, pointing to grand ancestor list of boxes, need the same AABB not copies to save Tested.
std::vector<AABB> m_ContainingBoxes; //WTODO: Boxes collide with themselves? Fix somehow, maybe float epsilon stuff.
std::vector<AABB> m_StaticObjects;
std::vector<AABB> m_DynamicObjects;
AABB m_Box; AABB m_Box;
inline bool hasChildren() const; inline bool hasChildren() const;
+65 -17
View File
@@ -88,10 +88,16 @@ bool OctTree::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const
return true; return true;
} }
} else { } else {
for (const auto& objBox : m_ContainingBoxes) { std::vector<std::vector<AABB>> objVectors = {
if (Collision::AABBVsAABB(boxToTest, objBox)) { m_StaticObjects,
outBoxIntersected = objBox; m_DynamicObjects
return true; };
for (const auto& objVector : objVectors) {
for (const auto& obj : objVector) {
if (Collision::AABBVsAABB(boxToTest, obj)) {
outBoxIntersected = obj;
return true;
}
} }
} }
} }
@@ -121,13 +127,20 @@ bool OctTree::RayCollides(const Ray& ray, Output& data) const
//Check against boxes in the node. //Check against boxes in the node.
float minDist = INFINITY; float minDist = INFINITY;
bool intersected = false; bool intersected = false;
for (const auto& objBox : m_ContainingBoxes) { std::vector<std::vector<AABB>> objVectors = {
float dist; m_StaticObjects,
if (Collision::RayVsAABB(ray, objBox, dist)) { m_DynamicObjects
minDist = std::min(dist, minDist); };
intersected = true; for (const auto& objVector : objVectors) {
for (const auto& obj : objVector) {
float dist;
if (Collision::RayVsAABB(ray, obj, dist)) {
minDist = std::min(dist, minDist);
intersected = true;
}
} }
} }
data.CollideDistance = minDist; data.CollideDistance = minDist;
return intersected; return intersected;
} }
@@ -135,26 +148,61 @@ bool OctTree::RayCollides(const Ray& ray, Output& data) const
return false; return false;
} }
void OctTree::AddBox(const AABB& box)
void OctTree::AddDynamicObject(const AABB& box)
{ {
if (hasChildren()) { if (hasChildren()) {
for (auto i : childIndicesContainingBox(box)) { for (auto i : childIndicesContainingBox(box)) {
m_Children[i]->AddBox(box); m_Children[i]->AddDynamicObject(box);
} }
} else { } else {
m_ContainingBoxes.push_back(box); m_DynamicObjects.push_back(box);
} }
} }
//TODO: Only clear dynamic boxes, AddDynamic, AddStatic void OctTree::AddStaticObject(const AABB& box)
void OctTree::ClearBoxes() {
if (hasChildren()) {
for (auto i : childIndicesContainingBox(box)) {
m_Children[i]->AddStaticObject(box);
}
} else {
m_StaticObjects.push_back(box);
}
}
void OctTree::BoxesInSameRegion(const AABB& box, std::vector<AABB>& outBoxes) const
{
if (hasChildren()) {
for (auto i : childIndicesContainingBox(box)) {
m_Children[i]->BoxesInSameRegion(box, outBoxes);
}
} else {
outBoxes.insert(outBoxes.end(), m_StaticObjects.begin(), m_StaticObjects.end());
outBoxes.insert(outBoxes.end(), m_DynamicObjects.begin(), m_DynamicObjects.end());
}
}
void OctTree::ClearObjects()
{ {
if (hasChildren()) { if (hasChildren()) {
for (OctTree*& c : m_Children) { for (OctTree*& c : m_Children) {
c->ClearBoxes(); c->ClearObjects();
} }
} else { } else {
m_ContainingBoxes.clear(); m_DynamicObjects.clear();
m_StaticObjects.clear();
}
}
void OctTree::ClearDynamicObjects()
{
if (hasChildren()) {
for (OctTree*& c : m_Children) {
c->ClearObjects();
}
} else {
m_DynamicObjects.clear();
} }
} }
@@ -194,7 +242,7 @@ std::vector<int> OctTree::childIndicesContainingBox(const AABB& box) const
case 2: case 2:
{ {
std::vector<int> ret; std::vector<int> ret;
//Bit-hax to calculate the right 4 cildren containing the box. //Bit-hax to calculate the correct 4 children containing the box.
//This works because of the childrens index determine what part of //This works because of the childrens index determine what part of
//the dimensions they are responsible for (which octant). //the dimensions they are responsible for (which octant).
bits.flip(); bits.flip();
+6 -4
View File
@@ -92,12 +92,14 @@ BOOST_AUTO_TEST_CASE(octTest)
glm::vec3 mini = glm::vec3(-1, -1, -1); glm::vec3 mini = glm::vec3(-1, -1, -1);
glm::vec3 maxi = glm::vec3(1, 1, 1); glm::vec3 maxi = glm::vec3(1, 1, 1);
OctTree tree(AABB(mini, maxi), 2); OctTree tree(AABB(mini, maxi), 2);
tree.AddBox(AABB(mini, -0.9f*maxi)); tree.AddDynamicObject(AABB(mini, -0.9f*maxi));
OctTree::Output data; OctTree::Output data;
glm::vec3 origin = 3.0f * mini; glm::vec3 origin = 3.0f * mini;
BOOST_CHECK(tree.RayCollides({origin , glm::normalize(mini - origin) }, data)); bool rayIntersected = tree.RayCollides({ origin , glm::normalize(mini - origin) }, data);
tree.ClearBoxes(); BOOST_CHECK(rayIntersected);
BOOST_CHECK(!tree.RayCollides({ origin , glm::normalize(mini - origin) }, data)); tree.ClearDynamicObjects();
rayIntersected = tree.RayCollides({ origin , glm::normalize(mini - origin) }, data);
BOOST_CHECK(!rayIntersected);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()