2

So, I have been making my MiniLD game and now I face with another problem. I am making a pong clone using slick2d, and when the ball hits my paddle in a certain range, it just goes through it. Here is the code I have (I can't use the intersects() method because it is an image)

    if (ballY >= paddlePlayerY && ballY <= paddlePlayerY + paddlePlayer.getHeight() && ballX <= paddlePlayerX + 10) {
        ballVelocity.x = -ballVelocity.getX();
        System.out.println("Hit");
    }

Edit: I managed to fix that, but now I can get the ball stuck behind the paddle, giving the CPU a load of points.

pta2002
  • 185
  • 1
  • 3
  • 12
  • I assume the player paddle is on the left hand side of the screen? – rdans Mar 27 '15 at 12:34
  • yep it is. Also, it is 128 pixels high – pta2002 Mar 27 '15 at 12:37
  • is it a variable frame rate? Is it possible that the ball would still be in collision range on the next frame following the first collision? If so it would reverse x velocity again. Also I think you need to deduct the ball height from the very first condition i.e. ballY - ballHeight >= paddlePlayerY – rdans Mar 27 '15 at 12:42
  • what do you mean? I am using VSync for the framerate. I am gonna check how many "Hit" messages I get though. I will give an update soon – pta2002 Mar 27 '15 at 12:44
  • UPDATE: I could not recreate the bug, but I noticed that I only get the "Hit" message when the ball hits the top part of the paddle – pta2002 Mar 27 '15 at 12:46
  • For your second issue, the ball will get stuck because you check if the ball is anywhere to the left of the paddle, and not just within the 10 pixel width. adding another condition should fix it: (ballX > paddlePlayerX). If you still get the issue it probably means your condition is met over more than one frame – rdans Mar 27 '15 at 13:00

0 Answers0