   One Object Faces Another
  I've been struggling with a math problem for a little while now.  I'm going to college, and I've taken trigonometry in high school and applied it successfully in my games.  However, this problem is a little beyond my grasp.  I'm programming with C# using the XNA Gamestudio. XNA Gamestudio deals with x and y coords just like Direct X with the origin in the top left corner, and y coords increasing as you move down the screen.The problem I'm having is this:  In two dimensions, I want one object to rotate to face another object by comparing both object's positions.  I have a SpriteManager class which tracks the position of all active, on screen sprites and includes some helper methods to do some useful things with this data.  For instance, FindNearestPlayerPosition(GameObject gameobject) takes the position of one object and compares it to all possible player objects (the game will include up to four players) to find the nearest one, then returns that player's x and y coords.  I also have a function called VecForPosition() which looks like such:public static float VecForPosition(Vector2 pos1, Vector2 pos2){    float x, y, angle;    x = pos2.X - pos1.X;    y = pos2.Y - pos1.Y;    angle = x / y;    angle = (float)Math.Tan(angle);    return angle;}My thought was that the tangent trig function would be appropriate.  The change in x and y would each be one leg of a hypothetical right triangle, and so the formula would look like this:tan angle = difference_of_x / difference_of_ySo, in the AI section of one of my enemy classes, I have a function call that looks like this:Rotate = SpriteManager.VecForPosition(this.Position, SpriteManager.FindNearestPlayerPosition(this));That's more or less what it looks like, leaving out a few details.  Hopefully I didn't leave out too many details.  When I fiddle around with the VecForPosition() method, it seems to sort of just barely work, but ONLY if the player is far away from the object that is trying to orient itself.  It doesn't make the enemy face the right direction, and depending on your position, the enemy object just wigs out and starts spinning really fast.  Unfortunately, XNA Gamestudio deals in radians.  I did some research on wikipedia, and it looks like the trig functions work differently with radians (which makes sense), but I still think I'm making another critical mistake that has to be fixed first.  Namely the application of the tangent function to the equation:angle = x / y;angle = (float)Math.Tan(angle);When it comes to doing math with a programming language, I can make trig functions work on the right hand side of the equality, but how can I make the trig function apply to the left hand side of the equality?  I'm almost completely certain that setting 'angle' equal to the tangent of itself, when it equals the quotient of the difference in x and the difference in y, is wrong.Sorry if this is tl;dr material, but I'm almost always able to figure out problems myself either through tinkering or research, but this is a bit out of my league for now.edit:  I used the powers of my community college education to remember that applying tangent to the left hand side of the equation is the same as applying the opposite of tangent to the right hand side of the equation.  The Math class has a function called Atan() which does exactly that.  Now my enemy class faces the player when the player is "above" it, but faces away from the player when the player is "below" it.  If anyone has any insight on how to get it to work no matter what the y position of the player is, I'd appreciate it.   
  There's a couple of problems with your 'get angle' function (which could probably have a better name than 'VecForPosition').1. It's probably Atan() that you want, not Tan(). Tan() returns the tangent of the input angle, but you're feeding it a tangent value and expecting an angle in return, which is backwards. It's Atan() (inverse tangent) that does what you want.2. You probably want y/x, not x/y (assuming standard conventions).3. Atan() is a poor choice here. The computation may fail if y is 0 or has small magnitude, and Atan() used by itself won't tell you which quadrant the angle is in. Short answer: use Atan2() instead.   ;  You want VecForPosition (this should probably have a better name) to return the angle between two vectors, correct? If so, you can make use of the fact that from which follows where a is the angle between the two vectors.EDIT: Actually, after reading over your post a bit more, it seems like you want VecForPosition to take two positions and return an angle between them. But that doesn't really make sense. You say you want to make one object face another object, but that's a question of orientation, not position. What you want is the difference between one object's direction vector and difference vector between the two objects. Plug those two vectors into the formula above to get your angle.[Edited by - Ariste on May 30, 2010 2:06:14 AM]