# Java Tetris - Matrix Rotations

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!

• Like I said in my answer to your last question, you needn't try to learn matrix rotations gamedev.stackexchange.com/questions/54299/… – Attackfarm Apr 24 '13 at 18:53
• @Attackfarm That doesn't help with variable shapes – Growler Apr 24 '13 at 19:12
• It actually works not only for non-classic shapes, but for any arbitrary polygon on continuous axes (e.g. A star that isn't composed of blocks). The only case where my answer is inappropriate is with rotations less than 90 degrees. It would even work for 3-dimensional shapes, as long as the rotations were 90 degrees at a time – Attackfarm Apr 24 '13 at 22:45

## 2 Answers

Your example is stuffed with bugs and inconsistencies, it is really hard to read, but your understanding error seems to be in sentence 4:

add the origin coordinates back to each resulting coordinate

That is not the original coordinates, but the coordinates of the origin that you are rotating around, so (1, 2) in the example case.

By the way, should you desire to rotate a piece around the corner between 4 squares instead of the centre of a square you can use an origin where both the X and the Y component end in .5, the final results will still be integer.

• +1, 10 seconds faster after the question sat for an hour. – MichaelHouse Apr 24 '13 at 17:24
• Why is it that you need to translate into 1’s and 0’s for it to work (aka: why do I need to subtract the origin from all the coordinates before performing matrix multiplication on it...? Then need to add the origin back to each of the new matrix vectors for it to work? Why can't I just use the original coordinates for the matrix?) I’m not understanding the magic of a matrix. – Growler Apr 24 '13 at 17:29
• A matrix can only rotate around (0, 0), but by translating your desired centre of rotation to (0, 0) and back you effectively get a rotation around any desired point. The operation can be optimised to simply perform the rotation on the original matrix, and then add (X+Y, -X+Y) to the result, where (X, Y) is the desired centre of rotation, but that procedure may be slightly harder to grasp. – aaaaaaaaaaaa Apr 24 '13 at 18:03

(1-1, 3+0) (1+0, 2+0) (1+0, 1+1) (2-1, 2+0)

That's not adding the origin back, that's adding your offset rotation to the initial values.

Adding the origin back looks like:

 (0,-1) (0, 0) (0, 1) (-1, 0)
+(1, 2) (1, 2) (1, 2) (1, 2)
-----------------------------
(1, 1) (1, 2) (1, 3) (0, 2)

• Yeap, I got it now. I misread what he wanted me to do. Btw, why is it that you need to translate into 1’s and 0’s for it to work (aka: why do I need to subtract the origin from all the coordinates before performing matrix multiplication on it...? Then need to add the origin back to each of the new matrix vectors for it to work? Why can't I just use the original coordinates for the matrix?) I’m not understanding the magic of a matrix. – Growler Apr 24 '13 at 17:24
• It's not translating into 1s and 0s, those just happen to be the values of the positions around the origin. Think of it this way: the matrix rotation rotates around 0,0. So, if you want to rotate your object around its origin, you need to move the object origin to 0,0 first. Then, after it's rotated, you move it back. – MichaelHouse Apr 24 '13 at 17:29
• You should read through this series of three blog posts, it'll likely help you solidify your understanding. – MichaelHouse Apr 24 '13 at 17:31
• Nice comics Growler. Maybe you can draw me a goblin for my profile picture? – MichaelHouse Apr 24 '13 at 17:48
• @Byte56 I already have the goblin on this site, you can get a unicorn or something. – aaaaaaaaaaaa Apr 24 '13 at 19:53