Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 554
Daniel Ribeiro Avatar asked Mar 12 '26 18:03

Daniel Ribeiro


1 Answers

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.

like image 146
Grzegorz Sławecki Avatar answered Mar 14 '26 07:03

Grzegorz Sławecki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!