为什么我的碰撞检测不正常?

时间:2016-05-20 18:53:06

标签: java jframe jpanel collision-detection pong

我正在创造一个广为人知的游戏 Pong ,我遇到了一些碰撞检测问题:桨和球大部分时间碰撞但有时会出现奇怪的问题:碰撞不是'检测到并因此,球落在桨的后面而不改变方向。我无法弄清楚隐藏的问题在哪里。任何和所有的帮助将不胜感激。

注意:由于评论而被修改。

GamePanel.java

public class GamePanel extends JPanel implements ActionListener, KeyListener {

    Ball ball = new Ball();
    Player player = new Player();

    private void update() {
        checkCollision();
    }

    @Override
    public void actionPerformed(ActionEvent a) {
        update();
    }

    public void checkCollision() {
        Rectangle playerRect = player.bounds();
        Rectangle ballRect = ball.bounds();
        if (playerRect.intersects(ballRect)) {
            if (ball.getyVelocity() < 0) {
                ball.setxVelocity(5);
                ball.setyVelocity(-5);
            } else if (ball.getyVelocity() > 0) {
                ball.setxVelocity(-5);
                ball.setyVelocity(5);
            }
        }
    }

}

Ball.java

public class Ball {

    private final int RADIUS = 15;
    private final int DIAMETER = RADIUS * 2;

    private int x_pos = 250;
    private int y_pos = 250;

    private int xVelocity = -5;
    private int yVelocity = -5;

    public void update() {
        x_pos += xVelocity;
        y_pos += yVelocity;

        if (x_pos <= 0 + RADIUS) {
            xVelocity = 5;
        } else if (x_pos >= Pong.WINDOW_WIDTH - (5 + RADIUS)) {
            xVelocity = -5;
        }

        if (y_pos <= 0 + RADIUS) {
            yVelocity = 5;
        } else if (y_pos >= Pong.WINDOW_HEIGHT - (15 + DIAMETER)) {
            yVelocity = -5;
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillOval(x_pos - RADIUS, y_pos - RADIUS, DIAMETER, DIAMETER);
    }

    public Rectangle bounds() {
        return (new Rectangle(x_pos - RADIUS, y_pos - RADIUS, DIAMETER, DIAMETER));
    }

    public int getxVelocity() {
        return xVelocity;
    }

    public int getyVelocity() {
        return yVelocity;
    }

    public void setxVelocity(int xVelocity) {
        this.xVelocity = xVelocity;
    }

    public void setyVelocity(int yVelocity) {
        this.yVelocity = yVelocity;
    }

}

Player.java

public class Player {

    private final int WIDTH = 15;
    private final int HEIGHT = 150;

    private final int X_POS = 5 + WIDTH / 2;

    private int y_pos = Pong.WINDOW_HEIGHT / 2 - HEIGHT / 2;

    private int yVelocity = 0;

    public void update() {
        y_pos += yVelocity;

        if (y_pos < 0 + HEIGHT / 2) {
            y_pos = 0 + HEIGHT / 2;
        } else if (y_pos > Pong.WINDOW_HEIGHT - 25 - HEIGHT / 2) {
            y_pos = Pong.WINDOW_HEIGHT - 25 - HEIGHT / 2;
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(X_POS, y_pos - HEIGHT / 2, WIDTH, HEIGHT);
    }

    public int getyVelocity() {
        return yVelocity;
    }

    public void setyVelocity(int yVelocity) {
        this.yVelocity = yVelocity;
    }

    public int getY_pos() {
        return y_pos;
    }

    public void setY_pos(int y_pos) {
        this.y_pos = y_pos;
    }

    public int getX_POS() {
        return X_POS;
    }

    public int getWIDTH() {
        return WIDTH;
    }

    public int getHEIGHT() {
        return HEIGHT;
    }

    public Rectangle bounds() {
        return (new Rectangle(X_POS, y_pos - HEIGHT / 2, WIDTH, HEIGHT));
    }

}

2 个答案:

答案 0 :(得分:3)

在checkCollision方法中,您必须将xVelocity设置回正数。试试这个:

public void checkCollision() {
      Rectangle playerRect = player.bounds();
      Rectangle ballRect = ball.bounds();
      if (playerRect.intersects(ballRect)) {
          if (ball.getyVelocity() < 0) {
            ball.setxVelocity(5);
            ball.setyVelocity(-5);
        } else if (ball.getyVelocity() > 0) {
            ball.setxVelocity(5);
            ball.setyVelocity(5);
        }
    }
}

以这种方式思考,如果球向左移动并击中球员,你想让它回到右边。 X坐标向右移动,因此将xvelocity设置为负数会导致它继续穿过挡板,就好像它没有接触它一样。

答案 1 :(得分:1)

public void checkCollision() {
    Rectangle playerRect = player.bounds();
    Rectangle ballRect = ball.bounds();
    if (playerRect.intersects(ballRect)) {
        if (ball.getyVelocity() < 0) {
            ball.setxVelocity(5); <-- these should be the same value
            ball.setyVelocity(-5);
        } else if (ball.getyVelocity() > 0) {
            ball.setxVelocity(-5); <-- these should be the same value
            ball.setyVelocity(5);
        }
    }
}

你的问题就在这里。在Pong中,你(球员)只能将球击向对方;如果你在左边,你向右击,反之亦然。因此,在任何碰撞之后,无论进入球的y速度如何,球的x速度应该总是在相同的方向上。但是,由于您根据球的y速度将x速度设置为不同的值,因此其中一种情况会导致球通过球拍。