# Thread: Pong Paddle Not Stopping Ball At Certain Speed

1. ## Pong Paddle Not Stopping Ball At Certain Speed

First off, sorry for the long thread title. I was trying to convey my error.

I'm making a pong-like game, and I have everything set up. There is one paddle which the player controls by moving his mouse; if the cursor is on the left half of the screen, the paddle follows, and vice-versa.

Now, the paddles reflect the ball perfectly as long as the ball's "speed" (in pixels per repaint) is less than three. The game starts the ball with a speed of 1, and each time the ball is hit, it increases by one. When the speed reaches three, the ball just flies right past my paddles.

A few notes before my code:
• getXPos() and getYPos() get the top left pixel. I add to or subtract from that amount to get other pixels of the shape.

• The ball is a circle with a radius of 10. The rectangle is 5x40 (5 wide, 40 high).

• A negative value for xSpeed denotes travel to the left; positive to the right.

Here is my code for paddle collision detection:

Java Code:
```// If ball collided with  player's paddle...
if(ball.getXPos() < windowWidth / 2)
{
if(ball.getXPos() == paddle.getXPos() + 5 &&
ball.getYPos() + RADIUS <= paddle.getYPos() + 40))
{
// ... turn ball around and increase speed.
ball.setXSpeed(-(ball.getXSpeed() - 1));
}
}
// If ball collided with opponent's paddle...
else
{
if(ball.getXPos() + (RADIUS * 2) == paddle.getXPos() &&
ball.getYPos() + RADIUS <= paddle.getYPos() + 40))
{
// ... turn ball around and increase speed.
ball.setXSpeed(-(ball.getXSpeed() + 1));
}
}```
My program's complete code is quite long (about 500 lines in total [counting empty newlines and Javadoc]), but if you need me to post it, let me know. Thank you in advance for your help.
Last edited by JDCAce; 10-12-2008 at 02:57 AM. Reason: Typos

2. Try debugging your code by adding some println() statements to see how the values are changing and the execution flow.
Break the mulitple conditions in the if up into single tests when you println them.
With the amount and complexity of the computations, you need to see all of the values and the results of all of the conditions.

3. Thanks for the suggestion, Norm. I found my problem (sort of): ball.getXPos() is returning 150 when clearly the ball is NOT in the 150th column. It's going to take me a bit to figure out why ball.getXPos() is reporting an incorrect position.

EDIT: What's really odd is this always happens on the third paddle hit (well, it would be the third if it didn't go through the paddle). Also, ball.getXPos() is returning 150 every, whether it's on the left or the right. Before the "third" paddle hit, ball.getXPos() is fine. Hmm, wait. windowWidth - (RADIUS * 20) = 150, though I don't think I ever use that equation. And if it says it's at 150, why isn't it at 150?

PS - These are rhetorical questions. I'll probably figure this out myself.

EDIT 2: EUREKA!! I solved it! You see, when the ball is moving at 3 (horizontal) pixels per refresh, it actually skips two pixels! One of the pixels it skips is the pixel I'm testing it for! Since it skips over the x = 20 (it goes from x = 22 to x = 19, when the speed is -3), x != 20! Now I need to find a way to fix it. The hard part is over!
Last edited by JDCAce; 10-12-2008 at 06:16 AM. Reason: Update

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•