Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Pong--paddle collision algorithm help

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Pong--paddle collision algorithm help

    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:

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

    Ball.txt
    Block.txt
    Locatable.txt
    paddle.txt
    pong.txt
    TheGame.txt


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong--paddle collision algorithm help

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pong--paddle collision algorithm help

    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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong--paddle collision algorithm help

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pong--paddle collision algorithm help

    Quote Originally Posted by Norm View Post
    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
    Right Paddle Y 156
    Left Paddle Y 100

    Ball X 312
    Ball Y 312
    Right Paddle Y 164
    Left Paddle Y 100

    Ball X 314
    Ball Y 314
    Right Paddle Y 172
    Left Paddle Y 100

    Ball X 316
    Ball Y 316
    Right Paddle Y 180
    Left Paddle Y 100

    Ball X 318
    Ball Y 318
    Right Paddle Y 188
    Left Paddle Y 100

    Ball X 320
    Ball Y 320
    Right Paddle Y 196
    Left Paddle Y 100

    Ball X 322
    Ball Y 322
    Right Paddle Y 204
    Left Paddle Y 100

    Ball X 324
    Ball Y 324
    Right Paddle Y 212
    Left Paddle Y 100

    Ball X 326
    Ball Y 326
    Right Paddle Y 220
    Left Paddle Y 100

    Ball X 328
    Ball Y 328
    Right Paddle Y 228
    Left Paddle Y 100

    Ball X 330
    Ball Y 330
    Right Paddle Y 236
    Left Paddle Y 100

    Ball X 332
    Ball Y 332
    Right Paddle Y 244
    Left Paddle Y 100

    Ball X 334
    Ball Y 334
    Right Paddle Y 252
    Left Paddle Y 100

    Ball X 336
    Ball Y 336
    Right Paddle Y 260
    Left Paddle Y 100

    Ball X 338
    Ball Y 338
    Right Paddle Y 268
    Left Paddle Y 100

    Ball X 340
    Ball Y 340
    Right Paddle Y 276
    Left Paddle Y 100

    Ball X 342
    Ball Y 342
    Right Paddle Y 284
    Left Paddle Y 100

    Ball X 344
    Ball Y 344
    Right Paddle Y 292
    Left Paddle Y 100

    hit Right Paddle
    Ball X 346
    Ball Y 346
    Right Paddle Y 300
    Left Paddle Y 100

    hit Right Paddle
    Ball X 344
    Ball Y 348
    Right Paddle Y 308
    Left Paddle Y 100

    hit Right Paddle
    Ball X 346
    Ball Y 350
    Right Paddle Y 316
    Left Paddle Y 100

    hit Right Paddle
    Ball X 344
    Ball Y 352
    Right Paddle Y 324
    Left Paddle Y 100

    hit Right Paddle
    Ball X 346
    Ball Y 354
    Right Paddle Y 332
    Left Paddle Y 100

    hit Right Paddle
    Ball X 344
    Ball Y 356
    Right Paddle Y 340
    Left Paddle Y 100

    hit Right Paddle
    Ball X 346
    Ball Y 358
    Right Paddle Y 348
    Left Paddle Y 100

    hit Right Paddle
    Ball X 344
    Ball Y 360
    Right Paddle Y 356
    Left Paddle Y 100

    hit Right Paddle
    Ball X 346
    Ball Y 362
    Right Paddle Y 364
    Left Paddle Y 100

    hit Right Paddle
    Ball X 344
    Ball Y 364
    Right Paddle Y 372
    Left Paddle Y 100

    Ball X 346
    Ball Y 366
    Right Paddle Y 380
    Left Paddle Y 100

    Ball X 348
    Ball Y 368
    Right Paddle Y 388
    Left Paddle Y 100

    Ball X 350
    Ball Y 370
    Right Paddle Y 396
    Left Paddle Y 100

    Ball X 352
    Ball Y 372
    Right Paddle Y 404
    Left Paddle Y 100

    Ball X 354
    Ball Y 374
    Right Paddle Y 404
    Left Paddle Y 100

    Ball X 356
    Ball Y 376
    Right Paddle Y 404
    Left Paddle Y 100

    Ball X 358
    Ball Y 378
    Right Paddle Y 404
    Left Paddle Y 100

    Ball X 360
    Ball Y 380
    Right Paddle Y 404
    Left Paddle Y 100

    Ball X 362
    Ball Y 382
    Right Paddle Y 404
    Left Paddle Y 100

    Ball X 364
    Ball Y 384
    Right Paddle Y 404
    Left Paddle Y 100

    Ball X 366
    Ball Y 386
    Right Paddle Y 404
    Left Paddle Y 100

    Ball X 368
    Ball Y 388
    Right Paddle Y 404
    Left Paddle Y 100

    Ball X 370
    Ball Y 390
    Right Paddle Y 404
    Left Paddle Y 100

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong--paddle collision algorithm help

    It'd be easier to read if the 4 values were on one line.
    Ball X=310 Y=310, Right Paddle X=??, Y=404 Left Paddle X=??? Y=100

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pong--paddle collision algorithm help

    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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong--paddle collision algorithm help

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pong--paddle collision algorithm help

    I don't quite follow---what will this tell me?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong--paddle collision algorithm help

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pong--paddle collision algorithm help

    I already tried the random changes, and of course, that didn't work so i'll try doing the other print statements later

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Pong--paddle collision algorithm help

    Ok, I'll be back tomorrow.


    Also posted at: http://www.java-forums.org/new-java/...ithm-help.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Pong game - Collision detection
    By Hokap in forum Java Theory & Questions
    Replies: 73
    Last Post: May 13th, 2012, 04:11 PM
  2. Has the ball hit my paddle?
    By JakkyD in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 3rd, 2012, 01:16 PM
  3. [SOLVED] Pong: Ricochet off Paddle Issue
    By Staticity in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2011, 06:57 AM
  4. breakout paddle algorithm
    By Brain_Child in forum Algorithms & Recursion
    Replies: 0
    Last Post: December 30th, 2009, 05:24 AM
  5. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 01:19 PM

Tags for this Thread