4

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."

enter image description here

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:

enter image description here

Any thoughts why?

Thanks!

user3871
  • 12,432
  • 33
  • 128
  • 268

2 Answers2

1

I haven't done matrix multiplication in a little while but it doesn't look like the order you inserted your points into the matrix to be rotated is the same as the ones you pull out.

You say you are left with (0, -1) (0, 0) (0, 1) (-1, 0). This looks like colums are your points and the top one is your x while the bottom is your y. If you did that same convention with your first set of points wouldn't the matrix that you multiply by the rotation matrix be (-1, 0) (0, 0) (1, 0) (0, 1) which isn't the set of points that you started with.

Since you started with the points (0, 1) (0, 0) (0, -1) (1, 0) then you would use the following matrix:

| 0 0 0 1 |
| 1 0 -1 0 |

as the matrix to be multiplied, I believe you end up with the points (-1,0), (0,0), (1,0), (0,1)

Mike
  • 8,137
  • 6
  • 28
  • 46
  • I always thought that that's how a matrix (vector) was arranged with (x, y) - each new coordinate (x, y) is a new column in your matrix. Is that not the case? – user3871 Apr 24 '13 at 15:44
  • @Growler I am not entirely sure, I just noted that you put the coordinates in your first matrix as (y,x) and took them out as (x, y) – Mike Apr 24 '13 at 15:46
  • Mike, am I adding `(-1,0), (0,0), (1,0), (0,1)` (the resulting points you put below) to my original coordinates `(1, 3) (1, 2) (1, 1) (2, 2)`? If that's the case, I get `(0, 3) (1, 2) (2, 1) (2, 3)` which doesn't plot correctly – user3871 Apr 24 '13 at 15:53
1

You have two mistakes.

Mistake 1:

You do this math:

(1-1, 3-2) (1-1, 2-2) (1-1, 1-2) (2-1, 2-2) =

(0, 1) (0, 0) (0, -1) (1, 0)

But the matrix you actually wrote down in your math (image) is:

[ -1 0 1 0 ]
[  0 0 0 1 ]

When it should have been:

[ 0 0  0 1 ]
[ 1 0 -1 0 ]

That is why it appears to be a 180 degree rotation, because you multiplied by the rotation matrix twice.

Mistake 2:

You should add all the output points to the origin.

You said:

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) = (0, 3) (1, 2) (1, 2) (1, 2)

But what you should really do is add them to the ORIGIN, i.e.

(0, -1) (0, 0) (0, 1) (-1, 0) - Matrix output

(0 + 1, -1 + 2) (0 + 1, 0 + 2) (0 + 1, 1 + 2) (-1 + 1, 0 + 2) - Add back the origin (origin coordinates in bold)

(1, 1) (1, 2) (1, 3) (0, 2) - Resulting points

Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158
  • Oops I guess that would make sense huh... obviously if I subtract the origin I'll need to add it back... oops haha – user3871 Apr 24 '13 at 16:07
  • Am I right in thinking that each new coordinate (x, y) is a new column in your matrix? – user3871 Apr 24 '13 at 16:11
  • I'm not exactly sure, but everything else in your post looked perfect to me except for that one "careless mistake" – durron597 Apr 24 '13 at 16:15
  • I just noticed that with your result `(1, 1) (1, 2) (1, 3) (0, 2)` from my original input `(1, 3) (1, 2) (1, 1) (2, 2)` as coordinates (x, y)... not (row, col)... all it's doing is mirroring the block horizontally 180 degrees... I don't see how that's possible since my multiplicative matrix was R(90 degrees), not R(180 degrees). Why is this so? – user3871 Apr 24 '13 at 16:19
  • @Growler I found the other mistake and edited my post accordingly – durron597 Apr 24 '13 at 16:47
  • damn stupid mistakes... I'm really not sure why I copied it down like that into the image :/ – user3871 Apr 24 '13 at 16:55
  • Also, 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. – user3871 Apr 24 '13 at 17:16
  • If you rotate the tetris piece around an origin that is far away, the entire piece will move very far, i.e. http://i1094.photobucket.com/albums/i445/vib-ribs/ROTATE.png?t=1298392967 – durron597 Apr 24 '13 at 17:34