3
\$\begingroup\$

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!

\$\endgroup\$

1 Answer 1

7
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Ah ok, I have pointed my sprite upwards! Haha! Many thanks! \$\endgroup\$ Commented Jan 19, 2011 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .