Thread: Angle of rotation formula depending on sprite position

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    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:
    Angle of rotation formula depending on sprite position-spaceship-png
    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.
    You ended that sentence with a preposition...Bastard!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Here's a unit circle which shows you how 2D rotation is performed.

    Angle of rotation formula depending on sprite position-unitcircle-jpg

    I made it myself!
    Devoted my life to programming...

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    xD nice picture
    but how can I relate it to a sprite?
    Is the "sprite" the "grey square" with the red arrow intersecting it?
    You ended that sentence with a preposition...Bastard!

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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...
    Last edited by GReaper; 05-31-2011 at 07:53 AM.
    Devoted my life to programming...

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    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 .
    You ended that sentence with a preposition...Bastard!

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, think about it. Your second sprite's angle is +90 degrees the first. cos(θ + π/2) = sinθ and sin(θ + π/2) = -cosθ

    EDIT: Simple...
    Devoted my life to programming...

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Sipher View Post
    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?
    You ended that sentence with a preposition...Bastard!

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    By looking at the picture I posted ( and with a little knowledge about trigonometry, that is )
    Devoted my life to programming...

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    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
    You ended that sentence with a preposition...Bastard!

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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!
    Devoted my life to programming...

  11. #11
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    if i rotate the grey square by 90
    y = cos
    x = sin
    but it doesn't explain why
    y = -cos
    You ended that sentence with a preposition...Bastard!

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by Eman View Post
    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 - θ ).
    Devoted my life to programming...

  13. #13
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    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
    You ended that sentence with a preposition...Bastard!

  14. #14
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by Eman View Post
    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.

    Quote Originally Posted by Eman View Post
    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.
    Devoted my life to programming...

  15. #15
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Sipher View Post
    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
    Last edited by Eman; 05-31-2011 at 08:45 AM.
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprite Rotation and in Direction C# and XNA
    By Eman in forum Game Programming
    Replies: 9
    Last Post: 02-21-2011, 11:06 PM
  2. Sprite Rotation With Sdl
    By Warlax in forum Game Programming
    Replies: 4
    Last Post: 08-15-2006, 12:49 PM
  3. Sprite/Image Rotation in SDL
    By Blizzarddog in forum Game Programming
    Replies: 4
    Last Post: 03-18-2005, 11:19 AM
  4. Sprite rotation
    By kawk in forum Game Programming
    Replies: 1
    Last Post: 01-25-2005, 01:39 PM
  5. sprite rotation in allegro
    By thedumbmutt in forum Game Programming
    Replies: 4
    Last Post: 01-14-2003, 08:43 AM