Advertisement

Tetris Help

Started by March 04, 2015 05:06 AM
1 comment, last by Jay Pascua 9 years, 10 months ago

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.

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.
Advertisement

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.

This topic is closed to new replies.

Advertisement