Jump to content
  • Advertisement

Archived

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

sakky

Rotation function for Tetris not working properly

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!