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?
Thank you for your time.

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.
