Search code examples
c#xnacollision-detectiongame-physics

Get collision details from Rectangle.Intersects()


I have a Breakout game in which, at some point, I detect the collision between the ball and the paddle with something like this:

// Ball class
rectangle.Intersects(paddle.Rectangle);

Is there any way I can get the exact coordinates of the collision, or any details about it, with the current XNA API?

I thought of doing some basic calculations, such as comparing the exact coordinates of each object on the moment of the collision. It would look something like this:

// Ball class
if((rectangle.X - paddle.Rectangle.X) < (paddle.Rectangle.Width / 2))
    // Collision happened on the left side
else
    // Collision happened on the right side

But I'm not sure this is the correct way to do it.

Do you guys have any tips on maybe an engine I might have to use to achieve that? Or even good coding practices using this method?


Solution

  • I've found at http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.rectangle.intersect.aspx

    that there are two similar static methods (they differ on the way the value is returned) which as a result give You the intersection which also is a rectangle. This should be enough to do what You want.