# How to rotate multi-tile structure on grid?

I need to be able to place structures in my game that span multiple tiles and can also be rotated. My idea on how to solve this would be to use 2D arrays that hold data about the structure.

EXAMPLE: (simplified)

Crafting table layout: XOX

X - data O - data, but also used as a pivot point

If we were to rotate it clockwise, it'd look like this:

X
O
X


My problem lies in that these positions have to be real-world coordinates, so here's the above example again but in a way that makes sense for my actual grid in-game. (replacing the X and O with coords):

(0,1)(1,1)(2,1)


And now, rotated clockwise (around the pivot point, which is (1,1)):

(1,2)
(1,1)
(1,0)


Complex workstation ((6,4) IS THE PIVOT!):

(5,5)(6,5)(7,5)
(5,4)(6,4)(7,4)


Rotate it counterclockwise and we should get this:

(5,5)(6,5)
(5,4)(6,4)
(5,3)(6,3)


I have tried and succeeded in doing it only on a conceptual level with the "X" and "O"s, but I don't know how to implement actual coordinates.

I've never dealt with rotation matrices before so any advice, suggestion or solution on how to implement this is appreciated.

• This looks like a conventional application of a rotation matrix. How have you tried computing these rotated coordinates so far, and where have you run into trouble you need help solving? – DMGregory May 12 at 21:27
• I have tried and succeeded in doing it only on a conceptual level with the "X" and "O"s, but I don't know how to implement actual coordinates. This might be obvious to someone who's dealt with matrices before, but I haven't, and I don't even know how to begin solving this problem. Any help, advice, or solution is appreciated. – franticabyss May 12 at 21:30

You can rotate a 2D point $$\\vec p\$$ around a center coordinate $$\\vec c\$$ by treating it as though it had a third dimension with a value of one, and multiplying it by a special matrix:
\begin{align}\begin{bmatrix}p_x^\prime\\p_y^\prime \end{bmatrix} &= \begin{bmatrix}a & -b & c_x -ac_x +bc_y\\b & a & c_y - bc_x - ac_y\end{bmatrix}\begin{bmatrix}p_x\\p_y\\1\end{bmatrix}\\ &=\begin{bmatrix}ap_x - bp_y +c_x -ac_x -bc_y\\bp_x + ap_y + c_y - bc_x - ac_y\end{bmatrix}\end{align}
where $$\a = \cos \theta\$$ and $$\b = \sin \theta\$$ given your counter-clockwise rotation angle $$\\theta\$$. In your case, for 90-degree rotations, these will only ever be -1, 0, or 1.
\begin{align} 0­°&: \begin{bmatrix}1 & 0 & 0\\0 & 1 & 0\end{bmatrix} & 90­°&: \begin{bmatrix}0 & -1 & c_x + c_y\\1 & 0 & c_y - c_x\end{bmatrix}\\ 180­°&: \begin{bmatrix}-1 & 0 & 2c_x\\0 & -1 & 2c_y\end{bmatrix}\quad &270­°&: \begin{bmatrix}0 & 1 & c_x - c_y\\-1 & 0 & c_y + c_x\end{bmatrix} \end{align}
You can verify that multiplying any of these matrices by $$\\vec c = \begin{bmatrix}c_x\\c_y\\1\end{bmatrix}\$$ just yields $$\\begin{bmatrix}c_x\\c_y\end{bmatrix}\$$ again, so the center point stays fixed for all of these transformations.