user5704670
user5704670

Reputation:

Mouse pointer locked around a point

Sorry if this question has been asked before, but if so I could not find it before posting this.

In a nutshell, I want to do this: Example.

I want a pointer (red) to rotate about the circle (blue) according to where the mouse is located. (If picture is not visible, it depicts a blue circle with a red triangle pointing away from it, towards the mouse).

If possible, please answer with a general mathematical equation rather than specific code. Thanks.

Upvotes: 0

Views: 28

Answers (1)

Alnitak
Alnitak

Reputation: 339816

Assuming a normal cartesian coordinate space, with the X axis going to the right and the Y axis going up, you first need to calculate the angle to the mouse coordinate (M) to the circle origin (O):

theta = atan2(M.y - O.y, M.x - O.x)

you can then calculate the position of a point (P) orbiting the circle at radius (r) with:

P.x = r * cos(theta)
P.y = r * sin(theta)

The atan2(y, x) function is a common math library function that just computes atan(y / x) but takes the relative signs of x and y into account to determine the correct quadrant.

Upvotes: 1

Related Questions