Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

Thanks!

share|improve this question

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
Ah ok, I have pointed my sprite upwards! Haha! Many thanks! –  JuniorDeveloper1208 Jan 19 '11 at 19:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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