Search code examples
c#xnarotationatan2

Rotate a sprite in the direction of it's movement vector


I've got an XNA Windows Phone Game and you move a sprite with the accelerometer

My problem is that I can't figure out how to make the sprite face the direction it is moving. So far this is my code:

        arrowPos.Y += -accelState.X*10; 
        arrowPos.X += -accelState.Y*10;

        rotation = -(float)Math.Atan2(arrowPos.Y, arrowPos.X);

Solution

  • Your current rotation calculation uses the current position of the sprite - the sprite may be in that position whether it is moving north or west.

    Try using the accelState.X and accelState.Y values instead.