Create a rectangle struct to be rotated and have a .Intersects() function

Posted by MintyAnt on Game Development See other posts from Game Development or by MintyAnt
Published on 2012-01-23T05:39:50Z Indexed on 2012/06/20 21:26 UTC
Read the original article Hit count: 170

Filed under:
|
|

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?

© Game Development or respective owner

Related posts about c#

Related posts about XNA