24
\$\begingroup\$

I have a gameboard grid which is 20 rows high and 10 columns wide with the origin (0,0) being in the top left hand corner. I have a tetrimino in play, which is composed of four blocks. I have the x,y location of each block relative to the top left hand corner origin. I also have the position of the pivot, the point around which I wish to rotate which is relative to the top left hand corner origin.

Each block has an integer position in a gameboard square and thus the pivot has a fractional position inbetween blocks e.g (10.5,10.5)

My question is: Which formula can I use to rotate each block around the pivot?

I have already looked at existing similar questions and answers but could not find an answer that I could get to work.

Help would be much appreciated.

\$\endgroup\$
5
  • \$\begingroup\$ belongs on game dev. \$\endgroup\$
    – Daniel A. White
    Commented Oct 1, 2011 at 11:26
  • \$\begingroup\$ and if its here ... show us some data structure \$\endgroup\$
    – nhed
    Commented Oct 1, 2011 at 11:29
  • \$\begingroup\$ If you're only rotating by multiples of right angles, then this should be fairly basic. Just draw a few diagrams, and you should be able to figure out an integer solution. \$\endgroup\$
    – Kerrek SB
    Commented Oct 1, 2011 at 11:34
  • 3
    \$\begingroup\$ I wouldn't bother to actually calculate the rotations, simply put all four possible rotated versions in an array. \$\endgroup\$ Commented Oct 1, 2011 at 12:30
  • 1
    \$\begingroup\$ I would also suggest what @FredOverflow said. If you look at my question (which is similar to yours), you see how I stored a single block for my implementation. \$\endgroup\$
    – bummzack
    Commented Oct 1, 2011 at 12:43

4 Answers 4

27
\$\begingroup\$

The standard Tetris rotation logic is called Super Rotation System. SRS is suited to high-level Tetris play, allowing for many variations on wall kicks and t-spins.

All tetrominoes exist inside a bounding square and rotate about the center of this square unless obstructed. Tetrominoes of width 3 (J, L, S, T, Z) are placed in the top two rows of the bounding square and (for J, L, and T) with the flat side down. I is placed in the top middle row.

All tetrominoes spawn in 2 usually hidden rows at the top of the playfield. They are placed in the center of these rows, rounding to the left. Once a tetromino lands, it does not lock until the lock delay expires. The lock delay behavior, called Infinity by the Tetris Company, resets the lock delay whenever the tetromino is moved or rotated. Hard drop is generally mapped to up, which has no lock delay.

Tetrominos in their rotation bounding boxes.

There is another common variant called ARS, which was used in Arika's Tetris: The Grand Master games.

\$\endgroup\$
11
\$\begingroup\$

General formula for rotating around origin is

xNew = x * cos(a) - y * sin(a)
yNew = x * sin(a) + y * cos(a)

For 90 degrees it becomes

xNew = -y
yNew = x

So, firstly get brick center coordinates relatively to pivot point:

x = xBrickCenter - xPivot
y = yBrickCenter - yPivot

Then rotate them around pivot point:

x1 = -y = yPivot - yBrickCenter
y1 = x = xBrickCenter - xPivot

And then add pivot coordinates to rotated point:

newXBrickCenter = xPivot + x1 = xPivot + yPivot - yBrickCenter
newYBrickCenter = yPivot + y1 = yPivot - xPivot + xBrickCenter

But if you need to rotate by multiples of 90 degrees, you can store four sets of brick positions for each orientation and switch between them instead of actually rotating.

\$\endgroup\$
0
\$\begingroup\$

You could rotate all square rings (a-through-h by 2 positions, A-through-P by 4 positions, and so on) around that pivot block:

ABCDE    MNOPA
PabcF    LghaB
Oh.dG -> Kf.bC
NgfeH    JedcD
MLKJI    IHGFE
\$\endgroup\$
-2
\$\begingroup\$

Follow the standard 2D rotation formulas in Mathematics should do your trick.

http://en.wikipedia.org/wiki/Rotation_(mathematics)#Matrix_algebra

\$\endgroup\$

You must log in to answer this question.