Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from Jan 24 '12 at 9:45

This question came from our site for professional and enthusiast programmers.

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. – William Mariager Jan 23 '12 at 6:05
Hate just dropping a link but the blog post covers just about everything.… – ClassicThunder Jan 23 '12 at 6:08
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. – Arcane Engineer Jan 24 '12 at 10:09

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.