I'm building Tetris in Java and am trying to use linear algebra to rotate a piece composed of 4 tiles.
My friend was explaining the way to do it is:
He said:
"To clarify, you do need to rotate each point -- that is you need to rotate one point for each Tile in a Piece. But NOT four corners for each Tile in a Piece. The origin is like if you stuck a pencil through a piece of paper and spun the pencil around.. the spot where the pencil is is the origin."
"So if you have a T on your board with Tiles at (7,9) (8,9) (9,9), (8,10) and its origin is at (8,9).."
So I'm doing it with coordinates (1, 3) (1, 2) (1, 1) (2, 2)… with origin (1, 2)
Then he said:
"You translate the Tiles to be relative to the origin. That is, you treat the origin as the new (0, 0) for this rotation. That's as easy as just subtracting the origin from each coordinate, giving you (7-8, 9-9), (8-8, 9-9), (9-8, 9-9), (8-8, 10-9) or (-1, 0) (0, 0) (1, 0) (0, 1)"
Subtract origin (1, 2) from each coordinate
(1-1, 3-2) (1-1, 2-2) (1-1, 1-2) (2-1, 2-2) =
(0, 1) (0, 0) (0, -1) (1, 0)
Then he said:
"Now rotate these four coordinates using the rotation matrix multiplication, like we have been talking about."
Finally he said:
"Then add the origin coordinates back to each resulting coordinate, and now you have your four rotated Tile coordinates."
From the matrix above, I have (0, -1) (0, 0) (0, 1) (-1, 0)… so I add these to the origin coordinates like he says (1-1, 3+0) (1+0, 2+0) (1+0, 1+1) (2-1, 2+0) =
Rotated coordinates: (0, 3) (1, 2) (1, 2) (1, 2)
But, looking on my rotated shape... it's completely wrong:
Any thoughts why?
Thanks!