1

I've found no way of creating a 2D collision AABB on google, and I was wondering how the maths of it work. I was thinking that I could use some kind of matrix transforms to achieve it but that idea failed epically. So basically, I want to know of any resource that can help me create an Angle-Aligned Bounding Box and check if a point intersects it, or an explanation of the maths and logic of it.

Edit: I don't know if I made it clear, but I need to test collisions with them. Just to make that crystal clear.

annonymously
  • 4,708
  • 6
  • 33
  • 47
  • I've now ported this: http://www.asawicki.info/news_1376_calculating_aabb_of_a_rotated_2d_rectangle.html but I can't figure out how to make it work. – annonymously Dec 23 '11 at 07:39
  • What are you trying to make an AABB around? Is it a simple shape like a rectangle or a circle, or is it something complicated like a sprite? – David Brown Dec 23 '11 at 07:39
  • @David I just need to make it for my physics engine. It should encompass anything I want it to when I make my games. But for now I'll keep it to a rotated rectangle – annonymously Dec 23 '11 at 07:40
  • That is very vague. To answer your question we need more specifics. Can don't you post a short example of the kind of objects you have and what you've tried so far? – David Brown Dec 23 '11 at 08:05
  • @DavidBrown well actually I don't need specifics, just the logic behind how they work and how to find if a point intersects the aabb – annonymously Dec 23 '11 at 08:09
  • Figuring out if a point intersects with an AABB is a completely different question that what you appear to be asking. If that is your question you should change your question's title and contents to reflect that. – David Brown Dec 23 '11 at 08:48
  • @DavidBrown I've edited my question and title. – annonymously Dec 23 '11 at 08:55
  • I've never heard of "angle-aligned bounding boxes". I'm pretty sure you're either talking about OBBs (oriented bounding boxes) or AABBs (axis aligned bounding boxes). If you clear that up, I'll gladly post a solution. – Tara Mar 08 '14 at 10:58

1 Answers1

1

An intersection check between two Rectangles does not allow you to check against a rotated rectangle. The as the simple intersects function will not work. Your idea of calculating a Rectangle that encompasses your "actual" rotated Rectangle, and then checking for collisions on the Bounding Rectangles instead.

Here is some code that will return a Rectangle based on another Rectangle and a Rotation Matrix transformation:

public static Rectangle CalculateBoundingRectangle(Rectangle rectangle, Matrix transform)
{
    // Get all four corners in local space
    Vector2 leftTop = new Vector2(rectangle.Left, rectangle.Top);
    Vector2 rightTop = new Vector2(rectangle.Right, rectangle.Top);
    Vector2 leftBottom = new Vector2(rectangle.Left, rectangle.Bottom);
    Vector2 rightBottom = new Vector2(rectangle.Right, rectangle.Bottom);

    // Transform all four corners into work space
    Vector2.Transform(ref leftTop, ref transform, out leftTop);
    Vector2.Transform(ref rightTop, ref transform, out rightTop);
    Vector2.Transform(ref leftBottom, ref transform, out leftBottom);
    Vector2.Transform(ref rightBottom, ref transform, out rightBottom);

    // Find the minimum and maximum extents of the rectangle in world space
    Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop),
                                Vector2.Min(leftBottom, rightBottom));
    Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop),
                                Vector2.Max(leftBottom, rightBottom));

    // Return that as a rectangle
    return new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y));
}

Of course, this type of collision detection is fast, but not accurate. Your Bounding rectangle will not be a 1:1 representation of your shape. If you need more accuracy, you could then use a per-pixel collision check after checking with AABB.

This answer might also be of interest to you, particularly the answer on SAT.

Community
  • 1
  • 1
jgallant
  • 11,143
  • 1
  • 38
  • 72
  • Well actually I was really more interested in checking collisions between a rotated rectangle and a point, not calculating a new bounding-box around the rotated rectangle, but thanks anyway. – annonymously Jan 01 '12 at 11:07