Tetris Rotation Problem

Started by
13 comments, last by Mekuri 11 years, 6 months ago
So, I'm programming Tetris at the moment. I researched around on how people approached their design of Tetris. That's when I came upon this article. The one part I got stuck on was the explanation of rotating a piece. It reads:

Next, we compute the new location. Our goal is to keep the center of the falling piece constant (or, given that this is not possible if we have an even number of rows or columns, to keep the center as constant as possible). Keeping the center of the falling piece constant during rotation is the most difficult part of Tetris, so read this part very carefully (though that is always good advice!). Besides making rotation more intuitive, we want to keep the center constant so that if we rotate around and around, the center does not "drift" -- a full 360 degree turn should bring us back to where we started. To meet these conditions, we observe that we just need to adjust the left column and top row of the falling piece by subtracting half of the change in the size of each dimension that results from each turn, where the change in the size of each dimension equals the difference between the number of rows and columns (though you have to think about whether this difference should be positive or negative). Read and re-read the preceding paragraph. Draw pictures. Make sense out of it. When you finally convert it into Java, you will find there are two simple lines that make rotation-about-a-fixed-center work:
fallingPieceRow -= <something>;
fallingPieceCol -= <something else>;[/quote]

Doing as he said, I've been writing and drawing stuff for an hour, and I still haven't figured out what he means. Is he referring to subtracting the half of "change in size" from the top left most cell of each piece to keep the center orientated throughout rotations? I'm having trouble wrapping my head around what he meant.
Advertisement
I don't understand it either. I'm not sure why it needs to be that complicated. Just pick one square of the piece to be the center of rotation, and go from that.
He is poorly explaining that section.
When he says, "subtract half the change in size of each dimension that results from each turn" He means...

For my example, imagine the 'T' piece.
" the size of each dimension"- for the T piece, it is 3 squares across (the x dimension) and two squares up-and-down (the y dimension)

If you were to rotate the T piece clockwise, so the part currently sticking down is sticking out to the left, it would be flipped to 2 squares across (x) and 3 squares up-and-down (y)

The change in size for each dimension is then 3-2=1 (X dimension)
2-3 = -1(Y dimension)
half the change in size: 0.5(X Dimension)
-0.5(Y Dimension)

At least, thats what it seems like he's saying to me. I'm actually not sure how that helps, and like he says, " you have to think about whether this difference should be positive or negative."

I don't understand it either. I'm not sure why it needs to be that complicated. Just pick one square of the piece to be the center of rotation, and go from that.


This is what i did when i made tetris, i played some games of tetris, and just watched for what square was being used for rotations, it's pretty straight-forward imo.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
I assumed there was some logic behind it, mainly because what happens when there's a variable set of rows and columns for each piece, aka custom pieces. Normally, it would be easy to say "choose this center block," but I figured he was explaining it because when you do rotate a piece, it ends up in the same spot, but not using like a single cell of the array.
I preferred a source that described the game itself. Search for section 5.3 on this page: Link It helped me a lot.

I preferred a source that described the game itself. Search for section 5.3 on this page: Link It helped me a lot.

This is actually very helpful. Thank you!
I made a quick video tutorial that explains how to rotate a tetris block with matrices. I hope you find it hepful:

[media]
[/media]

Here's the Python code for the demo program (requires the pslab library):

[source lang="python"]# Block rotation demo - Goran Milovanovic, 2012
#
# Left click on empty cells to populate with blue
# Left click on blue cells to set pivot
# Right click to erase

import pslab
import time


class Cell:

slab_stamp = pslab.Slab(62, 62)
slab_stamp.fill(0xFFFFFF)

slab_empty = pslab.Slab(64, 64)
slab_stamp.burnInto(slab_empty, 1, 1)

slab_blue = pslab.Slab(64, 64)
slab_stamp.fill(0x0055FF)
slab_stamp.burnInto(slab_blue, 1, 1)

slab_green = pslab.Slab(64, 64)
slab_stamp.fill(0x00FF55)
slab_stamp.burnInto(slab_green, 1, 1)

def __init__(self, idx_row, idx_col):

self.pos = (idx_row, idx_col)
self.slab = self.slab_empty


def setSlab(self, slab):

self.slab = slab


def getSlab(self):

return self.slab


def getPosBoard(self):

return self.pos


def getPosScreen(self):

return (self.slab.getWidth() * self.pos[1],
self.slab.getHeight() * self.pos[0])

class Board(list):

def __init__(self):

self.dim = (8, 8)

super().__init__(
( [ Cell(i, j) for j in range(self.dim[1]) ] for i in range(self.dim[0]) )
)

self.pivot = None


def setPivot(self, cell):

if cell:
cell.setSlab(Cell.slab_green)

if self.pivot:
self.pivot.setSlab(Cell.slab_blue)

self.pivot = cell


def getPivot(self):

return self.pivot


def getBlueCells(self):

active = []

for row in self:
for cell in row:
if cell.getSlab() == Cell.slab_blue:
active.append(cell)

return active


def burnInto(self, slab_dest):

for row in self:
for cell in row:
slab = cell.getSlab()
slab.burnInto(slab_dest, *cell.getPosScreen())

def posToIdx(self, sx, sy):

slab = board[0][0].getSlab()

return (sy // slab.getHeight(), sx // slab.getWidth())

def posValid(self, pos_board):

for i, n in enumerate(pos_board):
if n > self.dim - 1 or n < 0:
return False

return True


def matMulVec(mat, vec):

vec_new = []

for row in mat:
vec_new.append(sum( (a * b for a, b in zip(row, vec) ) ) )

return vec_new



if __name__ == "__main__":

window = pslab.Window(512, 512)
window.setTitle("Block Rot")

window.fill(0xEEEEEE)

board = Board()
board.burnInto(window)

window.update()

mat_rot = (
(0, -1),
(1, 0)
)

mouse = window.mouse
kb = window.keyboard

while True:

window.processEvents()

lclick = mouse.btnHit("lmb")
rclick = mouse.btnHit("rmb")

if lclick or rclick:

mx, my = mouse.getPosition()

i_row, i_col = board.posToIdx(mx, my)
cell = board[i_row][i_col]
slab = cell.getSlab()

if lclick:
if slab == Cell.slab_blue:
board.setPivot(cell)
elif slab == Cell.slab_empty:
cell.setSlab(Cell.slab_blue)
else:
if cell == board.getPivot():
board.setPivot(None)

cell.setSlab(Cell.slab_empty)

board.burnInto(window)
window.update()


if kb.keyHit("space") and board.getPivot():

blue_cells = board.getBlueCells()

vecs = (cell.getPosBoard() for cell in blue_cells)

for cell in blue_cells:
cell.setSlab(Cell.slab_empty)

vec_p = board.getPivot().getPosBoard()

vecs_rel = ((a - b for a, b in zip(vec, vec_p)) for vec in vecs)

vecs_rel_rot = (matMulVec(mat_rot, list(vec)) for vec in vecs_rel)

vecs_new = ([a + b for a, b in zip(vec_p, vec)] for vec in vecs_rel_rot)

for vec in vecs_new:
if board.posValid(vec):
cell = board[vec[0]][vec[1]]
cell.setSlab(Cell.slab_blue)

board.burnInto(window)
window.update()


if kb.keyHit("esc"):
break

time.sleep(1/30)
[/source]

+---------------------------------------------------------------------+

| Game Dev video tutorials -> http://www.youtube.com/goranmilovano | +---------------------------------------------------------------------+

I made a quick video tutorial that explains how to rotate a tetris block with matrices. I hope you find it hepful:
I recovered my password and spent lots of time trying to retrieve my old account here just to say that that video tutorial of yours is really well done and helpful! GJ!

If you don't understand the stuff written here, please sharpen your C++ skills.

x = x*cos(angle) - y*sin(angle);
----------------------------------------
y = x*sin(angle) + y*cos(angle);

It's pretty hard for a lot of people because it involves Algebra, and we all know that many people prefer to only touch Algebra with a ten foot long stick.

I'll try and explain the situation as best as I can using C++ and SDL as an example, but the problem is the same in any library/language:

First, you might want to try and center the X and Y coordinates of the focus block(the block that's currently falling and you're moving):


int Xcenter;
int Ycenter;


Next, you can try and implement a median (the distance from the center of the focus block to its sides ) like this:


Xcenter -= MEDIAN * 2;
Xcenter += MEDIAN * 2;
Ycenter += MEDIAN * 2;


Then, you can use a coordinate variable (e.g. int x, int y, int x1, int y1, etc.) to call a function (accessor) to return the center of the square, and position it at its origin:


x1 = Xcenter;
y1 = Ycenter;


Then, perform the rotation like follows:


x2 = - y1;
y2 = x1;


Make the necessary adjustments to your control, engine, etc. to change the image in memory of the rotation angle by the 90 degrees or -90 degrees as necessary, and you can get your original location like:


x2 = Xcenter;
y2 = Ycenter;


Of course, there's a bit more to it than just that. You can't expect to rotate an L-block without pre-checking to see if there's a collision at the position, and without not knowing the positions in any sense, or else you'd expect a creepy little bug.

But you said rotation was your problem. The solution I enlisted shows you how to fix a median from a centered image, and then rotate negative or positive, and move around the center of the origin.

Of course, again, someone not being so sufficient in math would probably gain very little from this.

But it's basically a rotation of four 90 degree angles. Whatever you ultimately can put together is up to each person.

If it works, it works. Those are the basic steps. Cleaning up messy code, working on memory use, and fixing small bugs can done progessively through the production, but testing a well-working program always should be satisfactory.
Yes, this is red text.

This topic is closed to new replies.

Advertisement