轴对齐边界框碰撞检测问题

时间:2013-07-30 22:36:47

标签: c++ visual-c++

我试图制作一个使用世界坐标和边界框结构的算法 检测两个边界框之间的碰撞。我真的不知道我在做什么,但我认为下面的代码会起作用。我的问题是它只检测碰撞,如果边界框位于完全相同的x,y,z位置。

BOOL AABB::isCollidedWith(AABB* bb)
{
if(bb == NULL) return FALSE;

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

float arr[12];

      //please note that all the mins are set to 0
      //and all the maxes are set to 1

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;

//bb coords

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

//this coords
arr[7]  = this->bbX - radX2;
arr[8]  = this->bbX + radX2;
arr[9]  = this->bbY - radY2;
arr[10] = this->bbY + radY2;
arr[11] = this->bbZ - radZ2;
arr[12] = this->bbZ + 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;
        }
    }
}
}

我正在比较的结构:

 AABB* aabb1 = new AABB(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f,0.0f,0.0f,0.0f);
 AABB* aabb2 = new AABB(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f,0.0f,0.0f,0.0f);
 aabb2->isCollidedWith(aabb1);

构造函数代码段: 另请注意,最后三个参数指示边界框的x,y,z线

AABB::AABB(float minx,float maxx,float miny,float maxy,float minz,float maxz,float x,float y,float z)
{
this->minX = minx;
this->maxX = maxx;
this->minY = miny;
this->maxY = maxy;
this->minZ = minz;
this->maxZ = maxz;

任何帮助,批评或建议都会有所帮助。

2 个答案:

答案 0 :(得分:0)

在创建minX = 0.0和maxX = 0.0的框时,bbX坐标必须与要碰撞的框相同(因为radX = 0)。 minZ = maxZ = 1.0也是如此。

请注意构造函数中参数的顺序:它是minX, maxX, minY, maxY, minZ, maxZ不是 minX, minY, minZ, maxX, maxY, maxZ(我想你应该是第二个订单,并希望定义一个1.0 x 1.0的框x 1.0尺寸)。

答案 1 :(得分:0)

简单错误!我忽略了列出参数的方式,导致了问题。 另外,我从数组“arr”的每个成员中减去0.5,找到AABB的中心。