# XNA Rotating a Rectangle?

I am in the process of making a giant shooter game and I have got to the point where I needed to use rectangles to detect bullets and giants hitting the player. I did that however, if you look at this image:

It shows the giant and the players rectangle, as you can see they are not rotating with the player, any idea how to fix this? Also yes I know they are no the same size as the sprites, that is because I set the size as they were to big before.

Thanks for any help :)

• Maybe if you added the code that adds the rectangle we'd be more able to help you. My first suggestion would be to add the rectangle under the transform node (the same that transforms your player and giant sprites). – Vaillancourt May 29 '15 at 19:34
• 'hitBox = new Rectangle((int)position.X, (int)position.Y, 30, 30);' This is the same how I do it do it for the main player. I just not sure how to rotate it. That code basically just makes a new rectangle using the Vector2 which is the x and y position of the sprite. Also yeah I posted it too early sorry. – Hash One May 29 '15 at 20:01
• Please edit your question. Without knowing where that line of code is, there is not much we can do. – Vaillancourt May 29 '15 at 20:02
• For the collisions to work with rotated rectangles you will need a custom made function that checks for collisions of rotated rectangles. XNA doesn't come with one. As for drawing a rotated rectangle, you can use the spriteBatch.Draw() version that has a "rotation" (a float) variable. – dimitris93 May 31 '15 at 18:26
• Would my sample on Rotated Rectangle collisions be of any use to you? xnadevelopment.com/tutorials/rotatedrectanglecollisions/… Covers drawing and detecting 2D rectangle collisions. – George Clingerman Jun 2 '15 at 20:56

You might be able to side-step using rectangles altogether. For a top-down shooter you could use bounding circles instead of boxes. Checking collision between circles is much easier and faster than checking collision between non-axis-aligned bounding boxes (just check if the distance squared is less than the sum of the radii squared) and you don't need to bother with rotations.

I assume your rectangle exist out of a X,Y,Width and Height.

First, Create 4 Vector2 for your rectangle:

Vector2 v1 = new Vector2(x,y);
Vector2 v2 = new Vector2(x+width,y);
Vector2 v3 = new Vector2(x,y+height);
Vector2 v4 = new Vector2(x+width,y+height);


These points are the 4 corners of your rectangle. Next apply some simple transformation on the vectors:

v = Vector2.Transform(v, Matrix.CreateRotationZ(MathHelper.ToRadians(30)));//rotates 30 degrees


Edit: CreateRotationZ might be CreateRotationX or CreateRotationY.