Frage

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!

War es hilfreich?

Lösung

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

Andere Tipps

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)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top