盒子碰撞代码

发布于 2024-11-08 21:03:46 字数 915 浏览 3 评论 0原文

我的盒子碰撞代码不起作用。

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。为什么它不起作用?

My box collision code is not working.

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;
}

On of my boxes is clearly going over the other, however nothing is happening.
It seems to be returning false for some reason. I'm checking if one "rectangle" is out of bounds of the other, and if it isn't return true. Why isn't it working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-11-15 21:03:46

这是我的做法(我会提供一个 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 盒子,但只需要多一条带有 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。

Here is how I would have done it (I would have provided an AABB object instead trough)

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;
}

Indeed you can replace the x1Max etc directly with the values instead of using temporary variables. I added them for readability.

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;
}

Note that this function does work with 2D boxes only, but it only require one more test line with z axis to be 3D compatible.

----------

Now let see your code with some comments on it

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;
}

So as you can see, half the tests are not required.
Considering the X axis, you first test is the same as the fourth one; your second test is the same as the third one.

----------

However, I can't spot any reason why your function does not return true when the box are overlapping. It should work.

I tested with the following values

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

And it return false, and true as expected.

岁月染过的梦 2024-11-15 21:03:46
bool checkCollide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
  if (y1 + h1 < y2
     || y1 > y2 + h2
     || x1 + w1 < x2
     || x1 > x2 + w2) return false;
  return true;
}
bool checkCollide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
  if (y1 + h1 < y2
     || y1 > y2 + h2
     || x1 + w1 < x2
     || x1 > x2 + w2) return false;
  return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文