Severely Laggy Bounding Box Detection

73 views Asked by At

I have a bounding box collision detection system for a basic game. Each time I spawn in an entity a bounding box is paired with it. The collision system it self works; however it is extremely laggy even for just two entities. My code is listed below, I know it is definitively not the most efficient way to do this, but I don't really know how to do it any other way.

bbX, bbY,and bbZ are the position of the AABB's in the world.

float radX1,radX2;
float radY1,radY2;
float radZ1,radZ2;

float arr[12];

radX1 = (bb->maxX - bb->minX) / 2;
radX2 = (this->maxX - this->minX) / 2;
radY1 = (bb->maxY - bb->minY) / 2;
radY2 = (this->maxY - this->minY) / 2;
radZ1 = (bb->maxZ - bb->minZ) / 2;
radZ2 = (this->maxZ - this->minZ) / 2;

arr[1] = bb->bbX - 0.5f - radX1;
arr[2] = bb->bbX - 0.5f + radX1;
arr[3] = bb->bbY - 0.5f - radY1;
arr[4] = bb->bbY - 0.5f + radY1;
arr[5] = bb->bbZ - 0.5f - radZ1;
arr[6] = bb->bbZ - 0.5f + radZ1;

//this coords
arr[7]  = this->bbX - 0.5f - radX2;
arr[8]  = this->bbX - 0.5f + radX2;
arr[9]  = this->bbY - 0.5f - radY2;
arr[10] = this->bbY - 0.5f + radY2;
arr[11] = this->bbZ - 0.5f - radZ2;
arr[12] = this->bbZ - 0.5f + radZ2;

if(arr[2] >= arr[7] && arr[1] <= arr[8])
{
    if(arr[4] >= arr[9] && arr[3] <= arr[10])
    {
        if(arr[6] >= arr[11] && arr[5] <= arr[12])
        {
            this->collided = TRUE;
            OutputDebugStringA("Collided!\n");
            return TRUE;


        }
    }
}

This function is called on a timer approximately every 15ms, but increasing the iterations didn't help much either.

The function that does the work of comparing existing bounding boxes: aabbPool is a list that stores all the AABB's currently loaded.

for(auto i = this->aabbPool.begin();i < this->aabbPool.end();++i)
{
    OutputDebugStringA("Called!\n");

    if(*i == NULL) ;
    else
    {
        exe = *i;

        for(auto k = this->aabbPool.begin();k < this->aabbPool.end();++k)
        {
                comp = *k;

                if(exe->id == comp->id) ;
                else
                {

                if(exe->isCollidedWith(comp)) OutputDebugStringA("Collided!\n");

                }
        }
    }

}
0

There are 0 answers