Don't recurse into quads that haven't had anything added to them.

This commit is contained in:
Steve 2016-04-20 13:53:25 +01:00
parent 6d8856deae
commit 7e752ccae4
3 changed files with 35 additions and 26 deletions

View File

@ -106,6 +106,8 @@ void addToQuadtree(Entity *e, Quadtree *root)
{
int index;
root->addedTo = 1;
if (root->node[0])
{
index = getIndex(root, e->x - (e->w / 2), e->y - (e->h / 2), e->w, e->h);
@ -176,18 +178,21 @@ void removeFromQuadtree(Entity *e, Quadtree *root)
{
int index;
if (root->node[0])
if (root->addedTo)
{
index = getIndex(root, e->x - (e->w / 2), e->y - (e->h / 2), e->w, e->h);
if (index != -1)
if (root->node[0])
{
removeFromQuadtree(e, root->node[index]);
return;
}
}
index = getIndex(root, e->x - (e->w / 2), e->y - (e->h / 2), e->w, e->h);
removeEntity(e, root);
if (index != -1)
{
removeFromQuadtree(e, root->node[index]);
return;
}
}
removeEntity(e, root);
}
}
static void removeEntity(Entity *e, Quadtree *root)
@ -222,30 +227,33 @@ static void getAllEntsWithinNode(int x, int y, int w, int h, Entity *ignore, Qua
{
int index, i;
if (root->node[0])
if (root->addedTo)
{
index = getIndex(root, x, y, w, h);
if (root->node[0])
{
index = getIndex(root, x, y, w, h);
if (index != -1)
{
getAllEntsWithinNode(x, y, w, h, ignore, root->node[index]);
}
else
{
for (i = 0 ; i < 4 ; i++)
if (index != -1)
{
getAllEntsWithinNode(x, y, w, h, ignore, root->node[i]);
getAllEntsWithinNode(x, y, w, h, ignore, root->node[index]);
}
else
{
for (i = 0 ; i < 4 ; i++)
{
getAllEntsWithinNode(x, y, w, h, ignore, root->node[i]);
}
}
}
}
for (i = 0 ; i < root->numEnts ; i++)
{
candidates[cIndex++] = root->ents[i];
if (cIndex == cCapacity)
for (i = 0 ; i < root->numEnts ; i++)
{
resizeCandidates();
candidates[cIndex++] = root->ents[i];
if (cIndex == cCapacity)
{
resizeCandidates();
}
}
}
}

View File

@ -20,7 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h"
#define QT_MAX_DEPTH 6
#define QT_MAX_DEPTH 5
#define QT_INITIAL_CAPACITY 8
extern void *resize(void *array, int oldSize, int newSize);

View File

@ -304,6 +304,7 @@ struct Quadtree {
Entity **ents;
int capacity;
int numEnts;
int addedTo;
Quadtree *node[4];
};