# Thread: Angle of rotation formula depending on sprite position

1. ## Angle of rotation formula depending on sprite position

Hello, ok I'm just going over game basics to get the hang of it again.
I know I have asked a question like this before, but this is slightly different in that I would like to find out how to derive direction formula based on sprite position.
I have a 2d spaceship:

I was using the one the right initially, but then I was messing with the rotation when I decided to use the one on the left.
To calculate the direction (velocity) vector based on that right sprite position I used this formula,

playerVel.X = Math.Sin(rotAngle) ;
playerVel.Y = -Math.Cos(rotAngle) ;

That worked fine, until I let the sprite's initial position look like that of the left. To calculate the direction I was guessing the formula..until I tried the original formula of finding direction based on angle of rotation. The one that makes sense.

playerVel.X = Math.Cos(rotAngle);
playerVel.Y = Math.Sin(rotAngle) ;

now this makes sense! Cos is used to find the X value and Sin to find the Y value. My question is, how does switching how the sprite's initial direction in a 2d image change the formula to find the new direction vector?
If I knew how to plot this on a graph paper I'd be able to derive the formula instead of guessing..I was never good at trigonometry so can someone please make this clear using some pictures of a graph or something..hopefully?
Thanks

NOTE:
Before Salem gets super angry (or anyone), I posted this on the gamedev.net last night..but they're not particularly helpful.

btw: I like the new attachment system. Easy on the user.

2. Here's a unit circle which shows you how 2D rotation is performed.

3. xD nice picture
but how can I relate it to a sprite?
Is the "sprite" the "grey square" with the red arrow intersecting it?

4. The red arrow is the direction vector and "θ" is the angle. I drew the grey square to show you the X and Y coordinates.
Therefore X = cosθ and Y = sinθ .

EDIT: I guess the vector's tip should be your spaceship's tip...

5. oh yeah I get that ..but like I said in the original question.
how the sprite is positioned would affect the formula to calculate the direction vector

as you say cos(angle) *r is for the x and sin(angle)*r is for the y but this only works if the sprite's direction on the sprite sheet is like that of the image on the left..the one I attached in original post.
So this formula is valid
x = cos(angle) ;
y = sin(angle) ;

but if the sprites' direction in the sprite sheet is facing upwards.

the formula becomes
x = sin(angle);
y = -cos(angle) ;
the formula is reserved except for the -...my question is why?
I know the answer is in your graph that you posted..but I can't "see" it .

6. Well, think about it. Your second sprite's angle is +90 degrees the first. cos(θ + π/2) = sinθ and sin(θ + π/2) = -cosθ

EDIT: Simple...

7. Originally Posted by Sipher
Well, think about it. Your second sprite's angle is +90 degrees the first. cos(θ + π/2) = sinθ and sin(θ + π/2) = -cosθ

EDIT: Simple...
hold on wait...i don't get the formula..simple for you maybe ha ha

cos(θ + π/2) = sinθ
sin(θ + π/2) = -cosθ
how did you arrive by that?

8. By looking at the picture I posted ( and with a little knowledge about trigonometry, that is )

9. ah come on! that is a little cryptic :P..
em, what can I read (nothing too complicated)? because the website only showed how to get the direction but not considering how a sprite's initial direction..
I wish I could understand this

10. Think of it this way: Try rotating that grey square by around its (0, 0) tip 90 degrees and see the result.

EDIT: Hey 777 posts! Ultimate perfection!

11. if i rotate the grey square by 90
y = cos
x = sin
but it doesn't explain why
y = -cos

12. Originally Posted by Eman
but it doesn't explain why
y = -cos
Well, it is a matter of which angle you pass it. Here we're using an angle that is 0 at +X and 180 at -X. If you don't put minus there you imply that we took the opposite angle ( 180 - θ ).

13. yeah, at least it isn't 666

sorry, this is why I hate math books what does +X and -X stand for?
Well, at least you know of one human who is infinitely stupid

14. Originally Posted by Eman
sorry, this is why I hate math books what does +X and -X stand for?
The positive side and the negative side of the X axis.

Originally Posted by Eman
Well, at least you know of one human who is infinitely stupid
Are you kidding me? You should have seen me a while ago... I only know all of these because I'm learning how to make games and game engines.

15. Originally Posted by Sipher
The positive side and the negative side of the X axis.

Ah, yeah

Are you kidding me? You should have seen me a while ago... I only know all of these because I'm learning how to make games and game engines.

Lol, thanks for trying to make me feel better. One day I will make them but by then I will have grey hair

If you don't put minus there you imply that we took the opposite angle ( 180 - θ ).
I just don't understand why it only affects the y axis.

EDIT:
by the opposite angle it means we're still referring to the positive angles..but when we reverse it we want the inverse, to tell it we are in the negative angles? Bad grammar but screw it lol