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,6 +178,8 @@ void removeFromQuadtree(Entity *e, Quadtree *root)
{
int index;
if (root->addedTo)
{
if (root->node[0])
{
index = getIndex(root, e->x - (e->w / 2), e->y - (e->h / 2), e->w, e->h);
@ -189,6 +193,7 @@ void removeFromQuadtree(Entity *e, Quadtree *root)
removeEntity(e, root);
}
}
static void removeEntity(Entity *e, Quadtree *root)
{
@ -222,6 +227,8 @@ static void getAllEntsWithinNode(int x, int y, int w, int h, Entity *ignore, Qua
{
int index, i;
if (root->addedTo)
{
if (root->node[0])
{
index = getIndex(root, x, y, w, h);
@ -249,6 +256,7 @@ static void getAllEntsWithinNode(int x, int y, int w, int h, Entity *ignore, Qua
}
}
}
}
static void resizeCandidates(void)
{

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];
};