SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Still have problems with collision detection

    So I temporarily gave up on my breakout game (code) due to getting frustrated and a short trip, but it's time to finish it. After all, this is the only thing that's left to fix/do. The ball does not bounce correctly off the sides (the code in the checkBlocks function). It only/always seems that the first if statement evaluates to true, and when it gets to the second if statement, it doesn't evaluate to true because the other one already reversed the ball's direction. So how can I correctly determine where the ball came from?

    Also, sometimes the ball gets stuck. For example, if you happen to hit the ball with the paddle in the bottom left corner, the ball will bounce back and forth along the left side of the board. I'm not sure how to fix this either.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great work, nemesis. Nice code! :-)

    I can't promise anything but I'll be looking at it when I can.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a reminder that I still need help

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This line:
    Code:
    if((ballLeft + ballWidthHeight) > blocksLeft[count] && (ballLeft - 50) < blocksLeft[count])
    will always be true, because you are doing a more specific version of the exact same if above it:
    Code:
    if ((ballTop - 20) < blocksTop[count] && (ballTop + ballWidthHeight) > blocksTop[count] && (ballLeft + ballWidthHeight) > blocksLeft[count] && (ballLeft - 50) < blocksLeft[count])
    Maybe what you could do after the first if is something like:
    1. create 2 temp variables containing the PREVIOUS x and y position of the ball before it was moved into the block
    2. check to see if the ball was above, below, left or right of the block
    a. if it was above or below the block, assume it hit the top or bottom
    b. if it was to the right or left of the block, assume it hit the right or left side
    c. if neither condition is true, assume it hit the corner of the block

    hope that helps!

    Jim

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it! Kinda frustrating in a way. I actually kept the exact same code, except that now I'm checking the previous position of the ball (as you mentioned) instead of the current position. So it's checking if the previous position of the ball was within a column or a row, instead of checking if it was above or below, or to the left or right (I actually had some problems with this method, the ball ignored some blocks for some reason). But it works!

    One ore small problem though...oh wait, I talked about that already. The ball still gets stuck along the walls if it happens to hit the paddle on the wall.

  7. #7
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great work!


Bookmarks

Posting Permissions

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