c++ - 盒子碰撞代码

标签 c++ collision

我的盒子碰撞代码不工作。

bool checkCollide(int x, int y, int oWidth, int oHeight, int xTwo, int yTwo, int oTwoWidth, int oTwoHeight)
{
   if (xTwo + oTwoWidth < x) 
      return false;  // box 2 is left of box 1

   if (x + oWidth < xTwo)
      return false;  // box 1 is left of box 2

   if (xTwo > x + oWidth) 
      return false;  // box 2 is right of box 1

   if (x > xTwo + oTwoWidth)
      return false;  // box 1 is right of box 2


   if (yTwo + oTwoHeight < y) 
      return false;  // box 2 is up of box 1

   if (y + oHeight < yTwo)
      return false;  // box 1 is up of box 2

   if (yTwo > y + oHeight) 
      return false;  // box 2 is down of box 1

   if (y > yTwo + oTwoHeight)
      return false;  // box 1 is down of box 2

   return true;
}

我的盒子中的一个明显越过了另一个,但是什么也没有发生。 由于某种原因,它似乎返回 false。我正在检查一个“矩形”是否超出了另一个“矩形”的范围,如果没有则返回 true。为什么它不起作用?

最佳答案

这是我的做法(我会提供一个 AABB 对象而不是槽)

bool checkCollide(int x, int y, int oWidth, int oHeight, int xTwo, int yTwo, int oTwoWidth, int oTwoHeight)
{
    // AABB 1
    int x1Min = x;
    int x1Max = x+oWidth;
    int y1Max = y+oHeight;
    int y1Min = y;

    // AABB 2
    int x2Min = xTwo;
    int x2Max = xTwo+oTwoWidth;
    int y2Max = yTwo+oTwoHeight;
    int y2Min = yTwo;

    // Collision tests
    if( x1Max < x2Min || x1Min > x2Max ) return false;
    if( y1Max < y2Min || y1Min > y2Max ) return false;

    return true;
}

事实上,您可以直接用值代替 x1Max 等,而不是使用临时变量。我添加它们是为了便于阅读。

bool checkCollide(int x, int y, int oWidth, int oHeight, int xTwo, int yTwo, int oTwoWidth, int oTwoHeight)
{
    if( x+oWidth < xTwo || x > xTwo+oTwoWidth ) return false;
    if( y+oHeight < yTwo || y > yTwo+oTwoHeight ) return false;

    return true;
}

请注意,此功能仅适用于 2D 框,但它只需要多一 strip z 轴的测试线即可与 3D 兼容。

------------

现在让我们看一下您的代码并附上一些注释

bool checkCollide(int x, int y, int oWidth, int oHeight, int xTwo, int yTwo, int oTwoWidth, int oTwoHeight)
{
   if (xTwo + oTwoWidth < x)    //(1) if( x2Max < x1Min )
      return false;

   if (x + oWidth < xTwo)       //(2) if( x1Max < x2Min )
      return false;

   if (xTwo > x + oWidth)       //(3) if( x2Min > x1Max ) ==> if( x1Max < x2Min ) ==> (2)
      return false;

   if (x > xTwo + oTwoWidth)    //(4) if( x1Min > x2Max ) ==> if( x2Max < x1Min ) ==> (1)
      return false; 


   if (yTwo + oTwoHeight < y)   //(5) if( y2Max < y1Min )
      return false;

   if (y + oHeight < yTwo)      //(6) if( y1Max < y2Min )
      return false;

   if (yTwo > y + oHeight)      //(7) if( y2Min > y1Max ) ==> if( y1Max < y2Min ) ==> (6)
      return false;

   if (y > yTwo + oTwoHeight)   //(8) if( y1Min > y2Max ) ==> if( y2Max < y1Min ) ==> (5)
      return false;

   return true;
}

如您所见,一半的测试不是必需的。 考虑到 X 轴,您的第一个测试与第四个相同;你的第二个测试与第三个相同。

------------

但是,我无法找出当框重叠时您的函数不返回 true 的任何原因。它应该有效。

我测试了以下值

std::cout << checkCollide(5, 5, 2, 2, 0, 0, 3, 3) << std::endl; // false no collision
std::cout << checkCollide(5, 5, 2, 2, 4, 4, 3, 3) << std::endl; // true collision

它返回 false,如预期的那样返回 true。

关于c++ - 盒子碰撞代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6083626/

相关文章:

c++ - 模板化类中的关系运算符重载 (C++)

C++ 静态库链接器错误

c++ - -fPIC 仅适用于共享库吗?

Java Map::hashCode() 冲突 - 为什么?

python - 在 Pygame 中将一条线作为 Sprite 并具有自己的碰撞

c++ - GTKmm3 (GTK+ 3 C++) 编译但在运行时抛出 GLib-GIO-CRITICAL 错误

C++ 异常抛出 : read access violation. 这是 nullptr

ios - UIimageview适合圆形

mysql - 如何更改 MySQL 中每个表中每个字段的冲突?

java - 检测到碰撞后如何处理