6
\$\begingroup\$

I'm working on implementing the backend logic behind the classical tetris game, and I'm a bit confused on how a rotation is performed. I understand that you can apply a rotation matrix, but I still have some questions that are perhaps better explained with an example.

In my implementation, I use square matrices of 0s and 1s to represent the shape. e.g., I use a 4x4 matrix with a single row filled to represent the long horizontal block: $$ \begin{bmatrix} 1 & 1 & 1 & 1 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{bmatrix} $$

And for each block, I also keep track of the bottom right location of the 4x4 matrix relative to the location of the game board.

The question I have is, when you apply a rotation (in this case it would turn into a vertical block), do you have to fix some center? If so, how is this center of rotation defined?

e.g., I could define the center of rotation of the above as the top left element, so apply 90 degree clockwise rotation would yield:

$$ \begin{bmatrix} 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 \end{bmatrix} $$

But you could also make any of the other 15 elements the center of rotation, though it probably makes the most sense to make one of the 4 elements of the original block the center of rotation. I think the issue here is that for the other 3 elements, if you make any of them the center of rotation, part of the rotated block is going to lie outside the existing 4x4 window. So how is something like this prescribed in an implementation?

Alternatively you can swap the indices as in Tetris - Rotations using Linear Algebra (Rotation Matrices) without using a center of rotation, but I'm not sure which approach makes the most sense. Swapping indices is certainly much easier to implement.

\$\endgroup\$
2
  • 7
    \$\begingroup\$ Different versions of Tetris implement different rotation systems — if you pick one you want to implement, you can likely find it documented in some detail on fan wikis (e.g. this article on the Super Rotation System / current guideline standard). When a rotation would move an element outside the field or into collision with a placed block, a "wall kick" rule is used to decide how to shift it, and again different rules are used in different versions. \$\endgroup\$
    – DMGregory
    Commented Dec 25, 2023 at 16:45
  • \$\begingroup\$ @DMGregory ahh I was not aware there was not a single standard rotational systme for tetris. in my OP, I mentioned the swapping of indices (i.e., taking a transpose) method, which seems to correspond to the "right hand rotation system" (1st method in your link)? it seems the flow with that system is a 90 degree CW and CCW rotation would result in the same thing, and a 180 degree rotation is a no op? \$\endgroup\$ Commented Dec 26, 2023 at 2:07

1 Answer 1

18
\$\begingroup\$

It works however you (as he game designer) define it. Various implementations of Tetris have implemented rotations in different ways:

Super Rotation System is the current Tetris Guideline standard for how tetrominoes behave, defining where and how the tetrominoes spawn, how they rotate, and what wall kicks they may perform. Henk Rogers developed this system in order to unify all new Tetris games under the Tetris Guideline:

  • When unobstructed, the tetrominoes all appear to rotate purely about a single point & is solely a mathematical rotation as opposed to the combination of rotation and translation found in some other systems.

SRS Tetris rotations

The NES and SNES use a "right handed rotation system":

  • O : no rotations.
  • I : two rotations, favoring the lower half when horizontal, and the right half when vertical.
  • J, L, and T : 4 rotations centered around the middle square of the three square edge.
  • S and Z : four rotations, favoring the bottom and right sides.

NES Tetris rotations

The Game Boy Tetris is identical to the NES version it uses a left hand rotation for the S and Z pieces.

GameBoy Tetris rotations

Sega rotation system is similar to the NES version, except that:

  • The flat-side-down states of J, L, and T are pushed down by one space
  • S and Z round in different directions
  • I rounds differently from the other pieces.
  • I requires more space under it to rotate to a vertical orientation.

Sega Tetris rotations

The above mostly covers the 'basic' rotations - the various systems have varying rules for things like wall kicks and spawning orientations. More information can be found here.

Subjectively, I wouldn't expect the I rotation to behave as you've described in your question because it differs strongly from the precedents I'm familiar with, but that doesn't necessarily make it wrong. Test it out and focus on how it plays rather than how it's implemented. If it play tests well and meets your design goals, then it is a working design.

\$\endgroup\$
6
  • \$\begingroup\$ These illustrations are helpful. and I think I agree with your last paragraph. when I think about my original idea, i.e., swapping the column and row indices (equivalent to taking the transpose of the matrix), it doesn't make a whole lot of sense because according to that logic a 180 rotation would be a no-op. \$\endgroup\$ Commented Dec 26, 2023 at 2:12
  • \$\begingroup\$ do you have a good resource for how the rotations in your answers are coded up? \$\endgroup\$ Commented Dec 26, 2023 at 2:13
  • 2
    \$\begingroup\$ @user1750101 - since the number of configurations is limited, you can hardcode each rotation as a separate "piece", and then keep track what can transition into what. Another alternative is to have "base" pieces, and then come up with some scheme that produces a rotated piece from it. Basically, you can represent each (row, col) combination as a 2D vector, offset each so that (0, 0) is at the center of rotation, write the procedures for rotating left and right, then transform the result back to matrix indices (or alternatively, rotate first, then offset the whole block). \$\endgroup\$ Commented Dec 26, 2023 at 12:20
  • \$\begingroup\$ @FilipMilovanović makes sense. I was thinking about hard coding it, and it makes sense when there's limited pieces; however, for my implementation, I was thinking of introducing some non-conventional pieces, so I was hoping to have use a more generic approach that can rotate any piece. any clue what the scheme is? I feel like there should already be something available but I'm struggling to find it in my searches \$\endgroup\$ Commented Dec 26, 2023 at 15:47
  • 1
    \$\begingroup\$ @user1750101 - everything past my first sentence in the previous comment is about how to do it without hardcoding it. There might be other schemes as well - try searching for "tetris" on GitHub (and/or google "tetris github"), you'll probably find something, and the codebase is likely to be fairly small with only a few files to check. \$\endgroup\$ Commented Dec 26, 2023 at 19:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .