In my XNA program, I am trying to swing a sword. The sword starts at an angle of 180 degrees, then rotates (clockwise) to an angle of 90 degrees.

The Rectangle struct that XNA provides,

Rectangle mAttackBox = new Rectangle(int x, int y, int width, int height);

However, this struct has two problems:

  1. Holds position and size in Integers, not Floats
  2. Cannot be rotated

I was hoping someone could help me in either telling me that i'm wrong and the Rectangle can be used for both these methods, or can lead me down the right path for rotating a rectangle.

I know how to create a Struct. I believe that I can make methods like classes. I can determine the 4 vertices of a 2D rectangle by calculating out the x,y of the other 3 given the length, width. I'm sure theres a Matrix class I can use to multiply each point against a Rotation matrix.

But once i have my 4 vertices, I got two other problems:

  • How do I test other rectangles against it? How does .Intersects() work for the rectangle struct?
  • Is this even the fastest way to do it? I'd be constantly doing matrix multiplication, wouldnt that slow things down?
  • 2
    \$\begingroup\$ You should consider simply having 2-4 points along the sword, and then check if any of those points are inside whatever objects are in front of your character. Easier and faster both to implement and to compute. \$\endgroup\$
    – William
    Commented Jan 23, 2012 at 6:05
  • 1
    \$\begingroup\$ Hate just dropping a link but the blog post covers just about everything. xnadevelopment.com/tutorials/rotatedrectanglecollisions/… \$\endgroup\$ Commented Jan 23, 2012 at 6:08
  • \$\begingroup\$ Do you require only to check collisions between axis-aligned rects? Or arbitrarily-rotated rects as well? There are other ways aside from rotation matrices. \$\endgroup\$
    – Engineer
    Commented Jan 24, 2012 at 10:09

1 Answer 1


First you want to create the matrix transformation that will rotate the rectangle.

Matrix transform = 
Matrix.CreateTranslation(new Vector3(-pivot, 0.0f)) *
Matrix.CreateRotationZ(angle) *
Matrix.CreateTranslation(new Vector3(position, 0.0f));

To calculate the rotated rectangle:

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));

Just use Rectangle.Intersects to see if it intersects with another rectangle. Stole this code from XNA transformed collision tutorial. http://create.msdn.com/en-US/education/catalog/tutorial/collision_2d_perpixel_transformed

  • \$\begingroup\$ What is the pivot variable? Is it the origin? \$\endgroup\$
    – Daniel G
    Commented Jan 21, 2017 at 20:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .