Rotate an object about a point with an angle

coordinate systemsgeometryrotations

This probably is a simple question, but I have been struggling with this for a good amount of time. I am making a game from scratch with pure python and I am struggling to rotate an object to the correct positions. I can make the object rotate from its center. So I simply want to move that rotated object to the correct position.

I have found many answers online. Many were along the lines of:

x = rsinθ
y = rcosθ 

But when I used the formulas it still didn't seem to work.

Here is a visual representation of the problem:
enter image description here

As you can see in the picture, there are no negative coordinates. It simply is from 0 to 1000 in both axis. I rotate the character and the rectangle above the character separately, so the center of rotation for the rectangle is its own center (width / 2, height/ 2). I just need to find the position to put the rectangle to make it appear above the player's head.

Best Answer

If the center of rotation is point $C = (X_C, Y_C) $ and the angle of rotation is $\theta$, then the rotated point coordinates are given by

$ P' = C + R(\theta) (P - C) $

where

$R(\theta) = \begin{bmatrix} \cos \theta && - \sin \theta \\ \sin \theta && \cos \theta \end{bmatrix}$

and $P=(X,Y)$ is the original point, whereas $P'= (X', Y')$ is its rotated version.

All points are represented here by column vectors.

The above matrix-vector equation can written as two scalar equations, as follows:

$ X' = X_C + \cos \theta ( X - X_C) - \sin \theta (Y - Y_C) $

and

$ Y' = Y_C + \sin \theta (X - X_C) + \cos \theta (Y - Y_C)$

You should rotate the rectangle and the character about the same point (same center of rotation) so as to preserve their relative position.

Related Question