Sprite Rotation and in Direction C# and XNA

This is a discussion on Sprite Rotation and in Direction C# and XNA within the Game Programming forums, part of the General Programming Boards category; Lol...This is stupid or I am stupid I rotate a sprite in XNA and try to move it in direction ...

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

    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));
    player.velocity.Y = (float)Math.Sin(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
    You ended that sentence with a preposition...Bastard!

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    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. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Shakti View Post
    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.
    You ended that sentence with a preposition...Bastard!

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    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.

    Check out wikipedia pages for trigonometry for more information.

    Math.Sin and Math.Cos takes radian angles.

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

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    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

    These 2 links will hopefully give you more information about angles, trigonometry and everything like that.

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

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

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21