Tetris
Algorithm
Game Development
Piece Rotation
Programming

Tetris Piece Rotation Algorithm

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Tetris is a timeless puzzle game that involves arranging falling tetrominoes (shapes made up of four squares) in a well. The challenge comes from rotating these pieces optimally to fit them into the existing structure. In this article, we will explore the algorithms used to handle Tetris piece rotation, including mathematical models, data structures, and implementation strategies.

Mathematical Background

At the heart of Tetris piece rotation are basic linear algebra and geometric transformations. Each tetromino can be represented by a matrix of its position on a grid. To rotate these matrices, we utilize concepts from linear algebra. The two primary operations are reflection and transposition:

  1. Rotation Matrix: Depending on the direction (clockwise vs counter-clockwise) and the angle of rotation (usually 90° or 270°), the rotation matrix is defined as:
    Clockwise by 90°:

R_90=[0110]R\_{90} = \begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix}

Counter-clockwise by 90°:

R_90=[0110]R\_{-90} = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}

  1. Matrix Multiplication: By applying the rotation matrix to a tetromino's position matrix, we update the tetromino's orientation. Multiplying the piece's coordinates by the rotation matrix results in a new set of coordinates.

Implementation Strategies

There are several ways to implement piece rotation in code. Below, we discuss array indices and bounding box methods widely adapted for efficiency in games.

Array-Based Approach

Tetrominoes are often represented by 4x4 grids in a two-dimensional array, where each position is either filled or empty. The key is to redefine this array as it rotates:

Zip Function: This Python function helps transpose arrays by unpacking rows into tuples of columns. • Reversing Columns: After transposing, we reverse each column to achieve a 90° rotation. • Wall Kick: If a rotation attempt results in a collision, the game engine tests several predefined offsets that "kick" the piece into an adjacent valid space. These offsets and tests are typically stored in a lookup table. • Rotation States: Store rotations as arrays of arrays for each piece. Each entry corresponds to a 90° rotated state. • Lookup Tables: Pre-calculate rotations and collision checks, making the game loop faster as it only accesses table data. • Mirrored Symmetries: Some tetrominoes behave the same upon flipping and thus can share rotation logic, reducing the number of unique cases to handle. • Pre-Computation: Use pre-computed rotation states and wall kicks stored as constants. • Bit Manipulation: Represent tetromino states with bitflags for rapid checking and manipulation.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.