Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tetris Piece Rotation Algorithm

What are the best algorithms (and explanations) for representing and rotating the pieces of a tetris game? I always find the piece rotation and representation schemes confusing.

Most tetris games seem to use a naive "remake the array of blocks" at each rotation:

http://www.codeplex.com/Project/ProjectDirectory.aspx?ProjectSearchText=tetris

However, some use pre-built encoded numbers and bit shifting to represent each piece:

http://www.codeplex.com/wintris

Is there a method to do this using mathematics (not sure that would work on a cell based board)?

like image 540
user21826 Avatar asked Oct 24 '08 14:10

user21826


People also ask

Is there an algorithm for Tetris?

The most basic algorithm for choosing blocks in a game of Tetris is the True Random version. This algorithm is equal to drawing from an urn of tetrominoes with replacement. Very fortunate and very unfortunate series of blocks are likely in an equal way.

What rotation system does Tetrio use?

The Super Rotation System, also known as SRS and Standard 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.


1 Answers

There is a limited amount of shapes, so I would use a fixed table and no calculation. That saves time.

But there are rotation algorithms.

Chose a centerpoint and rotate pi/2.

If a block of a piece starts at (1,2) it moves clockwise to (2,-1) and (-1,-2) and (-1, 2). Apply this for each block and the piece is rotated.

Each x is the previous y and each y - the previous x. Which gives the following matrix:

[  0   1 ] [ -1   0 ] 

For counterclockwise rotation, use:

[  0  -1 ] [  1   0 ] 
like image 141
Toon Krijthe Avatar answered Sep 30 '22 08:09

Toon Krijthe