4
\$\begingroup\$

enter image description here

I'm creating a Tetris clone right now. I've got the tiles of tetromino stored as an std::array<tile, 4>. Each tile holds its (x,y) coordinates relative to the piece. For example, the t-block is:

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

I want to create a function: rotate(const bool clockwise) that applies a 2D rotation matrix to the tiles to rotate the block either 90 degrees clockwise, or 90 degrees counter-clockwise.

After doing some reading on rotation matrixes, I've found that a 2D rotation matrix looks like this:

 cos(angle), sin(angle)
-sin(angle), cos(angle)

Since I'm doing 90 degrees only, I figure it should look like this:

//clockwise
 0, 1
-1, 0

//counter-clockwise
 0,-1
 1, 0

After doing some more reading on how to multiply matrixes, multiplying the tile position by the matrix gives me this:

//(-1, 0) should end up as (0, 1) if rotated clockwise

-1                     0, 1                1,-1
 0  /*multiplied by*/ -1, 0  /*gives me*/  0, 0

My question is how do I get an (x,y) position out of that result? Am I missunderstanding how matrixes work? Is there a more trivial way to rotate coordinates around the axis by 90 degrees?

\$\endgroup\$
  • \$\begingroup\$ If you multiply a 2-element vector by a 2x2 matrix, you should get two elements (either in a row or in a column, depending on details). \$\endgroup\$ – user253751 Oct 12 '15 at 1:19
  • 3
    \$\begingroup\$ If you're doing a faithful clone, don't forget to apply the correct rotation rules: tetris.wikia.com/wiki/SRS \$\endgroup\$ – Arturo Torres Sánchez Oct 12 '15 at 2:33
4
\$\begingroup\$

At 90 degrees, you can work out rotations in both directions just by swapping and reversing positions. Begin with the following shape:

enter image description here

Ahead, we rotate it manually clockwise, and look at the resulting positions (grey dots are rotated positions):

enter image description here

Analyzing point B's position: Before transforming, it was (1, 2). After transforming, it was (-2,1). If you look at the other points, they all follow the same pattern: Protated=(Py,-Px)

Applying the same principle you can find a similar property for counterclockwise 90-degree rotation: Protated=(-Py, Px)

And we never touched any matrices! However, note that this method rotates around the center (0,0)! So you should operate on the relative positions of the tiles, and not their world position (you would have to do that too with matrices, but it's always good to note).


Reiterating:

  • Clockwise: Protated=(Py,-Px)
  • Counter-clockwise: Protated=(-Py, Px)

Addendum: All paragraphs in this answer start with the letter A. Don't ask me why...

P.S.: Here is a interactive demo (requires Java): http://tube.geogebra.org/m/Qg9fcEwE

\$\endgroup\$
  • 1
    \$\begingroup\$ Ooh, very clever! I'm implimenting and testing it right now. :) \$\endgroup\$ – Willy Goat Oct 11 '15 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.