• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# need some help with tetric blocks rotation logic

This topic is 5822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Basically my "map" is a 2d array of pointers to CBlock objects. CBlock* m_map[width][height]; Right now the only way i can think about rotating the shapes is by hardcoding all the values in for each rotation state. The shapes are in a CForm class, which basically contains four blocks, each shapes if formed of blocks) like this [][][][] [][] [][] [] [][][] and so on. So i was just wondering if there was anything more logic to deal with the rotations.

##### Share on other sites
I store the tetris pieces in a square matrix and then rotate the entire matrix(either clockwise or counter-clockwise). This way, you can make myriad of tetris pieces that vary in size and shape.

// Rotates the contents of a square matrix
// with dimensions "size x size" clockwise.
// J points to the source matrix and
// K points to the resultant matrix.
int rotate_matrix_CL(int size, int **J, int **K) {
int n, m, helper;

if(J == NULL || K == NULL)
return -1;

helper = size - 1;

// A NxN matrix is altered by this double loop
// as follows:
// Row 1 becomes column N
// Row 2 becomes column N-1
// ........................
// Row N becomes column 1
for(n = 0; n < size; n++)
for(m = 0; m < size; m++)
K[m][helper-n] = J[n][m];

return 0;
}

// Rotates the contents of a square matrix
// with dimensions "size x size" counter-clockwise.
// J points to the source matrix and
// K points to the resultant matrix.
int rotate_matrix_CCL(int size, int **J, int **K) {
int n, m, helper;

if(J == NULL || K == NULL)
return -1;

helper = size - 1;

// A NxN matrix is altered by this double loop
// as follows:
// Row 1 is reversed and becomes column 1
// Row 2 is reversed and becomes column 2
// ........................
// Row N is reversed and becomes column N
for(n = 0; n < size; n++)
for(m = 0; m < size; m++)
K[helper-m][n] = J[n][m];

return 0;
}

##### Share on other sites
hmm I understand what you do, but what if i my array is not square. My height is bigger than my width, much like

o o o o o o o
o o o o o o o
o o o o o o o
o o o o o o o
o o o o o o o
o o o o o o o
o o o o o o o
o o o o o o o
o o o o o o o
o o o o o o o

i thought about just adding the missing part to form a square matrix then rotating but then it rotates all wrong. So i dunno

##### Share on other sites
Here are all the regular tetris pieces(1''s indicate the piece) in a square matrix(they will all rotate in the same way as they do in any tetris game):
I T J S
0000 0000 0000 0000
0000 0010 0000 0011
1111 0111 0111 0110
0000 0000 0001 0000

Z L 0
0000 0000 0000
0110 0001 0110
0011 0111 0110
0000 0000 0000

Hope this helps.

##### Share on other sites
Still lost here...

does the matrix have to be square to work?? ie only 3 X 3, 4 X 4 and so on???

Because the only thing I can think of is copying the whole 2d array to a matrix and then rotating it. But my array is WIDTH X HEIGHT meaning something like 12 X 17 i think.

##### Share on other sites
Your array that is 12x17 - that is the board, correct?
I am talking about the arrays that hold the pieces until they are placed on the board. Does this help?

##### Share on other sites
Yes the board is 12 * 17.

I tried doing this:

each time I press the rotate key,
I create a temporary matrix 4 x 4, put the shape in it, and then rotate the matrix.

But my problem is this:

the way a form is made is a CForm object, which contains an array of 4 CBlock object. each of those blocks have a 2d coords of where they are on the board.

So you see my problem, I can put the form in the 4X4 matrix easily, but when I rotate it, I dont get the real board coordinates, I get the matrix ones.

for example.

0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 1 1 1 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

ok so I have a CForm, composed of 4 CBlocks with those coordinates :

(2-6)
(3-6)
(4-6)
(5-6)

when i rotate those blocks in the matrix, the coordinates will stay the same for one, And I wont be able to just use the matrix[m][n] value because they will be way off.

GRRRRR hehe

no idea?

##### Share on other sites
Just have four versions of each block. One for every direction, then you don''t need to care a lot about rotating it. Just check keystroke and see if the ''rotated'' block fits, and if it dos, change the current block into it. Problem solved.

##### Share on other sites
A better idea would be to store the position of a piece, and within the piece have a list of 2d offsets of each of the blocks that comprise the piece. In this way you don''t constrain the dimensions of the piece, number of blocks in the piece, or number of orientations the piece can have. Although, you do have to hand-input each of the orientations for each piece, it''s flexible enough that you can write these into a data file and generate all the pieces at run-time.

i.e.
block s_block
orientation 0
0 0
1 0
1 1
2 1

orientation 1
1 0
0 1
1 1
0 2

etc.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 10
• 11
• 13
• 9
• 9
• ### Forum Statistics

• Total Topics
634082
• Total Posts
3015401
×