# Angle for pointing at a certain point in 2d space

Recently, I have been programming a simple game. Very simple: There is a tank, and the cannon will aim at whatever position the mouse is at.

Now lets talk about the cannon graphic. The cannon graphic points to the north, with 0 rotation.

Here are the variables I have for my game, and that might be important factors for solving my problem:

Tx = The tank's X position in the world. Ty = The tank's Y position in the world. Mx = The mouse's X position in the world. My = The mouse's Y position in the world.

Also, in this programming language, the greater the Y coordinate, the lower you are. And the less the Y coordinate is, the higher you are. So, Y = 0 means the top.

My problem is, how do calculate the rotation needed for my cannon graphic to "point" to the mouse's position?

-
Have you checked Wikipedia's Trigonometry page? – Yuval Filmus Jan 13 '11 at 6:25
+1 for giving enough information that you are asking a well-defined question. – Ross Millikan Jan 13 '11 at 13:52
@Ross Millikan: It was well-defined, until I got to the end of my answer and realized Omega doesn't say whether the angle gives clockwise or counterclockwise rotation of the tank's cannon! – hardmath Jan 13 '11 at 14:32

Suppose this is the situation:

$\displaystyle D_x$ is the difference of the $\displaystyle x$-coordinates and $\displaystyle D_y$ is the difference of the $\displaystyle y$-coordinates.

Then angle $\displaystyle w$ is given by $\displaystyle \tan w = \frac{D_y}{D_x}$ and thus $\displaystyle w = \arctan (\frac{D_y}{D_x})$. The angle you will need to rotate would then be anti-clockwise $\displaystyle \frac{\pi}{2} + w$, if the tank is point "up".

Note: The above assumes that $\displaystyle w$ is acute. I will leave it to you to try to work out the other cases (for different tank and mouse positions) and come up with a general formula.

I would suggest reading up on atan or (to avoid a trap involving division by $0$) atan2. Most likely the Math package of your programming language will have both.

Hope that helps.

-
One thing: The result ranges from around -1.5 to -0.5. I didn't quite get what to do with these, I need degrees to work with my graphics.. – Zol Tun Kul May 6 '11 at 23:07
– Aryabhata May 6 '11 at 23:10

Okay, in your coordinates "north" presumably means up, i.e. the direction of decreasing $y$ coordinates. Let's assume for convenience that all $y$ coordinates are nonnegative, so $y=-1$ is off the display.

From a location $(x_0,y_0)$ to target $(x_1,y_1)$ the angle you want has cosine $(y_0 - y_1)/d$ where $d = \sqrt{(x_1 - x_0)^2 + (y_1 - y_0)^2}$ is the distance location to target.

Taking the arccosine of this gives the absolute value of the angle you want. For the sign you need to say whether the cannon rotates clockwise or counterclockwise as the angle increases. If increasing the angle (from zero = up/north) rotates clockwise, then choose a positive sign whenever $x_1 > x_0$ and a negative sign when $x_1 < x_0$. If $x_1 = x_0$, then the angle is either zero when $y_1 < y_0$ or $\pi$ radians (aka 180 degrees) if $y_1 > y_0$.

-