0
\$\begingroup\$

I can easily rotate sprites using the Draw method. However I would like to simply rotate a Rectangle (the structure) so that I can get the coordinate of the four corners of the rectangle. How can I do that?

\$\endgroup\$

3 Answers 3

5
\$\begingroup\$

You can't rotate a Microsoft.Xna.Framework.Rectangle. It is by nature an axis aligned structure only.

What do you hope to do with the corners that you want to find? Maybe we could suggest a good way to accomplish your goal.

\$\endgroup\$
2
\$\begingroup\$

You can make for all 4 points something like this:

public static Vector2 RotateAround(this Vector2 source, Vector2 target, float radians)
 {
 Vector2 fromAround = source - target;
 float postRotationX = (float)(fromAround.X * Math.Cos(radians) - fromAround.Y * Math.Sin(radians));
 float postRotationY = (float)(fromAround.X * Math.Sin(radians) - fromAround.Y * Math.Cos(radians));
 return target + new Vector2(postRotationX, postRotationY);
}

Code From MSDN http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector2.aspx

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Pretty sure that rotation should have a + before the Y * cos term. \$\endgroup\$ Commented Jun 17, 2011 at 1:32
0
\$\begingroup\$

If you want to get the coordinates of the rotated corners, you have to transform the coordinates from the non-rotated space to the rotated space. In other words, you have to multiply them by a matrix. I'm not sure the exact xna syntax, but it should be something like this:

float angle = 60;
Matrix rotate = Matrix.CreateRotationZ(angle);    
Vector2 transformedPoint = Vector2.Transform(unTransformedPoint, rotate);

This will rotate the rectangle about it's top left corner. I'm not sure if that's what you want or not, but you can easily change this by adding a translate transform before the rotate transform to change the rotation origin.

\$\endgroup\$

You must log in to answer this question.

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