【问题标题】:Axis Aligned Bounding Box Collision Detection Issue轴对齐边界框碰撞检测问题
【发布时间】:2013-07-30 22:36:47
【问题描述】:

我试图产生一种算法,它使用世界坐标和边界框结构来 检测两个边界框之间的碰撞。我真的不知道自己在做什么,但我认为下面的代码会起作用。我的问题是,它仅在边界框位于完全相同的 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);

构造函数 sn-p : 另请注意,最后三个参数决定边界框的 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;

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

【问题讨论】:

  • 你能举一个碰撞的例子,一个有效的,一个无效的?
  • AABB* aabb1 = 新 AABB(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f,0.0f,0.0f,0.0f) AABB* aabb2 = 新 AABB(0.0 f,0.0f,0.0f,1.0f,1.0f,1.0f,0.0f,0.0f,0.0f) 有效。
  • AABB* aabb1 = 新 AABB(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f,0.0f,0.0f,0.0f) AABB* aabb2 = 新 AABB(0.0 f,0.0f,0.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f) 不起作用。
  • 基本上当aabb2的xy或z大于0时。
  • 避免使用new java-like sintax

标签: c++ visual-c++


【解决方案1】:

当您创建具有 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 尺寸的盒子) .

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 2015-02-01
      • 2014-02-04
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多