# Thread: Sprite Rotation and in Direction C# and XNA

1. ## Sprite Rotation and in Direction C# and XNA

Lol...This is stupid or I am stupid

I rotate a sprite in XNA and try to move it in direction it is facing..using the same bl**dy formula I used for Allegro that worked but no joy..
this is the code I am trying...
Code:
```player.velocity.X = (float)Math.Cos(MathHelper.ToRadians(player.rotation));
When I rotate press left or right I rotate the player.
then I call the update function to add the velocity to the current position.

when I press up or down..the above code works out the direction it is facing..the new X and Y or it is supposed to at least..but it isn't working :@ :@

Any ideas?
Thanks

2. Exactly how is it not working? What is your current result, what is the expected result?

One possible error i can think of that you have to make sure so that if you have a rotation of 0 the player is facing straight down the x-axis and not up the y-axis or something like that.

3. Originally Posted by Shakti
Exactly how is it not working? What is your current result, what is the expected result?

One possible error i can think of that you have to make sure so that if you have a rotation of 0 the player is facing straight down the x-axis and not up the y-axis or something like that.
I think the in XNA the if the rotation value is 0.0f the sprite is facing Up. left is 90.0f
down is 180.0f and so on..

when i change the rotation value..I want to get the direction in which I am facing...
but it isn't working..either I am only moving up or down the y axis or I am correctly in the x axis but wrongly in the y axis.

the Math.Sin(angle) does it take angle in radians or in degree? I tried it both ways no difference.

4. well the trig functions the way you are using them assumes you move along the x-axis when you have a rotation of 0.0f, then going counter-clockwise along the unit-circle.

Math.Sin and Math.Cos takes radian angles.

5. ok i was able to solve one part:
the Up movement with this code:
Code:
```                player.velocity.X = (float)Math.Sin((player.rotation));
player.velocity.Y = -(float)Math.Cos((player.rotation));```
but the down movement doesn't work...
Code:
``` player.velocity.X = (float)Math.Sin((player.rotation));
player.velocity.Y = (float)Math.Cos((player.rotation));```
the graph is reversed..so the origin is at the top left corner (0,0)

one thing I don't understand is..even though the graph is reversed...why is it that we use
the Sin to get the X axis? it is meant to be cos and sin for y.

6. The origin doesnt matter. The only thing that matters is how you (and XNA) defines the angles; where 0.0f points and in what direction along the unit-circle it is.

Trigonometry - Wikipedia, the free encyclopedia
Unit circle - Wikipedia, the free encyclopedia

7. i will read the article...thanks

but quickly...the formula to calculate a new x and y position in a normal cartesian coordinate is
x =cos(angle) *r
y = sin(angle) *r

because the graph is reversed in XNA
it becomes
x = sin(angle) * r;
y = cos(angle) * r;
can you..or anyone explain why that is? Looking at the graph I drew here..it doesn;t make sense to me..and I hate using a formula blindly.

8. The coordinate system isnt reversed, it is rotated, and one of the properties of sin and cos is this (assuming cos works with normal degrees):
cos(x-90) = sin(x)
and
sin(x+90) = cos(x)
(hope i got those right).

I cant see any graphs or anything here but i think this answers your question somewhat.

Read the articles, it is too much to explain trigonometry on these forums and the explanation probably wouldnt be half as good as the one found on wikipedia.

9. mmn..yeah I read it and that was difficult to do..

cos(x-90) = sin(x)
and
sin(x+90) = cos(x)

what does that mean?

10. cos(x-90) = sin(x)
and
sin(x+90) = cos(x)
It means you need to do a bit more reading and brushing up on your mathematics before jumping in and coding your game.