Rotating Tetris Piece

BlitzMax Forums/BlitzMax Beginners Area/Rotating Tetris Piece

Czar Flavius(Posted 2007) [#1]
I'm having difficulty getting pieces to rotate properly in my Tetris game. I can get them to rotate 90 degrees exactly, but if you play a decent game of Tetris you will notice that they rotate about a specific block, or pivot, in the piece, not just the top left corner. I've tried for ages to figure it out but I just can't :( The type TPiece has an array of TPoint, which contains a relative X and Y (ie 0, 0 1, 0 2, 0 1, 1 for T) and fields for the coordinates of the pivot (ie 1, 0). Any help will be much appreciated.


CS_TBL(Posted 2007) [#2]
Can't you just put your shape into a 4 4x4 array, and precalc all 4 angles into them? Then it's a matter of showing the right array.


Czar Flavius(Posted 2007) [#3]
I could have done, but I wanted to do it the "smart" way ;)

And also allow user-made pieces without giving the user a lot of work to do.


H&K(Posted 2007) [#4]
SetHandle( x#,y# )
or
SetImageHandle( image:TImage,x#,y# )

But if your not doing the collitions in that way then this was a useless post. But look them up, cos maybe you are


Czar Flavius(Posted 2007) [#5]
No afraid not.

The game consists of a 2D array grid and each cell can contain a block. The blocks are organised into shapes. Each shape has x/y origins, an array of x/y points that make up the block and its x/y pivot. Research tells me that this is a good way of doing it.

The array of points are relative to the shape's origin.

There are methods to place a piece and remove a piece, at point x/y. This loops through each point and adds the point's value to the origin to find out which cell of the grid should have a block added/removed.


ImaginaryHuman(Posted 2007) [#6]
You need to first `translate` the center of rotation from where it starts out to the center of the piece, ie add an offset. Then you do your rotation. So using set handle to the middle of the piece before rotating should do it?


MGE(Posted 2007) [#7]
"Can't you just put your shape into a 4 4x4 array, and precalc all 4 angles into them? Then it's a matter of showing the right array."

THAT's the smart way to do it. ;)


Czar Flavius(Posted 2007) [#8]
Except with my way you can have pieces of any arbitary size and shape... whether it be 4x4 or 6x2. But perhaps I am overthinking it?


CS_TBL(Posted 2007) [#9]
How much memory are you trying to save, anno 2007? If you have a piece of 6x2, then put it into a 6x6 array. in a byte array, the diff is 24 bytes.. whoopiedoo! I'd gladly trade 24 bytes for a headache.. ^_^ (well, 24x4 due rotated versions)


Czar Flavius(Posted 2007) [#10]
OK OK OK

I'm trying to replicate this, specifically the rotation code in the 4th yellow box.

http://www.codeproject.com/csharp/tetriscontrol.asp

It's not specifically to save memory, it's just I think this way is more elegant, and also I could have pieces of any shape or size I wanted without having to presize any arrays.

Also I want the pieces to be rotated automatically from just one set piece, so users can design their own pieces without bother.


CS_TBL(Posted 2007) [#11]
The latter is easy with those arrays. Just as the user 'submits' a new piece, rotate it 3x and there you have 'em.

But, it's all mere suggestions ofcoz, I'd go with arrays all the way tho.


Czar Flavius(Posted 2007) [#12]
I don't think you see what's the problem.

I can, and I have, made a version which uses a 4x4 array. Rotating the array by 90 degrees is easy. But the problem is not how the pieces are stored, but how they are rotated.

Take the long XXXX piece. When you rotate it, the pivot is usually on the second X. So when you rotate it, it goes around the second X. If you simply store it in an array and rotate it by 90 degrees, everything is rotated by 90 degrees, including the blank space. Not only is this not how Tetris is supposed to work, it also looks really odd as the piece jumps around an invisable box. Make a thick line in a blank square in paint, and try rotating it.

Or take the

XXX
..X

piece. Its pivot is the second block along the top. When you rotate it, that block always remains in the same place.

The problem is not about arrays, it's about how to rotate a shape around a pivot.


CS_TBL(Posted 2007) [#13]
Then:
....
XXXX
....
....

.X..
.X..
.X..
.X..

....
XXXX
....
....

.X..
.X..
.X..
.X..


Note that the last two models are the same as the first two. You could supply a rotate-amount per rotated version. The 3rd and 4th version would have the same rotate angles as the first respective two.

...
XXX
.X.

.X.
.XX
.X.

.X.
XXX
...

.X.
XX.
.X.



.XX
.X.
.X.

...
XXX
..X

.X.
.X.
XX.

X..
XXX
...



With all these, you don't need a pivot.


Grey Alien(Posted 2007) [#14]
I encounted the same issue a couple of years back when making Tetroid. First I made some code to rotate them "properly" about a pivot point but it played weird. I found out the "official" rotations and coded them in an array. I think it was CS_TBL who told me to do it that way then too! :-)


Czar Flavius(Posted 2007) [#15]
I've solved it, if anybody's interested.