Java平滑2D矩形碰撞?

时间:2011-10-13 11:40:39

标签: java 2d collision-detection

嘿伙计们我正在制作一个迷宫游戏,我正试图弄清楚如何使2D碰撞变得平滑。目前我有一些非常糟糕的碰撞代码我想到了,因为我还没有完成这部分而且它很脆弱,有时会进入墙内,然后你无法离开。

我的代码:

    public void checkCollision() {
    Rectangle player_rectangle = new Rectangle(player.getX(),player.getY(),32,32);

    for(Wall wall : walls) {

        Rectangle wall_rectangle = new Rectangle(wall.getX(), wall.getY(), 32,32);

        if(player_rectangle.intersects(wall_rectangle)) {
            if(player.xspeed == 1) {
                player.xspeed = 0;
                player.x -= 1.2;
            } else {

                if(player.xspeed == -1) {
                    player.xspeed = 0;
                    player.x += 1.2;
                }
                else
                    if(player.yspeed == 1) {
                        player.yspeed = 0;
                        player.y -= 1;
                    } else {
                        if(player.yspeed == -1) {
                            player.yspeed = 0;
                            player.y += 1;
                        }
                    }

            }
        }

    }

}

碰撞的更好方法是什么?这是我将拥有的地图类型的图片:

enter image description here

修改

新碰撞代码:

            if (player_rectangle.intersects(wall_rectangle)) {
            Rectangle intersection = (Rectangle) player_rectangle.createIntersection(wall_rectangle);

            if (player.xspeed > 0) {
                player.x -= intersection.getWidth();
            }

            if (player.yspeed > 0) {
                player.y -= intersection.getHeight();
            }

            if (player.xspeed < 0) {
                player.x += intersection.getWidth();
            }

            if (player.yspeed < 0) {
                player.y += intersection.getHeight(); 
            }

            Print(Integer.toString(intersection.width) + ", " + Integer.toString(intersection.height));

        }

运动代码:

    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if(key == KeyEvent.VK_LEFT) {

        xspeed = -1;

    } else {
        if(key == KeyEvent.VK_RIGHT) {

                xspeed = 1;

        } else {
            if(key == KeyEvent.VK_UP) {
                yspeed = -1;
            } else {
                if(key == KeyEvent.VK_DOWN) {
                    yspeed = 1;
                }
            }
        }
    }
}

感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用Rectangle.createIntersection(Rectangle)

如果玩家向正方向移动,只需从他的位置减去所得Rectangle的宽度和高度,如果他移动到负数,则添加它们。

不要将速度设置为零,这可能会导致播放器挂起。

修改

if (playerRectangle.intersects(wallRectangle) {
    Rectangle intersection = (Rectangle) playerRectangle.createIntersection(wallRectangle);

    if (player.xspeed > 0) {
        player.x -= intersection.getWidth();
    }

    if (player.yspeed > 0) {
        player.y -= intersection.getHeight();
    }

    if (player.xspeed < 0) {
        player.x += intersection.getWidth();
    }

    if (player.yspeed < 0) {
        player.y += intersection.getHeight();
    }
}

<强> EDIT2

据我所知,keyPressed仅在您按键时触发,而不是在您释放时触发。试试这样:

private boolean up, down, left, right;

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KEyEvent.VK_UP) {
        up = true;
    }

    ...
}

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KEyEvent.VK_UP) {
        up = false;
    }

    ...
}


// Call this in the mainthread
public void updateMovement() {
    if (up) {
        player.xspeed = 1;
    }

    ...
}

所以在每一帧中,你都会更新动作,移动玩家,然后检查碰撞。