Rotating Textures

I have not created my own tutorial for rotating textures with Monogame because some good ones already exist. I do have some clarification about how rotations work around the origin coordinate that I found confusing until I experimented a bit.

Rotating textures could allow some really game mechanics like having your character move towards or shoot at the mouse location, or a fixed point.

Rotating Textures

Here is a good tutorial to start with when Rotating Textures:

http://rbwhitaker.wikidot.com/monogame-rotating-sprites

Here is some more info on rotation if you are not fully understanding the above tutorial:

https://gamedev.stackexchange.com/questions/127688/understanding-spritebatch-draw-overload-xna

Rotating Example:

In my example program, I have a method that returns the angle of rotation required between two points to have a texture point in the right direction. It assumes that the texture is pointed to the right initially. Feel free to use it. I have also put it at the bottom of this page.

Specifying the 'origin' point determines the spot on the texture that will be placed on the draw location point. This is also the point at which your texture will be rotated.

https://github.com/AldworthClass/Monogame-Texture-Rotation

If you are using a Rectangle for Collision, you need to understand that where your Texture is actually drawn, might be in a different location than the Rectangle passed to Draw() if the origin point is not set to (0,0).

The 6th argument of the Draw() method that you will be using to rotate a texture is called the Origin. Changing the 'Origin' allows us to specify what MonoGame calls the ‘origin’ of the image. By default, this is the top-left corner of the image. So when you draw an image to a specified location, it puts the origin point in the texture at that location. By setting this argument to something like the middle of our texture, we will override the ‘origin’ point of the image.

Here is a poor diagram with my attempt at an explanation:

One of the things that I found wasn’t explained well was the idea that when you rotate a texture, the origin point you specify to rotate your sprite around will be placed where the location of your Rectangle or Vector2 is, and then it will be rotated.

Finding the Angle of Rotation Method:

public float GetAngle(Vector2 originPoint, Vector2 secondPoint)

{

float rise = secondPoint.Y - originPoint.Y;

float run = secondPoint.X - originPoint.X;


// First or Fourth Quadrant

if (originPoint.X <= secondPoint.X && originPoint.Y <= secondPoint.Y || originPoint.X <= secondPoint.X && originPoint.Y >= secondPoint.Y)

return (float)Math.Atan(rise / run);

//Second or Third Quadrant

else

return (float)(Math.PI + Math.Atan(rise / run));

}