Quadtree fixes.

This commit is contained in:
Steve 2018-02-12 18:27:12 +00:00
parent 7f257f2740
commit ef9d6db851
2 changed files with 36 additions and 34 deletions

View File

@ -329,10 +329,10 @@ static void checkPlatformContact(void)
int i; int i;
SDL_Rect srcRect; SDL_Rect srcRect;
srcRect.x = self->x - MAP_TILE_SIZE; srcRect.x = self->x;
srcRect.y = self->y - MAP_TILE_SIZE; srcRect.y = self->y;
srcRect.w = self->w + MAP_TILE_SIZE * 2; srcRect.w = self->w;
srcRect.h = self->h + MAP_TILE_SIZE * 2; srcRect.h = self->h + 8;
candidates = getAllEntsWithin(srcRect.x, srcRect.y, srcRect.w, srcRect.h, NULL); candidates = getAllEntsWithin(srcRect.x, srcRect.y, srcRect.w, srcRect.h, NULL);

View File

@ -68,33 +68,35 @@ void initQuadtree(Quadtree *root)
node->ents = malloc(sizeof(Entity*) * node->capacity); node->ents = malloc(sizeof(Entity*) * node->capacity);
memset(node->ents, 0, sizeof(Entity*) * node->capacity); memset(node->ents, 0, sizeof(Entity*) * node->capacity);
if (i == 0) switch (i)
{ {
case 0:
node->x = root->x; node->x = root->x;
node->y = root->y; node->y = root->y;
node->w = w; node->w = w;
node->h = h; node->h = h;
} break;
else if (i == 1)
{ case 1:
node->x = root->x + w; node->x = root->x + w;
node->y = root->y; node->y = root->y;
node->w = w; node->w = w;
node->h = h; node->h = h;
} break;
else if (i == 2)
{ case 2:
node->x = root->x; node->x = root->x;
node->y = root->y + h; node->y = root->y + h;
node->w = w; node->w = w;
node->h = h; node->h = h;
} break;
else
{ default:
node->x = root->x + w; node->x = root->x + w;
node->y = root->y + h; node->y = root->y + h;
node->w = w; node->w = w;
node->h = h; node->h = h;
break;
} }
initQuadtree(node); initQuadtree(node);
@ -110,7 +112,7 @@ void addToQuadtree(Entity *e, Quadtree *root)
if (root->node[0]) if (root->node[0])
{ {
index = getIndex(root, e->x - (e->w / 2), e->y - (e->h / 2), e->w, e->h); index = getIndex(root, e->x, e->y, e->w, e->h);
if (index != -1) if (index != -1)
{ {
@ -152,7 +154,7 @@ static int getIndex(Quadtree *root, int x, int y, int w, int h)
{ {
if (topQuadrant) if (topQuadrant)
{ {
index = 1; index = 0;
} }
else if (bottomQuadrant) else if (bottomQuadrant)
{ {
@ -163,7 +165,7 @@ static int getIndex(Quadtree *root, int x, int y, int w, int h)
{ {
if (topQuadrant) if (topQuadrant)
{ {
index = 0; index = 1;
} }
else if (bottomQuadrant) else if (bottomQuadrant)
{ {
@ -182,7 +184,7 @@ void removeFromQuadtree(Entity *e, Quadtree *root)
{ {
if (root->node[0]) if (root->node[0])
{ {
index = getIndex(root, e->x - (e->w / 2), e->y - (e->h / 2), e->w, e->h); index = getIndex(root, e->x, e->y, e->w, e->h);
if (index != -1) if (index != -1)
{ {