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
    If anyone is feeling charitable, they might want to take into account that the y axis is reversed. Increasing values move downwards on the screen....if this matters. I don't think it does, but if I knew any better, I wouldn't be asking.2012-06-10
  • 0
    I'm not fully sure which angle you are talking about. But I am pretty sure you can solve your problem by applying the law of sines.2012-06-10
  • 0
    Take a point on a Cartesian coordinate system. Draw a line, starting at this point and going out to the right. Now draw a point somewhere else. I need the angle between the line and the line which connects these two points. I think you are right, but I'm trying to figure out how to apply it so that it takes into account angles larger than 90 degrees, since this could be up to 360.2012-06-10
  • 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
    See my comments above...I think this only goes up to 90.2012-06-10
  • 0
    @Aerovistae: and only down to -90. But usually you get back radians.2012-06-10