# Moving A Sprite In The Direction Its Facing XNA [duplicate]

There are many questions on this site that explain this except they seem to all be in Java. I want to move a sprite in the direction it's facing, in XNA. I know you must use Radians and lots of complicated maths which I am not too familiar with. I tried doing this:

``````position.X += RotationAngle * 5;
``````

This code obviously makes no sense whatsoever, and that's why I need help with it. How can I accomplish the rotation?

-

## marked as duplicate by Byte56♦, bummzack, Trevor Powell, Josh Petrie♦, AnkoApr 8 '13 at 8:31

I suggest taking a bit more time getting some basic trig into you skull. Microsoft provides an excellent example on how to do this. – Dialock Mar 10 '13 at 22:48
@Dialock Do you mind sharing a link please – Battle_Pasture Mar 10 '13 at 23:14
If that level of math is complicated, you may want to brush up on your math. Khan academy is a great resource. Also check out the Wolfire blog on this. – Byte56 Mar 11 '13 at 0:58
I think posting a duplicate question, just because you don't understand how to implement the concept in the language you're using, doesn't justify a duplicate. Ask someone in chat or on one of the discussion oriented sites in the FAQ to explain it to you. – Byte56 Mar 11 '13 at 1:02

To do this requires a little bit of knowledge of trigonometry to work out exactly where to move the sprite.

C#'s Math class and XNA's MathHelper class can help considerably, but you will still need to know how to apply them.

Your entity would first need to have the following two fields.

``````public Vector2 Position { get;set; }
public float RotationAngle { get;set; }
``````

If you want RotationAngle to be able to be set using degrees rather than Radians you can use the MathHelper functions to do so:

``````// RotationAngle is set using a value in degrees (the float f).
public void SetRotationAngle(float f)
{
}
``````

Then to apply a change in position using Rotation angle you would do some trig:

``````// This method applies the effects of movement to our entity.
// Speed is the number of pixels the object should move per call.
public void MoveEntity(float speed)
{
Vector2 direction = new Vector2((float)Math.Cos(RotationAngle),
(float)Math.Sin(RotationAngle));
direction.Normalize();
Position += direction * speed;
}
``````

Here we determine the direction we want to travel in using the rotation factor of the sprite. We normalize the Vector so that it has a velocity of 1 unit. We then multiply our direction vector by speed to give us a direction and velocity we want. Finally we apply this directional velocity to our current position.

The thing to bear in mind here is that XNA sprites are initialized to be "right facing" meaning a 0 degree rotation angle will push your sprite right (along the x axis).

Finally, when we call our sprite to be drawn it should look something like this, though it will vary slightly based on where your objects are stored:

``````spriteBatch.Draw(texture,
new Rectangle((int)entity.Position.X,
(int)entity.Position.Y,
texture.Width,
texture.Height),
null,
Color.White,
entity.RotationAngle,
new Vector2(0, 0),
SpriteEffects.None, 0);
``````
-