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

标签 java swing matrix linear-algebra rotational-matrices

我正在用 Java 构建俄罗斯方 block ,并尝试使用线性代数来旋转由 4 个方 block 组成的方 block 。

我的 friend 正在解释这样做的方法是:

他说:

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

enter image description here

最后他说:

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

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

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

enter image description here

有什么想法吗?

谢谢!

最佳答案

你有两个错误。

错误一:

你做这个数学:

(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

关于Java Tetris - 使用旋转矩阵的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16196039/

相关文章:

c++ - 四元数到旋转矩阵,使用特征库的值不正确

矩阵和数据框之间的逐行差异

java - Spring 和 SWT 可以一起用于 Java 桌面应用程序吗?

java - 如何通过操作添加列数据以修改其值

java - 使 JComponent 不透明

java - JProgressBar.setMaximum(int) 有时似乎不起作用

KeyListener 中的 Java 字符串比较失败

string - 将字符串拆分为最少数量的字符串,其中每个字母重复奇数次

java - Apache POI 和 EXCEL

Java 邮件逻辑 : Could not convert socket to TLS