Why is my collision detection not working properly?

user6258402

I'm creating the widly-known game Pong and I ran into some collision detection problems: the paddle and the ball collide most of the times but sometimes an odd issue occurs: the collision isn't detected and because of that, the ball gets behind the paddle and not change direction. I can't figure out where's the problem hiding. Any and all help would be appreciated.

Note: edited due to a comment.

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

}
Orin

In your checkCollision method, you have to set the xVelocity back to a positive number. Try this:

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

Think about it this way, if the ball is going left and hits the player, you want it to go back to the right. X coordinates go up as they go to the right, so setting the xvelocity to a negative number results in it keep moving through the paddle as if it is not touching it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is my collision detection not working properly?

From Dev

Why isn't my collision detection working properly in pygame?

From Dev

Why isn't my collision detection working in Phaser?

From Dev

Collision Detection in Java 2D Not Working Properly

From Dev

Why are my dplyr group_by & summarize not working properly? (name-collision with plyr)

From Java

Collision detection in pygame not working

From Dev

Collision detection not working?

From Dev

Collision detection does not work properly

From Dev

JQuery collision not working properly

From Dev

Why is my function not working properly?

From Dev

Why is my function not working properly

From Dev

Why is my Constructor not working properly?

From Dev

Why is my code not working properly?

From Dev

Why is my RegexExp not working properly?

From Dev

PyGame border collision detection not working

From Dev

Pygame Jumper collision detection not working

From Dev

Weird behaviour in my collision Detection

From Dev

Collision Detection Javascript Not Working(rather Im not working)

From Dev

Why is my Hover Function not working Properly?

From Dev

Why is my default value for DropDownListFor not working properly?

From Dev

Why my 9 patch not working properly?

From Dev

Why is dequeue not working properly for my code?

From Dev

Why my 9 patch not working properly?

From Dev

Why my button onclick function not working properly ?

From Dev

Why is my python installation not working properly?

From Dev

Why is my Hover Function not working Properly?

From Dev

Cannot figure out why collision detection on one of my enemies won't work

From Dev

2D Collision detection not working

From Dev

Collision Detection hitTestObject not working.. ish

Related Related

HotTag

Archive