i am trying to make a breakout game where you have a paddle and ball bounces around breaking bricks.
the problem is that if ball touches the corner of player(green paddle). than it goes inside of player. i could not understand why. only thing it might be bc of all the dx, -dx, dy, -dy's. i am doing this so it would change ball postion. so it looks like it bounces of the player or window.
here i am check collision between ball and window. x,y,dx,dy are ball postion and speed.
here i am check collision between player and ball. if ball touches playeer than change ball dy.
the problem is that if ball touches the corner of player(green paddle). than it goes inside of player. i could not understand why. only thing it might be bc of all the dx, -dx, dy, -dy's. i am doing this so it would change ball postion. so it looks like it bounces of the player or window.
here i am check collision between ball and window. x,y,dx,dy are ball postion and speed.
Code:
public void BallWCollision()
{
//dx, dy is ball speed
if(x <= 0){ //if ball goes right of screen
dx = -dx;
}
else if(x >= Main.WINDOW_WIDTH){ //if ball goes left of screen
dx = -dx;
}
if(y <= 0){ //if ball goes top of screen
dy = -dy;
}
else if(y >= Main.WINDOW_HEIGHT){ //if ball goes bottom of screen
dy = -dy;
}
}
here i am check collision between player and ball. if ball touches playeer than change ball dy.
Code:
public void playerBallCollision(Ball b)
{
if(player.getBounds().intersects(b.getBounds())){ //if player and ball touch each other
b.setDy(-b.getDy());
}
}
Comment