0

I am making a 2d game using the c# (monogame engine). And I need a function that will return true or false depending on if the rotated rectangle is colliding with some other rectangle that can be also rotated.

For now, I have a function that returns true or false depending on if the two rectangles are colliding. But without rotation.

Something like this

public bool Collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
    if (x1 < x2 + w2 &&
        x1 + w1 > x2 &&
        y1 < y2 + h2 &&
        h1 + y1 > y2)
        return true;
    return false;
}

But instead all these parameters I have two objects.

Thanks.

ramix
  • 9
  • 2
  • Duplicate of https://stackoverflow.com/questions/641219/how-can-i-perform-collision-detection-on-rotated-rectangles – Caramiriel Dec 17 '18 at 19:51

2 Answers2

2

Doing collision detection for rotated rectangles is a lot more challenging than for axis-aligned rectangles, but it's not super hard:

The basic idea is that two rectangles intersect if and only if you cannot draw a straight line between them. This sounds quite obvious and is in fact true for any convex shape.

However, we can make things a bit simpler because we're dealing with rectangles: One of these potential dividing lines will be parallel to one of the sides of the rectangle. That leaves 4 possible axis (two for each rectangle). We only know that the line is parallel, not exactly where it is, but there's a trick:

For each line direction, let's create another line perpendicular to that. Then, we project both rectangles on that line. Now we can check if the rectangles overlap.

If they do, we don't know anything, but if they don't, we know that the rectangles definitively don't intersect.

Once we tried all four possible directions and haven't found a separating axis, we know that the rectangles intersect.

The only challenging part is projecting the rectangles onto the line. This is done using the dot product, but you'll probably find better explanations elsewhere. The basic idea is to turn every corner of the rectangles into a number that represents how far up the line it is. Then take the minimum and maximum number for each rectangle and check whether these areas overlap.

I'm sorry I didn't provide any code, but it should be possible to follow these steps to get a working implementations.

I based my answer on this (where you can also find pictures):

https://www.gamedev.net/articles/programming/general-and-gameplay-programming/2d-rotated-rectangle-collision-r2604

jalgames
  • 781
  • 4
  • 23
-2

You can build two Rectangle objects and call IntersectsWith()

public bool Collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
    var rect1 = new System.Drawing.Rectangle(x1,y1,w1,h1);
    var rect2 = new System.Drawing.Rectangle(x2,y2,w2,h2);
    return rect1.IntersectsWith(rect2);
}
Oxymoron
  • 1,380
  • 2
  • 19
  • 47
  • 2
    I don't think that will work, as Rectangle does not store any rotation. What IntersectsWith() is the same thing as the code he already has. – jalgames Dec 17 '18 at 19:52
  • in that case he could apply some Matrix logic around the rotation https://learn.microsoft.com/en-us/dotnet/api/system.drawing.drawing2d.matrix?view=netframework-4.7.2 – Oxymoron Dec 17 '18 at 19:56
  • Ok, Thanks again. – ramix Dec 17 '18 at 19:58