• February 3rd, 2013, 04:20 PM
sora628
hello all!

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"); } }```

Attachment 1745
Attachment 1746
Attachment 1747
Attachment 1748
Attachment 1749
Attachment 1750
• February 3rd, 2013, 05:06 PM
Norm
Quote:

the ball does not bounce off of the right paddle correctly.
Can you describe what it does incorrectly?
What do you see when the ball does not properly bounce off the paddle? Can you describe what happens?
Does the ball completely ignore the paddle or does it react to some parts of the paddle or when some parts of the paddle touches some parts of the ball.

Try debugging the code by adding some println statements that print out the ball's and the paddle's locations when the ball gets close to the paddle (use the x locations would be good).

Please post the code in the forum so it can be copied and not have to be downloaded.
• February 3rd, 2013, 09:04 PM
sora628
Hi norm,

When the right paddle moves in front of the ball (when they have the same y values if i'm not mistaken) the ball stutters back and forth and moves in the same direction up or down, but stutters left and right while the right paddle is in it's direct line of sight. I tried putting prints in, but i couldn't debug it. Also tried flipping the greater than and less than signs and it didn't really help. Any ideas?
• February 3rd, 2013, 10:01 PM
Norm
Quote:

Any ideas?
The print out should help you see why the ball stutters when near the paddle. It should show which of the great number of conditions in the if statement is true when the ball changes direction.

Can you post the print outs that show the x & y values for the ball's location and for the paddle's location for when the ball stutters.
• February 5th, 2013, 07:15 PM
sora628
Quote:

Originally Posted by Norm
The print out should help you see why the ball stutters when near the paddle. It should show which of the great number of conditions in the if statement is true when the ball changes direction.

Hi again, just saw the reply. The ball stutters when its y value correlates with the right paddles y value. It isn't a particular y value though, it is for any y value when the two are the same (that's confusing to read---the ball stutters when both the y value for the ball and the paddle are the same, at any location)

print statements are as follows: I just pulled a handful from before the ball intercepts the paddles y intercept, as both y values are the same, and after the y value of the ball are past the paddles y value. It seems that when the y values come between 40 of each other, the ball starts acting up. In the print statement, there is a "hit right paddle" if the first "if" statement which should determine if the paddle actually hits the ball. In this scenario, it prints when their y values are within 40 (if i'm not mistaken)

Ball X 310
Ball Y 310

Ball X 312
Ball Y 312

Ball X 314
Ball Y 314

Ball X 316
Ball Y 316

Ball X 318
Ball Y 318

Ball X 320
Ball Y 320

Ball X 322
Ball Y 322

Ball X 324
Ball Y 324

Ball X 326
Ball Y 326

Ball X 328
Ball Y 328

Ball X 330
Ball Y 330

Ball X 332
Ball Y 332

Ball X 334
Ball Y 334

Ball X 336
Ball Y 336

Ball X 338
Ball Y 338

Ball X 340
Ball Y 340

Ball X 342
Ball Y 342

Ball X 344
Ball Y 344

Ball X 346
Ball Y 346

Ball X 344
Ball Y 348

Ball X 346
Ball Y 350

Ball X 344
Ball Y 352

Ball X 346
Ball Y 354

Ball X 344
Ball Y 356

Ball X 346
Ball Y 358

Ball X 344
Ball Y 360

Ball X 346
Ball Y 362

Ball X 344
Ball Y 364

Ball X 346
Ball Y 366

Ball X 348
Ball Y 368

Ball X 350
Ball Y 370

Ball X 352
Ball Y 372

Ball X 354
Ball Y 374

Ball X 356
Ball Y 376

Ball X 358
Ball Y 378

Ball X 360
Ball Y 380

Ball X 362
Ball Y 382

Ball X 364
Ball Y 384

Ball X 366
Ball Y 386

Ball X 368
Ball Y 388

Ball X 370
Ball Y 390
• February 5th, 2013, 07:44 PM
Norm
It'd be easier to read if the 4 values were on one line.

What is the x values for the paddles?
Also there should be a printout when a collision happens.
what is the size of the ball? What is the size of the paddle?

There shouldn't be any printouts when the ball is at a distance from the paddle. You're only interested in seeing what happens when the ball approaches the paddle and there is a collusion.
• February 5th, 2013, 08:13 PM
sora628
The right and left paddle's x values do not change and are 555, and 0, respectively. There is a print statement when there is a collision-- if you look, there is a print of "hit Right Paddle" in certain circumstances. If it hits the left paddle, there is a print statement "hit Left Paddle". The ball is 10x10 and the paddles are 20x50. Does that help? and why shouldn't i have print statements all the time? I need to know the values either way don't I?
• February 5th, 2013, 08:43 PM
Norm
Quote:

print statements all the time
Reduces output if you don't print values when there isn't a collision about to happen.

For events like collisions add some eye catchers like >>>>>> HIT RIGHT <<<<<<
To save having to do arithmetic in your head printing the x,y values on the side where the collision is going to happen will make it easier to see. When the ball moves to the right, print the right side of the ball and the left side of the paddle. When the ball moves left, print the left side of the ball and the right side of the paddle.
Then overlaps between ball and paddle will be obvious and not require any mental math.
• February 5th, 2013, 09:16 PM
sora628
I don't quite follow---what will this tell me?
• February 5th, 2013, 09:27 PM
Norm
Quote:

what will this tell me?
It will show you where the ball and paddle are when they collide and how their locations change because of the logic of the code. I assumed you wanted to find out what the code was doing so you could change it to act the way you want. One way to see what the code is doing is to print out the objects' locations so you can see where they are when they collide.

Or you can continue to make random changes to the conditions in the if tests until you hit on the correct logic.
• February 5th, 2013, 09:35 PM
sora628