1

I've recently started coding a game in XNA Game Studio using C#, however I am having trouble detecting when a ball hits a particular block, could someone help.

Note: blocksPosition is an Array, as there is multiple of them

Here is my code:

public Boolean DetectBallBox(int BoxNum)
    {
        if (blocksPosition[BoxNum].X >= ballPosition.X + ball.Width && blocksPosition[BoxNum].Y >= ballPosition.Y + ball.Height)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public void detectBallCollision()
    {
        for (int blockCount = 0; blockCount < 40; blockCount++)
        {
            if (DetectBallBox(blockCount))
            {
                Console.WriteLine("Block Hit");
            }
        }
    }
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
William Smith
  • 137
  • 2
  • 3
  • 11

1 Answers1

0

Simple solution:

use Rectangle.Intersects:

Rectangle x = new Rectangle(ball.x, ball.y, ball.width, ball.height);
Rectangle y = new Rectangle(x,y, width, height);
return x.Intersects(y);

Thats really simple answer, becouse ball is more like ellipse... :)

Better solution

Finding intersection between Rectangle and Ellipse:

System.Drawing.Rectangle brick = new Rectangle();
int ballX, ballY, radiusX, radiusY;

brick.Width = // dont see it in your code
brick.Height = // dont see it in your code
brick.X = blocksPosition[BoxNum].X;
brick.Y = blocksPosition[BoxNum].Y;

ballX = ballPosition.X;
ballY = ballPosition.Y;

radiusX = ball.Width;
radiusY = ball.Height;

// find closest point:
var closestX = Math.Min(brick.Width + brick.X, Math.Max(ballX, brick.X));
var closestY = Math.Min(brick.Height + brick.Height, Math.Max(ballY, brick.Y));

// find distance:
var distanceX = ballX - closestX;
var distanceY = ballY - closestY;

// if distance is lesser then circle radius, then we have intersaction:
var dSq = ((distanceX * distanceX) + (distanceY * distanceY))
if (dSq < (radiusX*radiusX))
{
  return dSq < (radiusY*radiusY);
}
return false;

The Best solution (©®™):

Use any 2d physics engine, like, lets say Farseer. Its more to learn from start, but in end, it will be much better and easier for you.

Quick and Dirty example of Ball hitting brick will looks like this:

World world = new World(new Vector2(0f, 0f)); // no gravity

Body myBall = world.CreateBody();
myBall.BodyType = BodyType.Dynamic;
CircleShape circleShape = new CircleShape(0.5f);
Fixture fixture = myBall.CreateFixture(circleShape);

Fixture brick = FixtureFactory.CreateRectangle();
// etc... Setting Position and so on.
// Its big topic to discuss here whole project.

and every frame:

world.Step(0.033333f); // 60 fps

In final project, you will have two "spaces" - one physical (where you/farseer will count collisions, forces etc) and second one, where you draw your bodies from physic world (btw - Body is only point in space, Shape is any shape / polygon and Fixture is what is holding Body and Shape together... As i say, its really big topic. Feel free to ask any other question about Farseer :) )

Jan 'splite' K.
  • 1,667
  • 2
  • 27
  • 34