1

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");

                }
        }
    }

}
Nick Ellas
  • 97
  • 7
  • You're doing I/O on every object in the pool? Also the program is quadratic. Also, do you have to do this in floats, not integers? – Mike Dunlavey Aug 06 '13 at 21:22
  • Yes, the function checks every object it in the pool, and compares it with everything else in the pool except it self. However, I do believe the former function is the problem. I don't really know how to write a more efficient BB collision detector. – Nick Ellas Aug 06 '13 at 21:25
  • 1) first get rid of the `OutputDebugStringA`. 2) see if you can use integers, not floats. 3) If it's still too slow, keep the objects sorted by one of the dimensions, so you can easily discard the ones that are way out of range. – Mike Dunlavey Aug 06 '13 at 21:27
  • Well, I'm using OpenGL right now to render everything, so I adopted its coordinate system(which is comprised of floats) . I guess I could use integers if I casted the bounding box coords. – Nick Ellas Aug 06 '13 at 21:28
  • Try one thing at a time, and what I do is take manual stack samples to see exactly what is responsible for the time. – Mike Dunlavey Aug 06 '13 at 21:29
  • 1
    That seemed to do it Mike! Changed the data to integers then got rid of **OutputDebugStringA**. – Nick Ellas Aug 06 '13 at 21:35

0 Answers0