【问题标题】:rectangle collision detection not working矩形碰撞检测不起作用
【发布时间】:2013-07-28 11:29:43
【问题描述】:

我有一个带有子弹和怪物的游戏,其中我需要在使用矩形碰撞时释放子弹和怪物,但是当发生碰撞时,该方法不会返回 true

这是我所做的: 我为每个子弹/怪物设置界限

bounds.left = (int)X;
bounds.top = (int)Y ;
bounds.right = (int)X + monsterWidth;
bounds.bottom = (int)Y + monsterHeight;

然后做了一个布尔方法

return bounds.intersect((int)X,(int) Y ,(int) X + monsterWidth, (int) Y + monsterHeight);

我对两者都有这个界限,然后我在游戏线程上调用它们

int i=0, j=0;
        while (!monster.isEmpty() && monster.size()>i){
            j=0;
            while (!bullets.isEmpty() && bullets.size()>j){
                if(monster.get(i).isColliding(bullets.get(j).getBounds())){
                    Log.v("collided", "Collided!");
                    monster.get(i).setRelease(true);
                    bullets.get(j).setRelease(true);
                }
                j++;
            }
            i++;
        }

我在每个边界上都有日志,它们都是正确的 left

【问题讨论】:

  • 它应该是轴对齐(不旋转)矩形到矩形碰撞检查吗?
  • 是的,它是轴对齐的,相交仅用于旋转矩形?
  • 我不确定,因为我从来没有使用过它,我只是做了我自己的相交检查方法,我在下面向你展示了。看看吧
  • 我将 if 语句更改为你所说的,但我仍然没有得到日志...
  • 好吧,问题都是关于碰撞检测不起作用,而我向您展示的检测方法正在正常工作。除非您没有将其正确应用于您的代码,否则这意味着问题出在其他地方。确保您传递给碰撞检测方法的坐标是正确的,并代表您在屏幕上实际获得的坐标。如果这又不是问题,那么您应该仔细检查您的游戏逻辑。

标签: android collision rectangles


【解决方案1】:

这是我的矩形到矩形碰撞检查方法(假设两个矩形都是轴对齐的)

where x1 is x start
where x2 is x1 + rect width
where y1 is y start
where y2 is y1 + rect height

_x1, _x2, _y1, _y2 使用相同的公式,但代表第二个矩形。

bool boolResult = false;

    if (x2 >= _x1 && x1 <= _x2 && y2 >= _y1 && y1 <= _y2)
    {
        boolResult = true;
    }

return boolResult;

它对我有用,所以看看它是否适合你

【讨论】:

    【解决方案2】:

    我已经通过更改解决了这个问题:

    return bounds.intersect
    

    bounds.contains(r)
    

    就是这样~

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多