Public Group

# Tetris Help

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

## Recommended Posts

Yeah, I'm kinda stuck sadly, and any help would be appreciated.

So my pieces are being stores in a 2D array.

For example my T_Block is:

	tBlock[0][1] = 1;
tBlock[1][1] = 1;
tBlock[2][1] = 1;
tBlock[1][2] = 1;


Which shows up fine as

X

XX

X

I have it on the screen, spawning at the top traveling down fine, collision fine, but I'm running into an issue with the rotation. I've googled for some help and started to implement a matrix multiplication method. Granted I know I'm going to be putting it into it's own helper function I literally just hardcoded it in just to test it out to see if it would work. The problem being that it seems to only do 2 of the possible rotations. For instance when I press the rotate key the first time it goes to:

X

XX

X

the second time it goes to

X

XXX

then cycles between those two.

void T_Block::rotate()
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (tBlock[j][i] == 1) {

//Store Original Point
int premodifiedX = j;
int premodifiedY = i;

// Pivot Point
int pivotX = 1;
int pivotY = 1;

// Find Relative coords
int relativeX = premodifiedX - pivotX;
int relativeY = premodifiedY - pivotY;

// 90 degree Rotation Matrix
int rotationTL = 0;
int rotationTR = -1;
int rotationBL = 1;
int rotationBR = 0;

// Get GLobal Coords Post Rotation
int rotatedGlobalX = (rotationTL * relativeX) + (rotationTR * relativeY);
int rotatedGlobalY = (rotationBL * relativeX) + (rotationBR * relativeY);

// Convert to Post Rotation Relative Coords
int rotatedRelativeX = rotatedGlobalX + pivotX;
int rotatedRelativeY = rotatedGlobalY + pivotY;

// If mark doesn't exist, mark it, otherwise leave it.
if (tBlock[rotatedRelativeX][rotatedRelativeY] == 0) {
tBlock[rotatedRelativeX][rotatedRelativeY] = 1;
tBlock[j][i] = 0;
}
}
}
}
}


Like I said I'm sorry again regarding it just being all hardcoded in there, it's midnight atm, and stayed up trying to implement this and it's kicking my ass. Thank you in advance.

##### Share on other sites
You are reading from tBlock and writing to the same array, which muddles things in strange ways. Try to use a different array as destination and then copy the result over to the original array.

##### Share on other sites

You are reading from tBlock and writing to the same array, which muddles things in strange ways. Try to use a different array as destination and then copy the result over to the original array.

Worked Perfectly. Thank you.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013443
×