#### Archived

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

# Rotation function for Tetris not working properly

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

## Recommended Posts

When I rotate the blocks for my Tetri game, they do not look right. After they rotate, the either loose some blocks or break up in some weird pattern. I know it''s how I''m rotating them, I just wondered if any of you know how it is done. Here is the code I''m using...
//-----------------------------------------------------------------------------

//	Rotate the block

//

void RotateBlock( )
{
int i, j, mat[ 4 ][ 4 ];

// Use temporary matrix for the rotated block

//

for ( i = 0; i < 3; ++i )
for ( j = 0; j < 3; ++j )
mat[ j ][ i ] = mainblock.mat[ 4 - j ][ i ];

// Check for collision using the temporary block

//

if ( CheckCollision( mat, mainblock.x, mainblock.y ) )
return;

// Swap values with the rotated block

//

for ( i = 0; i < 4; ++i )
for ( j = 0; j < 4; ++j )
mainblock.mat[ j ][ i ] = mat[ j ][ i ];
}

The idea for rotatingthe figures that way, was Jason Zelos tip in another post. I though I could really cut down on space and time with this idea, but it dons''t seam to be working

##### Share on other sites
Well, I'm not sure if this is intentional, but in your first matrix copy for loop you're only assigning the first 3 out of 4 on each variable, then in the second you're assigning 4 of 4, is that accurate? It's hard to tell since you haven't really specified what the matrices mean, I'm guessing they're block positions.

It also looks like you're mirroring the block rather than rotating it, if you're doing what I think you're doing. Try drawing it out and seeing what math it would take to rotate it.

I believe it should look more like this:
mat[ j ][ i ] = mainblock.mat[ i ][ 3 - j ];

-fel

[edited by - felisandria on June 8, 2004 3:46:06 PM]

##### Share on other sites
Okay, I actually pasted the code I was working on instead off the real code...sort of.
This is the code I’m working with

void RotateBlock( ){	char mat[ 4 ][ 4 ];	for ( int i = 0; i < 4; ++i )	for ( int j = 0; j < 4; ++j )		mat[ j ][ i ] = mainblock.mat[ 4 - i ][ j ];	if ( CheckCollision( mat, mainblock.x, mainblock.y ) )		return;	for ( int i = 0; i < 4; ++i )	for ( int j = 0; j < 4; ++j )		mainblock.mat[ j ][ i ] = mat[ j ][ i ];	update = true;}

The matrices are for the Tetris block figures. They are just little 4x4 tile maps. I just get a lot of weird shapes each time I rotate.

##### Share on other sites
Are you sure CheckCollision is working fine? Maybe you should post that code.

Zorx (a Puzzle Bobble clone)
Discontinuity (an animation system for POV-Ray)

##### Share on other sites
Check out my Tetris Tutorial #2 (or whole series) at http://acolyte.ozforces.com.au
There I explain on how to do proper rotation.

Good luck.

" Do we need us? "

##### Share on other sites
Change to that.
void RotateBlock( ){	char mat[ 4 ][ 4 ];	for ( int i = 0; i < 4; ++i )	for ( int j = 0; j < 4; ++j )// ----> SCREAM - CHANGE HERE 4 TO 3 SCREAM END <----		mat[ j ][ i ] = mainblock.mat[ 3 - i ][ j ];	if ( CheckCollision( mat, mainblock.x, mainblock.y ) )		return;	for ( int i = 0; i < 4; ++i )	for ( int j = 0; j < 4; ++j )		mainblock.mat[ j ][ i ] = mat[ j ][ i ];	update = true;}

You were reading some out of bounds memory.

[edited by - Endurion on June 9, 2004 10:25:26 AM]

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632993
• Total Posts
3009761
• ### Who's Online (See full list)

There are no registered users currently online

×