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
+65 -17
View File
@@ -88,10 +88,16 @@ bool OctTree::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const
return true;
}
} else {
for (const auto& objBox : m_ContainingBoxes) {
if (Collision::AABBVsAABB(boxToTest, objBox)) {
outBoxIntersected = objBox;
return true;
std::vector<std::vector<AABB>> objVectors = {
m_StaticObjects,
m_DynamicObjects
};
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.
float minDist = INFINITY;
bool intersected = false;
for (const auto& objBox : m_ContainingBoxes) {
float dist;
if (Collision::RayVsAABB(ray, objBox, dist)) {
minDist = std::min(dist, minDist);
intersected = true;
std::vector<std::vector<AABB>> objVectors = {
m_StaticObjects,
m_DynamicObjects
};
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;
return intersected;
}
@@ -135,26 +148,61 @@ bool OctTree::RayCollides(const Ray& ray, Output& data) const
return false;
}
void OctTree::AddBox(const AABB& box)
void OctTree::AddDynamicObject(const AABB& box)
{
if (hasChildren()) {
for (auto i : childIndicesContainingBox(box)) {
m_Children[i]->AddBox(box);
m_Children[i]->AddDynamicObject(box);
}
} else {
m_ContainingBoxes.push_back(box);
m_DynamicObjects.push_back(box);
}
}
//TODO: Only clear dynamic boxes, AddDynamic, AddStatic
void OctTree::ClearBoxes()
void OctTree::AddStaticObject(const AABB& box)
{
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()) {
for (OctTree*& c : m_Children) {
c->ClearBoxes();
c->ClearObjects();
}
} 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:
{
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
//the dimensions they are responsible for (which octant).
bits.flip();