• 02-03-2013, 11:51 PM
sora628
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:

Code:

```if(  (ball.getX() <=  rightPaddle.getX() + rightPaddle.getWidth() + Math.abs(ball.getXSpeed())                                 && (ball.getY() >= rightPaddle.getY()                                 && ball.getY() <= rightPaddle.getY() + rightPaddle.getHeight()                                 || ball.getY() + ball.getHeight() >= rightPaddle.getY()                                 && ball.getY() + ball.getHeight()  < rightPaddle.getY() + rightPaddle.getHeight())))                 {                         System.out.println("hit Right Paddle");                     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!
• 02-04-2013, 01:28 AM
Ronin
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.

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.
• 02-06-2013, 02:03 AM
sora628
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?
• 02-06-2013, 03:14 AM
sora628
umm......spam?
• 02-06-2013, 12:19 PM
Ronin
```ball.getY() >= rightPaddle.getY() && ball.getY() <= rightPaddle.getY() + rightPaddle.getHeight()```