0
$\begingroup$

In a video game I am I'm working on I'm trying to rotate an object around a secondary object. The secondary object will always be in the exact center and the rotating object will always rotate in a circular pattern. As the object rotates around the central object I need to determine how far along the circle the object has traveled (in degrees). Here is a more abstract representation of the problem:

enter image description here

Above I have two circles showing two examples of the problem I'm trying to solve. x1,y1 represents the starting point of the object that is rotating, x2,y2 represents the current position of the same rotating object and x3,y3 represents the object around which it is rotating. I need to calculate in degrees the distance traveled around the circle at any given point in the rotation. The object will never rotate around the circle more than 360 degrees. The radius will be variable but will be a known value that can be used in the equation. I will need an equation that can calculate the distance traveled up to 360 degrees.

Appreciate your help friends!

$\endgroup$
5
  • $\begingroup$ This function can help. en.wikipedia.org/wiki/Atan2 Atan2($y_k,x_k$) will give you the angle (measured counterclockwise) from the $x$-axis to $(x_k,y_k)$. $\endgroup$
    – Steve Kass
    Commented May 28, 2016 at 1:14
  • $\begingroup$ Are you looking for a function $f$ which returns $\phi = f((x_1,y_1), (x_2,y_2), R, (x_3, y_3))$? $\endgroup$
    – mvw
    Commented May 28, 2016 at 1:23
  • $\begingroup$ Does $(x_1, y_1) = (x_3, y_3) + (0, R)$ always hold? $\endgroup$
    – mvw
    Commented May 28, 2016 at 1:27
  • $\begingroup$ or $R^2 =(x_1-x_3)^2+(y_1-y_3)^2$ $\endgroup$
    – reuns
    Commented May 28, 2016 at 1:31
  • $\begingroup$ @mvw no, it does not always hold. $\endgroup$
    – omatase
    Commented May 28, 2016 at 5:24

3 Answers 3

2
$\begingroup$

Let the radius (distance from centre two two corrdinates) be $r$.

Let the distance from $(x_1, y_1)$ to $(x_2,y_2)$ be represented by $c$.

$$c=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$$

If $x_1<x_2$ (from Law of Cosines):

$$\theta=\arccos\frac{2r^2-c^2}{2r^2}$$

If $x_1>x_2$:

$$\theta=360-\arccos\frac{2r^2-c^2}{2r^2}$$

$\endgroup$
6
  • $\begingroup$ I initially misread your question and have corrected my response. $\endgroup$ Commented May 28, 2016 at 1:27
  • $\begingroup$ If Point #1 is in the second quadrant, and Point #2 is in the first quadrant, then x_1 < x_2, but the angle is less than 180 degrees... $\endgroup$
    – DJohnM
    Commented May 28, 2016 at 3:17
  • $\begingroup$ Thanks for pointing this out. See if edit makes sense. $\endgroup$ Commented May 28, 2016 at 3:20
  • $\begingroup$ I'm having difficulty using your example I'm trying to plug in some simple values $(x_1,y_1)$ as $(3,6)$, $(x_2,y_2)$ as $(6,3)$ and $(x_3,y_3)$ as $(3,3)$. When I use these values I get $c=\sqrt{(6-3)^2-(3-6)^2}$ which becomes $c=\sqrt{9-9} = 0$. I think this has already gone a little awry but if I take it all the way through and find $\theta$ with $\theta = \arccos\frac{2r^2-0^2}{2r^2}$ I get $\theta = \arccos\frac{1}{1}$ which is $0°$ where the answer should be $90°$ $\endgroup$
    – omatase
    Commented May 28, 2016 at 5:59
  • $\begingroup$ Oooooh it looks like it works if I change the $c=\sqrt{(x_2-x_1)^2-(y_2-y_1)^2}$ to $c=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$. Might just be a simple signage mistake. $\endgroup$
    – omatase
    Commented May 28, 2016 at 13:55
1
$\begingroup$

Note that the question is over-specified: If Point #1 and Point #2 and the radius are specified, the location of Point #3, the center, is already determined. So:

Firstly: Subtract $x_3$ from $x_1$ and $x_2$ and then $y_3$ from $y_1$ and $y_2$ This produces new co-ordinates $(X_1, Y_1)$ and $(X_2, Y_2)$

Check that $\sqrt{X_1^2+Y_1^2}=\sqrt{X_2^2+Y_2^2}=R$

where R is the radius of the circle (which is also a given quantity). If not, then you have bad data.

Let D the distance between the two points on the circle:$$D=\sqrt{(X_1-X_2)^2+{(Y_1-Y_2)^2}}$$

Then $\theta$, the angle between the radii going to the two points is given by $$\theta=2\times \sin^{-1}\left(\frac{D}{2R} \right)$$

This will give a value for $\theta$ between $0$ and $180$ degrees.

$\endgroup$
2
  • $\begingroup$ I tried to get this to work with an example of (x1,y1) = (3,6), (x2,y2) = (6,3) and (x3,y3) = (3,3) but I am not getting a good result. In this simple example the answer should be 90°. Here's what I get. D = square root of ((3 - 6)^2 + (6 - 3)^2) = 4.2426. θ = 2 x sin^-1(4.2426/2*3) = wolframalpha.com/input/?i=2+x+sin%5E-1(4.2426%2F2*3) $\endgroup$
    – omatase
    Commented May 28, 2016 at 5:32
  • $\begingroup$ That's a times symbol, not a variable x; be sure to divide by both 2 and 3; the answer is in radians, so convert to degrees... The example works on my calculator and in Wolfram $\endgroup$
    – DJohnM
    Commented May 28, 2016 at 16:48
1
$\begingroup$

Hint: the transformation from polar coordinates ($r$,$\theta$) to Cartesian ($x$,$y$) is given by: $$x=r\cos\theta$$ and $$y=r\sin\theta$$

where $r$ is the distance from the origin (in your case the unmoving object) and $\theta$ is the angle measured anti-clockwise from the positive $x$ axis.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .