I've got a simple BreakOut game (vertical, paddle along bottom) up and running but the collision detection is a bit off. I'm using a Rectangle.Intersect to initially check if the ball intersects the given brick at all and then if it does have 4 more Intersect calls to check whether the ball is colliding vertically (side of brick, left+right) or horizontally (top+bottom of brick). On some occasions when the ball collides with a brick at an acute angle near the intersection of 2 horizontally adjacent bricks it removes both of them and continues in an upward direction. I assume this is a double up of collisions. I've forcibly exited the foreach loop which checks for rectangle intersects the moment it finds one but this hasn't resolved issue.
This is in the main Update() function of the game class:
foreach (Brick brick in bricks)
{
if (brick.CheckCollision(ball))
{
break;
}
}
This is the Brick.CheckCollision() function:
public bool CheckCollision(Ball ball)
{
if (alive && ball.Bounds.Intersects(location))
{
alive=false;
ball.Deflection(this);
return true;
}
return false;
}
And this is the Ball.Deflection() function. In this I'm using rectangles with no width/heigth to mark the sides of the brick. Not sure if that is the correct way:
public void Deflection(Brick brick)
{
//For vertical collision (top or bottom of brick) invert Y motion
if (this.Bounds.Intersects(new Rectangle(brick.Location.Left, brick.Location.Top, brick.Location.Right - brick.Location.Left, 0)) ||
this.Bounds.Intersects(new Rectangle(brick.Location.Left, brick.Location.Bottom, brick.Location.Right - brick.Location.Left, 0)))
{
motion.Y *= -1;
return;
}
//For horizontal collision (left or right side of brick) invert X motion
if (this.Bounds.Intersects(new Rectangle(brick.Location.Left, brick.Location.Top, 0, brick.Location.Bottom - brick.Location.Top)) ||
this.Bounds.Intersects(new Rectangle(brick.Location.Right, brick.Location.Top, 0, brick.Location.Bottom - brick.Location.Top)))
{
motion.X *= -1;
return;
}
}
I assume I'm making a blatantly obvious logic error but if the general approach can be improved I'm all ears!