Results 1 to 3 of 3
  1. #1
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Question Pong Paddle Not Stopping Ball At Certain Speed

    First off, sorry for the long thread title. I was trying to convey my error.

    I'm making a pong-like game, and I have everything set up. There is one paddle which the player controls by moving his mouse; if the cursor is on the left half of the screen, the paddle follows, and vice-versa.

    Now, the paddles reflect the ball perfectly as long as the ball's "speed" (in pixels per repaint) is less than three. The game starts the ball with a speed of 1, and each time the ball is hit, it increases by one. When the speed reaches three, the ball just flies right past my paddles.

    A few notes before my code:
    • getXPos() and getYPos() get the top left pixel. I add to or subtract from that amount to get other pixels of the shape.

    • The ball is a circle with a radius of 10. The rectangle is 5x40 (5 wide, 40 high).

    • A negative value for xSpeed denotes travel to the left; positive to the right.


    Here is my code for paddle collision detection:

    Java Code:
    // If ball collided with  player's paddle...
    if(ball.getXPos() < windowWidth / 2)
    {
      if(ball.getXPos() == paddle.getXPos() + 5 &&
         (ball.getYPos() + RADIUS >= paddle.getYPos() &&
          ball.getYPos() + RADIUS <= paddle.getYPos() + 40))
      {
        // ... turn ball around and increase speed.
        ball.setXSpeed(-(ball.getXSpeed() - 1));
      }
    }
    // If ball collided with opponent's paddle...
    else
    {
      if(ball.getXPos() + (RADIUS * 2) == paddle.getXPos() &&
         (ball.getYPos() + RADIUS >= paddle.getYPos() &&
          ball.getYPos() + RADIUS <= paddle.getYPos() + 40))
      {
        // ... turn ball around and increase speed.
        ball.setXSpeed(-(ball.getXSpeed() + 1));
      }
    }
    My program's complete code is quite long (about 500 lines in total [counting empty newlines and Javadoc]), but if you need me to post it, let me know. Thank you in advance for your help.
    Last edited by JDCAce; 10-12-2008 at 03:57 AM. Reason: Typos
    "Things are made of littler things that jiggle."

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,875
    Rep Power
    25

    Default

    Try debugging your code by adding some println() statements to see how the values are changing and the execution flow.
    Break the mulitple conditions in the if up into single tests when you println them.
    With the amount and complexity of the computations, you need to see all of the values and the results of all of the conditions.

  3. #3
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default

    Thanks for the suggestion, Norm. I found my problem (sort of): ball.getXPos() is returning 150 when clearly the ball is NOT in the 150th column. It's going to take me a bit to figure out why ball.getXPos() is reporting an incorrect position.

    EDIT: What's really odd is this always happens on the third paddle hit (well, it would be the third if it didn't go through the paddle). Also, ball.getXPos() is returning 150 every, whether it's on the left or the right. Before the "third" paddle hit, ball.getXPos() is fine. Hmm, wait. windowWidth - (RADIUS * 20) = 150, though I don't think I ever use that equation. And if it says it's at 150, why isn't it at 150?

    PS - These are rhetorical questions. I'll probably figure this out myself.

    EDIT 2: EUREKA!! I solved it! You see, when the ball is moving at 3 (horizontal) pixels per refresh, it actually skips two pixels! One of the pixels it skips is the pixel I'm testing it for! Since it skips over the x = 20 (it goes from x = 22 to x = 19, when the speed is -3), x != 20! Now I need to find a way to fix it. The hard part is over!
    Last edited by JDCAce; 10-12-2008 at 07:16 AM. Reason: Update
    "Things are made of littler things that jiggle."

Similar Threads

  1. Ping pong game
    By adam405 in forum New To Java
    Replies: 8
    Last Post: 10-20-2010, 04:52 PM
  2. stopping thread...using flags
    By rstepler in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 10:36 PM
  3. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 10:08 PM
  4. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 08:02 PM
  5. compare speed
    By bbq in forum JDBC
    Replies: 1
    Last Post: 06-28-2007, 06:34 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
  •