Java Tetris – 使用旋转矩阵的旋转

我正在用
Java构建俄罗斯方块,我正在尝试使用线性代数来旋转由4个瓦片组成的片段.

我的朋友在解释这样做的方法是:

他说:

“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)..”

所以我用坐标(1,3)(1,2)(1,1)(2,2)……与原点(1,2)这样做

然后他说:

“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)”

从每个坐标减去原点(1,2)

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

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

然后他说:

“Now rotate these four coordinates using the rotation matrix
multiplication, like we have been talking about.”

最后他说:

“Then add the origin coordinates back to each resulting coordinate,
and now you have your four rotated Tile coordinates.”

从上面的矩阵,我有(0,-1)(0,0)(0,1)(-1,0)……所以我将这些添加到原点坐标,就像他说的那样
(1-1,3 0)(1,0 0)(1 0,1 1)(2-1,2 0)=

旋转坐标:(0,3)(1,2)(1,2)(1,2)

但是,看看我的旋转形状……这是完全错误的:

有什么想法?

谢谢!

最佳答案 你有两个错误.

错误1:

你做这个数学:

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

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

但是你在数学(图像)中写下的矩阵是:

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

什么时候应该是:

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

这就是为什么它似乎是180度旋转,因为你乘以旋转矩阵两次.

错误2:

您应该将所有输出点添加到原点.

你说:

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)

但你应该做的是将它们添加到ORIGIN,即

(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

点赞