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(cyan 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.

public void BallWCollision()
{
    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.

public void playerBallCollision(Ball b)
{
    if(player.getBounds().intersects(b.getBounds())){  //if player and ball touch each other
     b.setDy(-b.getDy());
    }
}

5d2560c01df7baef31079685ee42901f

How big are dx and dy? If, for example they ate 10, then your ball could be 9 or 10 pixels inside the player in just one clock interval.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.