Made some optimizations in OctTree and added a unit test for BoxesInSameRegion.
This commit is contained in:
@@ -25,9 +25,6 @@ public:
|
|||||||
OctTree(const OctTree&& other) = delete;
|
OctTree(const OctTree&& other) = delete;
|
||||||
OctTree& operator= (const OctTree& other) = delete;
|
OctTree& operator= (const OctTree& other) = delete;
|
||||||
|
|
||||||
//Collision test function. WTODO: Probably remove or relocate elsewhere, Collision system?
|
|
||||||
void Update(float dt, World* world, Camera* cam);
|
|
||||||
|
|
||||||
void AddDynamicObject(const AABB& box);
|
void AddDynamicObject(const AABB& box);
|
||||||
void AddStaticObject(const AABB& box);
|
void AddStaticObject(const AABB& box);
|
||||||
|
|
||||||
@@ -36,10 +33,13 @@ public:
|
|||||||
void ClearObjects();
|
void ClearObjects();
|
||||||
void ClearDynamicObjects();
|
void ClearDynamicObjects();
|
||||||
|
|
||||||
|
//Collision test function. WTODO: Probably remove or relocate elsewhere, Collision system?
|
||||||
|
void Update(float dt, World* world, Camera* cam);
|
||||||
//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.
|
||||||
//On collision with a box, that box is written to [outBoxIntersected].
|
//On collision with a box, that box is written to [outBoxIntersected].
|
||||||
|
//Note: More efficient than calling BoxesInSameRegion from outside and testing there.
|
||||||
bool BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const;
|
bool BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -47,7 +47,6 @@ private:
|
|||||||
//WTODO: Do -derived class from AABB- struct containing 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.
|
||||||
//Store indices in the struct, pointing to grand ancestor list of boxes, need the same AABB not copies to save Tested.
|
//Store indices in the struct, pointing to grand ancestor list of boxes, need the same AABB not copies to save Tested.
|
||||||
//WTODO: Boxes collide with themselves? Fix somehow, maybe float epsilon stuff.
|
|
||||||
std::vector<AABB> m_StaticObjects;
|
std::vector<AABB> m_StaticObjects;
|
||||||
std::vector<AABB> m_DynamicObjects;
|
std::vector<AABB> m_DynamicObjects;
|
||||||
AABB m_Box;
|
AABB m_Box;
|
||||||
|
|||||||
+34
-21
@@ -21,6 +21,16 @@ bool isFirstLower(const ChildInfo& first, const ChildInfo& second)
|
|||||||
return first.Distance < second.Distance;
|
return first.Distance < second.Distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isSameBoxProbably(const AABB& first, const AABB& second)
|
||||||
|
{
|
||||||
|
const float EPS = 0.0001f;
|
||||||
|
const auto& ma = first.MaxCorner();
|
||||||
|
const auto& mi = first.MinCorner();
|
||||||
|
return (std::abs(ma.x - mi.x) < EPS) &&
|
||||||
|
(std::abs(ma.z - mi.z) < EPS) &&
|
||||||
|
(std::abs(ma.y - mi.y) < EPS);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OctTree::OctTree()
|
OctTree::OctTree()
|
||||||
@@ -131,16 +141,18 @@ bool OctTree::BoxCollides(const AABB& boxToTest, AABB& outBoxIntersected) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::vector<std::vector<AABB>> objVectors = {
|
for (const auto& obj : m_StaticObjects) {
|
||||||
m_StaticObjects,
|
if (Collision::AABBVsAABB(boxToTest, obj)) {
|
||||||
m_DynamicObjects
|
outBoxIntersected = obj;
|
||||||
};
|
return true;
|
||||||
for (const auto& objVector : objVectors) {
|
}
|
||||||
for (const auto& obj : objVector) {
|
}
|
||||||
if (Collision::AABBVsAABB(boxToTest, obj)) {
|
for (const auto& obj : m_DynamicObjects) {
|
||||||
outBoxIntersected = obj;
|
//If there is a collision and it is not testing against itself.
|
||||||
return true;
|
if (!isSameBoxProbably(boxToTest, obj) &&
|
||||||
}
|
Collision::AABBVsAABB(boxToTest, obj)) {
|
||||||
|
outBoxIntersected = obj;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,17 +182,18 @@ 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;
|
||||||
std::vector<std::vector<AABB>> objVectors = {
|
for (const auto& obj : m_StaticObjects) {
|
||||||
m_StaticObjects,
|
float dist;
|
||||||
m_DynamicObjects
|
if (Collision::RayVsAABB(ray, obj, dist)) {
|
||||||
};
|
minDist = std::min(dist, minDist);
|
||||||
for (const auto& objVector : objVectors) {
|
intersected = true;
|
||||||
for (const auto& obj : objVector) {
|
}
|
||||||
float dist;
|
}
|
||||||
if (Collision::RayVsAABB(ray, obj, dist)) {
|
for (const auto& obj : m_DynamicObjects) {
|
||||||
minDist = std::min(dist, minDist);
|
float dist;
|
||||||
intersected = true;
|
if (Collision::RayVsAABB(ray, obj, dist)) {
|
||||||
}
|
minDist = std::min(dist, minDist);
|
||||||
|
intersected = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,5 +16,25 @@ BOOST_AUTO_TEST_CASE(octTreeTest2)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(octSameRegionTest)
|
||||||
|
{
|
||||||
|
glm::vec3 mini = glm::vec3(-1, -1, -1);
|
||||||
|
glm::vec3 maxi = glm::vec3(1, 1, 1);
|
||||||
|
OctTree tree(AABB(mini, maxi), 2);
|
||||||
|
AABB firstQuadrant(mini, 0.8f*mini);
|
||||||
|
tree.AddStaticObject(firstQuadrant);
|
||||||
|
AABB testBox(0.9f*mini, 0.8f*mini);
|
||||||
|
std::vector<AABB> region;
|
||||||
|
tree.BoxesInSameRegion(testBox, region);
|
||||||
|
BOOST_REQUIRE(region.size() == 1);
|
||||||
|
AABB& box = region[0];
|
||||||
|
BOOST_CHECK_CLOSE_FRACTION(box.Center().x, firstQuadrant.Center().x, 0.00001f);
|
||||||
|
BOOST_CHECK_CLOSE_FRACTION(box.Center().y, firstQuadrant.Center().y, 0.00001f);
|
||||||
|
BOOST_CHECK_CLOSE_FRACTION(box.Center().z, firstQuadrant.Center().z, 0.00001f);
|
||||||
|
BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().x, firstQuadrant.HalfSize().x, 0.00001f);
|
||||||
|
BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().y, firstQuadrant.HalfSize().y, 0.00001f);
|
||||||
|
BOOST_CHECK_CLOSE_FRACTION(box.HalfSize().z, firstQuadrant.HalfSize().z, 0.00001f);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user