XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

need help about rotating a sprite

Last post 4/20/2008 12:27 PM by Stainless. 4 replies.
  • 4/19/2008 12:30 AM

    need help about rotating a sprite

    hey im trying to rotate a sprite (my player ship) towards a angle given by the controller

    current code

    if(Angle < WantedAngle-2)

             Angle += MyShip.TurnSpeed;

    else if(Angle > WantedAngle+2)

             Angle -= MyShip.TurnSpeed;

    this works as long as the angle is -90 to 90 (right is 0`) but if the angle is at 179 and wants to go to -170 instead of just going over it goes all the way around and i cant think of the code to fix it.

    by the way my angles are in degrees from -180(left) to 180(left) with 0 on the right -90 on the top and 90 on the bottem

     

    thanks in advance

     

    Lead Programmer At 2.0 Studios

    Play Project Alpha Today
  • 4/19/2008 5:36 AM In reply to

    Re: need help about rotating a sprite

    Before your code:

    if( ( Angle - WantedAngle ) > 180.0f )
        Angle += 360.0f;
    else if( ( Angle - Wanted ) < -180.0f )
        Angle -= 360.0f;


    I think that should do the trick.
  • 4/19/2008 7:02 AM In reply to

    Re: need help about rotating a sprite

    another way of doing it is based on the techniques used in inverse kinematics

    Vector3 travel = new Vector3((float)Math.Cos(rotation), (float)Math.Sin(rotation), 0.0f);
    Vector3 desired = new Vector3((float)Math.Cos(targetdir), (float)Math.Sin(targetdir), 0.0f);
    travel.Normalize();
    desired.Normalize();
    Vector3 turn=Vector3.Cross(travel, desired);

    if (turn.Z < 0)
    {
    turndir = -1.0f;
    }
    else
    {
    turndir = 1.0f;
    }

    You could also test for == 0

    And the arccos of the dot product of the vectors is the angle between the vectors.

     

    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
  • 4/20/2008 2:22 AM In reply to

    Re: need help about rotating a sprite

    Yep, converting to vectors and taking the cross product works as well.  You don't actually need to normalize those vectors though.  The result of the Sin/Cos pairs will be unit length anyways, and even if they weren't, it wouldn't affect the sign of the cross products z value.
  • 4/20/2008 12:27 PM In reply to

    Re: need help about rotating a sprite

    force of habit.

     

    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
Page 1 of 1 (5 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG