Results 1 to 6 of 6
  1. #1
    sora628 is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Question 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() <= 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!
    Last edited by sora628; 02-03-2013 at 10:53 PM.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default 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. #3
    sora628 is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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. #4
    sora628 is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Pong--paddle collision algorithm help

    umm......spam?

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default 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()
    && ball.getY() <= rightPaddle.getY() + rightPaddle.getHeight()
    Regards.
    Last edited by Ronin; 02-06-2013 at 11:19 AM. Reason: typo

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Pong--paddle collision algorithm help

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Moving a Paddle with the Mouse
    By aortell24 in forum New To Java
    Replies: 2
    Last Post: 08-06-2012, 11:26 PM
  2. Breakout Paddle
    By Newbieprogrammer in forum New To Java
    Replies: 1
    Last Post: 08-05-2012, 02:02 PM
  3. Paddle Class: Trying to code details to ball & paddle..
    By CuppaCoffee in forum New To Java
    Replies: 1
    Last Post: 01-09-2012, 12:14 AM
  4. Let's Check My Collision Detection Algorithm
    By Revenna in forum Java 2D
    Replies: 3
    Last Post: 03-23-2011, 02:44 PM
  5. Pong Paddle Not Stopping Ball At Certain Speed
    By JDCAce in forum Java Applets
    Replies: 3
    Last Post: 04-01-2009, 11:07 PM

Posting Permissions

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