Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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.

share|improve this answer

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

share|improve this answer
    
Pretty sure that rotation should have a + before the Y * cos term. –  Andrew Russell Jun 17 '11 at 1:32

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.

share|improve this answer

Your Answer

 
discard

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.