1. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Pong--paddle collision algorithm help

hello all! I'm not sure whether this goes in this section of the forum, but here goes it!

I'm new to the forum, so forgive me if i'm asking too much or if i'm asking something unreasonable. For my APCS class, we were told to make one of three games. I choose the hardest of the three, pong. We were supplied with some basic code as an outline to start off of. Currently, i've created a game of pong were the left and right paddle move properly, but the ball does not bounce off of the right paddle correctly. If i manage to get the ball to the left side, if bounces off the paddle if the paddle is in its course. the ball sticks to the left or right wall if it hits it. it bounces off of the the top and bottom walls as it's supposed to. the code is attached below. There are six classes that i use for this. In the actual "pong" class (not the class "the game") the algorithm for detecting if the ball hits the right paddle is off (like i said before). Can anyone help? Cheers!

the algorithm that needs fixing is as follows:

Java Code:
```if(  (ball.getX() <=  rightPaddle.getX() + rightPaddle.getWidth() + Math.abs(ball.getXSpeed())
&& (ball.getY() >= rightPaddle.getY()
|| ball.getY() + ball.getHeight() >= rightPaddle.getY()
&& ball.getY() + ball.getHeight()  < rightPaddle.getY() + rightPaddle.getHeight())))
{

if(ball.getX() >= rightPaddle.getX() + rightPaddle.getWidth() - Math.abs(ball.getXSpeed()))
{
ball.setYSpeed(-ball.getYSpeed());
//System.out.println("changing y speed");
}
else
{
ball.setXSpeed(-ball.getXSpeed());
//System.out.println("changing x speed");
}
}```

**EDIT** for some reason i can't open my manage attachments page.....i'll post the files if someone asks for them!
Last edited by sora628; 02-03-2013 at 11:53 PM.

2. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: Pong--paddle collision algorithm help

Hi Sora, welcome to the forums.

As it is homework I would expect there would be few who would directly answer your question. Instead I'm hoping I can point you in the right direction to solve this yourself.
When you said the ball does not bounce off the right paddle correctly, what does it do?

There are a couple of things you could look at.

1) You have already said the code for the left paddle is working correctly, so what is different about this code? It shoud just be a mirror of what you have already done.

2) Have you tried debugging the code? Stepping through would allow you to see the values of each variable and should allow you to see what is happening during a collision.

3) Check the maths. The coordinate system Java uses defines 0,0 as being top left so I don't think the first 'if' statement is doing what you intend it to do. It may be worth providing a brief explaination of what you want it to do.

Regards.

3. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Re: Pong--paddle collision algorithm help

Ronin,

Thanks very much for the response!! Unfortunately, our teacher has been out with either the flu or some winter based illness, and i couldn't go to him to ask him about the code--that's why i came here! When the ball's y value (I'm pretty sure it's the y) lines up with the paddle's y value, the ball moves in the same x direction and just gets stuck at the same y value. And at first I thought it would be a mirror of the left paddle, but the ball just passes through the paddle with the same code. I've tried doing a bit of debugging over the past few days with no luck unfortunately. I've added print statements and that didn't help me because it's hard to tell where the ball actually is when looking at the print statements. The first if statement was actually given to us by our teacher, and to be honest, I couldn't tell you why the values are added and compared the way they are. It's supposed to see if the ball is hitting the paddle. Any ideas on what the problem might be?

4. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Re: Pong--paddle collision algorithm help

umm......spam?

5. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: Pong--paddle collision algorithm help

Hi Sora,

Can you please supply the complete code for this class as well as the ones for ball and rightPaddle.
Can you also confirm the value of rightPaddle.getHeight(). I would expect a negative value for the code below to work.

Java Code:
```ball.getY() >= rightPaddle.getY()
Regards.
Last edited by Ronin; 02-06-2013 at 12:19 PM. Reason: typo

6. ## Re: Pong--paddle collision algorithm help

#### Posting Permissions

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