0

in a BrickBreaker-game I have the following code to check the collision of the ball with the bar:

if(ball.x + ball.getWidth() > bar.x && ball.x < bar.x + (bar.getWidth()/5)) {
        ...
    }

    else if(ball.x + ball.getWidth() > bar.x + (bar.getWidth()/5) && ball.x < bar.x + (2*bar.getWidth()/5)) {
        ...
    }

    else if(ball.x + ball.getWidth() > bar.x + (2*bar.getWidth()/5) && ball.x < bar.x + (3*bar.getWidth()/5)) {
        ...
    }

    else if(ball.x + ball.getWidth() > bar.x + (3*bar.getWidth()/5) && ball.x < bar.x + (4*bar.getWidth()/5)) {
        ...
    }

    else if(ball.x + ball.getWidth() > bar.x + (4*bar.getWidth()/5) && ball.x < bar.x + bar.getWidth()) {
        ...
    }

but still if the ball doesn't hit the bar, it says, that a collision happened. What's wrong with my collision checking?

Stupe
  • 305
  • 1
  • 2
  • 11

1 Answers1

0

What's wrong with my collision checking?

Wierd math. Check this Circle-Rectangle collision detection (intersection)

Community
  • 1
  • 1
Olsavage
  • 1,118
  • 8
  • 11
  • Oh, sorry. I think, I've understood what you want to achieve. What about bar.y and ball.y? This code seems correct for me. – Olsavage Aug 07 '12 at 20:09
  • ty but i already solved my problem ;) I have bar.y and ball.y checking in my code but it didnt worked. I solved it with a complex mathematic calculation :) – Stupe Aug 08 '12 at 11:40