0
$\begingroup$

Say I have coordinates center X, center Y. I also have myX and myY.

I'm writing a 2D game, and I need the second point to revolve around the first.

This involves finding the angle between the revolver and the "positive-x-axis" from the center point.

How can I do this?

  • 0
    I feel like there must be a cleverer way than just doing a 4-way conditional that adds 0, 90, 180, or 270 to the angle depending what quadrant the point is in, relative to the original point.2012-06-10

3 Answers 3

2

An alternative approach would be to just rotate the 'my' point about the current point by some angle $\theta$ without computing the current angle. This avoids any 'fiddlyness' with $\arctan$. I am using the usual (in mathematics, not screens) axes here.

Let $c=\cos \theta$, $s=\sin \theta$. Let $(x,y)$ be the current point, and let $(x',y')$ be the 'my' point. Then to compute the rotated 'my' point, compute $\binom{x_{my\_rotated}}{y_{my\_rotated}} = \binom{x}{y} + \begin{bmatrix} c & -s \\ s & c \end{bmatrix} \binom{x'-x}{y'-y}.$

Or explicitly: $x_{my\_rotated} = x+c(x'-x)-s(y'-y)$, $y_{my\_rotated} = y+s(x'-x)+c(y'-y)$.

1

Following Whocares notation, if the center is $A=(A_x,A_y)$ and the rotating point is $B=(B_x,B_y)$, you can use the atan2 function and ask for atan2$(B_x-A_x,B_y-A_y)$. It normally returns the angle in the range $(-\pi, \pi]$ and is in radians (check the documentation of your language), but it worries about all four quadrants for you. If you want to consider that the $y$ axis is going downward, you may have to reverse the sign

0

I'll call the center point $A$ and the rotating point $B$. So $A=(A_x,A_y)$ and $B=(B_x,B_y)$

The equation you are looking for is $angle=\arctan((B_y-A_y)/(B_x-A_x))$

  • 0
    @Aerovistae: and only down to -90. But usually you get back radians.2012-06-10