Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

need help with collision between ball and rect. i dont know how to test if ball hit the rec from top, bottom, right or left.

x = ball x
y = ball y
radius = ball width and height
rx = rect x
ry = rect y
rheight = rect height
rwidth = rect width



main game loop:

x += dx;   //keep the ball moving so i can test the collision
y += dy;

//collsion between ball and box
if(x+radius > rx && x < rx+rwidth)
 {
if(y+radius > ry && y < ry+rheight)
{
  if(y+radius == ry) 
    {  
    System.out.println("top");
    }
   else  if(y == ry+rheight) 
    {
     System.out.println("bottom");
    }
   else if(x+radius == rx)
        {
    System.out.println("left");
        }
     else if(x == rx+rwidth)
    {
     System.out.println("right");
    }
   }
}
share|improve this question
    
It is duplicate of gamedev.stackexchange.com/questions/44483/… –  Ivan Kuckir Mar 30 '13 at 0:53
add comment

closed as too localized by bummzack, Sean Middleditch, Byte56, ClassicThunder, Trevor Powell Apr 6 '13 at 4:21

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

The code that you are using does not check for the collisions from right and bottom side, it only checks when the ball collides from top left side.case 1case 2

For this case the above code fails to check for a collision
case 3

take a look at this code its just basic detection not very accurate but you should get the point. Hope this helps.

if (ballLocation.Center.Y > BrickLocation.Y && ballLocation.Center.Y < (BrickLocation.Y + BrickLocation.Height))
            {
                if (BrickLocation.Center.X > ballLocation.Center.X)
                {
                    velocity.X = mod(velocity.X) * -1;
                }
                else if (BrickLocation.Center.X < ballLocation.Center.X)
                {
                    velocity.X = mod(velocity.X);
                }
            }
            else if (ballLocation.Center.X > BrickLocation.X && ballLocation.Center.X < (BrickLocation.X + BrickLocation.Width))
            {
                if (BrickLocation.Center.Y < ballLocation.Center.Y)
                {
                    velocity.Y = mod(velocity.Y);
                }
                else if (BrickLocation.Center.Y > ballLocation.Center.Y)
                {
                    velocity.Y = mod(velocity.Y) * -1;
                }
            }
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.