I have a problem with my XNA Game, I have a function where I need my sprite to face towards an enemy, using this code:

        Vector2 currentPosition = Position;
        Vector2 nearestEnemy = gameObjectManager.GetNearestEnemy(Position, GameObjectTypes.Zombie);

        Orientation = (float)Math.Atan2(nearestEnemy.Y - currentPosition.Y, nearestEnemy.X - currentPosition.X)

As found on an XNA forum results in my character looking 90 degrees away from the enemy.

Adding "MathHelper.ToRadians(90);" to the end of my Orientation assignment fixes this bug, why is this happening?



It seems to me that your sprite may be incorrectly oriented in the source file. Atan2, I believe, returns the angle of the given vector, where zero indicates a vector pointing "to the right" (+X, Y=0). If your sprite file does not point "to the right", you will have to make angle corrections.

  • \$\begingroup\$ Ah ok, I have pointed my sprite upwards! Haha! Many thanks! \$\endgroup\$ – JuniorDeveloper1208 Jan 19 '11 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.