   XNA Rotating an Image.
  Hi guys. Im trying to rotate my image (2D Sprite) so it faces towards the currant cursor position.Here is outline of what im trying to do.http://ghpaa.org.uk/images/Dia1.jpg: M Is the Currant Mouse Poistion (Mx and My are the X and Y co-ords): P Is the Player PositionI have worked out the trig to rotate and have written this functionpublic float Rotate2Mouse(MouseState m){ // Aim /* Return a float rotation using the currant position of the ship and the  position of the mouse  */ float q = (m.Y - vLocation.Y) / (vLocation.X - m.X); return (float)Math.Tan((double)q);}However, this doesn't work, the image keeps spinning around when it moves. Here are some thing I don't like in the code.1/ doing a Math.Tan(). I think I should use a Tan^-1() on the calc.2/ all the Cast typing, Float to Double3/ My maths maybe a little off :S If anyone has time to look at this it would be highly appreciated thank you.   
http://en.wikipedia.org/wiki/Atan2this is what you are looking for, take a look at it. In C# the call is Math.Atan2.In your case it should be (done on the fly and not tested)Math.Atan2(m.Y - vLocation.Y, m.X - vLocation.X);Try to read the wikipedia article for more details.   ;  I see what you're doing wrong. This is entirely a math error.What you're trying to do, based on the diagram, is find q, an angle. I do believe there is a function call Math.ArcTan() or Math.ATan() or something of the like. Arctangent is the "tan^-1()"; it's the more commonly used name in computers, since using a ^ operator literally does powers, not inverse trig.Anyway, I digress. Basically, use an arctangent function. You have to solve the equation from the diagram for Q, which means you must arctangent (or what you called tan^-1) both sides. The result, therefore, should be the following: double q = arctangentfunctionnamehere((m.Y - vLocation.Y) / (vLocation.X - m.X)); return (float)q;Hope that helped. Also fixed up your excessive typecasting by just making q double in the first place, but still returning a float as you wanted.   ;Quote:Original post by TheAirPumpI see what you're doing wrong. This is entirely a math error.What you're trying to do, based on the diagram, is find q, an angle. I do believe there is a function call Math.ArcTan() or Math.ATan() or something of the like. Arctangent is the "tan^-1()"; it's the more commonly used name in computers, since using a ^ operator literally does powers, not inverse trig.Anyway, I digress. Basically, use an arctangent function. You have to solve the equation from the diagram for Q, which means you must arctangent (or what you called tan^-1) both sides. The result, therefore, should be the following: double q = arctangentfunctionnamehere((m.Y - vLocation.Y) / (vLocation.X - m.X)); return (float)q;Hope that helped. Also fixed up your excessive typecasting by just making q double in the first place.This is wrong too, the one argument atan does not identify correctly between diametrically opposite directions and it will lead to errors, the solution is using atan2 to identify the correct quadrant.   ;double q = Math.Atan2(m.Y - vLocation.Y, vLocation.X - m.X);return (float)q;ok thanks guys, this is working Almost perfectly.However, the rotation is the inverse of the mouse movement.As in if I move the mouse up(decrease in Y value) then to Rotation Increases however it should Decrease.I think as result the Rotation is 180 degrees off.EDIT: to make myself clearerhttp://ghpaa.org.uk/images/new1.jpgwhere the dot is the cursor (ish)EDIT 2: SOLVED. Thanks a lot guys here's how i solved it, let me know if there was an easier waypublic float Rotate2Mouse(MouseState m)   {  // Aim  /* Return a float rotation using the currant position of the ship and the position of the mouse   */  double q = Math.Atan2(m.Y - vLocation.Y, vLocation.X - m.X);  return -(float)(q + MathHelper.ToRadians(90.0f));   };Quote:Original post by Andy474double q = Math.Atan2(m.Y - vLocation.Y, vLocation.X - m.X);return (float)q;ok thanks guys, this is working Almost perfectly.However, the rotation is the inverse of the mouse movement.As in if I move the mouse up(decrease in Y value) then to Rotation Increases however it should Decrease.I think as result the Rotation is 180 degrees off.Your solution is fine too, it is just a change of coordinates base that is flunking the base calculation.Try with double q = Math.Atan2(m.Y - vLocation.Y, m.X - vLocation.X);and tell us if it works. (i don't have XNA neither a DX enabled PC atm. :P)   