【问题标题】:Java Rect.intersects() not working sometimesJava Rect.intersects() 有时不工作
【发布时间】:2014-03-17 23:19:37
【问题描述】:

我目前循环遍历我的所有精灵,检查它们是否像这样相互交叉:

for (Sprite s : sprites) {
        if (s.dead) {
            dead.add(s);
        }
        for (Sprite sprite : sprites) {
            if (!sprite.equals(s)) {
                s.collide(sprite, maxX, maxY);
            }
        }
        s.run();

}

精灵检查使用 Rect.intersects() 方法,如下所示:

if (getRect().intersects(s.getRect()))

但有时它只是完全忽略了碰撞,而对象只是相互穿过。

有什么想法吗?

【问题讨论】:

  • 精灵是否移动得如此之快,以至于在实际交叉点之前完成了一次检查,在交叉点之后完成了下一个检查,但没有路口?如有疑问,一些System.out.println 语句(或调试器运行)可能有助于找出问题所在。
  • 我添加了一个检查帧之间的冲突,现在它工作得更好了。

标签: java collision rectangles


【解决方案1】:

您应该尝试将代码更改为

if(getRect().intersects(s.getRect()) || s.getRect().intersects(getRect()))
{
    // They have intersected
}

这样做的原因是,每个矩形的交集方法检查都是唯一的。执行相交检查以查看矩形 a 是否与矩形 b 相交与执行相交检查以查看矩形 b 是否与矩形 a 相交是不同的。

除此之外,你能告诉我更多关于你的矩形的信息吗?他们在旋转吗?他们的移动速度有多快?它们有多大?其他信息也将被完整使用,我可以尝试考虑它们没有发生冲突的其他原因。

【讨论】:

    【解决方案2】:

    我通过使它在框架之间覆盖的区域创建一个矩形来修复它,如下所示:

    private void checkForNextCollision() {
        double boundsWidth = width + dX ;
        if(dX < 0){
            boundsWidth= width - dX ;
        }
    
        double boundsHeight = height + dY ;
        if(dY < 0){
            boundsHeight = height - dY ;
        }
        double boundx = xWorld + dX ;
        double boundy = yWorld + dY ;
        betweenRect = new Rectangle((int)(boundx),(int)(boundy),(int)(boundsWidth), (int)(boundsHeight));
    
    
    }
    

    然后将该矩形与其他精灵中创建的矩形进行检查,以检查下一帧是否应该发生碰撞:

        public void collide(Sprite s, int maxX, int maxY) {
        maxWX = maxX;
        maxWY = maxY;
    
    
        //check for collision with borders
        if (xWorld <= 0) {
            dX = -dX;
            xWorld += 2;
            if(xWorld < -1000){
                dX = 0;
                xWorld += 10;
            }
        }
        if (yWorld <= 0) {
            dY = -dY;
            yWorld += 2;
            if(yWorld < -1000){
                dX = 0;
                yWorld += 10;
            }
        }
        if (xWorld + width >= maxX) {
            dX = -dX;
            xWorld -= 2;
            if(xWorld+width > maxX + 1000){
                dX = 0;
                xWorld -= 10;
            }
        }
        if (yWorld + height >= maxY) {
            dY = -dY;
            yWorld -= 2;
            if(yWorld+height > maxY + 1000){
                dY = 0;
                yWorld -= 10;
            }
        }
    
    
    
        //check for collision with borders
        if(betweenRect.intersects(s.betweenRect)){
            willIntersect = true;
        }else{
            willIntersect = false;
        }
    
        // Use all checks to see if they should collide
        if (getRect().intersects(s.getRect()) || s.getRect().intersects(getRect()) || willIntersect || (xWorld + width > s.xWorld && xWorld < s.xWorld + s.width && yWorld < s.yWorld+s.height && yWorld + height > s.yWorld) ) {
    
                double lastDy = dY;
                double lastsDy = s.dY;
                double lastDx = dX;
                double lastsDx = s.dX;
    
                dY = (((weight - s.weight) / (weight + s.weight)) * lastDy)
                        + (((2.0 * s.weight) / (weight + s.weight)) * lastsDy);
                s.dY = (((s.weight - weight) / (weight + s.weight)) * lastsDy)
                        + (((2.0 * weight) / (weight + s.weight)) * lastDy);
    
                dX = (((weight - s.weight) / (weight + s.weight)) * lastDx)
                        + (((2.0 * s.weight) / (weight + s.weight)) * lastsDx);
                s.dX = (((s.weight - weight) / (weight + s.weight)) * lastsDx)
                        + (((2.0 * weight) / (weight + s.weight)) * lastDx);
    
    
    
                if(willIntersect){
                    willIntersect = false;
                    //s.willIntersect = false;
                }
    
    
            }
    
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      相关资源
      最近更新 更多