How can I rotate a coordinate around a circle?

I need to rotate an object in a circle around a central point. All I know is the rotation of the centre point in degrees, the centre point location (Which will always be 0,0), and the object location (Well actually the object distance to the centre, but because the centre is 0,0 it is the same thing).

Why I want to do this is because I have a ship in the world and I can check for collisions for it, however when checking for if the CannonBall is colliding with the ship I cannot rotate the ship, meaning that to check for collisions I must instead rotate the CannonBall around the ship depending on the ship’s rotation.

(As you can see on my trailer (https://www.youtube.com/watch?v=2YU7oEUnwlw), I already attempted to implement this however have been unable to do it. Also I uploaded that video april 30, which means I have actually been having the issue for over a month.)


Solution 1:

If your central point is $(c_x,c_y)$ and you want to rotate counter-clockwise about this point by an angle of $\theta$ (in radians) you would shift the center to the origin (and the rest of the points of the plane with it), then rotate, then shift back. You can use:

$x_{\text{rot}}=\cos(\theta)\cdot(x-c_x)-\sin(\theta)\cdot(y-c_y)+c_x$

$y_{\text{rot}}=\sin(\theta)\cdot(x-c_x)+\cos(\theta)\cdot(y-c_y)+c_y$

$(x,y)$ are your initial coordinates and $(x_\text{rot},y_{\text{rot}})$ are the new coordinates after rotation by $\theta$ about $(c_x,c_y)$

Example: If you want to rotate the point $(3,0)$ by $90^{\circ}$=$\frac{\pi}{2}$ radians about the point $(3,2)$ the formula should give $(5,2)$. Computing to check:

$x_{\text{rot}}=\cos(\frac{\pi}{2})\cdot(3-3)-\sin(\frac{\pi}{2})\cdot(0-2)+3=5$

$y_{\text{rot}}=\sin(\frac{\pi}{2})\cdot(3-3)+\cos(\frac{\pi}{2})\cdot(0-2)+2=2$