0
\$\begingroup\$

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)

Another example purely for more information, but more complex (2x3 array):

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ – DMGregory May 12 at 21:27
  • \$\begingroup\$ 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. \$\endgroup\$ – franticabyss May 12 at 21:30
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Nevermind. I just tried it again and got it (for a single number...). The real challenge now will be coding everything from scratch and making it work to achieve such a simple thing. Either way, thank you! \$\endgroup\$ – franticabyss May 13 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.