Sign in to follow this  
password

Tetris block rotation

Recommended Posts

I have an 4x4 vector like this: {0, 0, 0, 0 0, 0, 0, 0 0, 0, 0, 0 0, 0, 0, 0} And I want to make it possible to rotate the blocks by pressing the up button. I managed to do it on one block by using the most inefficient and ugliest method ever and want to stay away from that method if possible.. Let's say the current block is an L-block. 0 1 0 0 0 1 0 0 0 1 0 0 1 1 0 0 And I want the vector to look like this for example, when pressing the up button. 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 0 And like this the next time I press it the next time. (in the same pattern) 0 1 1 0 0 1 0 0 0 1 0 0 0 1 0 0 Anyone have any ideas how to accomplish this?

Share this post


Link to post
Share on other sites
If your going to store it as a vector, why not just keep 4 vectors for each shape in an array and index them depending on which one you want.

Sometimes the simplest way to do something, is the simplest way to do something. Don't use complex rotation's if you really don't need them.

Share this post


Link to post
Share on other sites
I agree with the poster above. It is much easier to create a few extra vectors to make your life easier, since it won't have much of an impact on memory use.

But, if you're determined to calculate the rotation, you might consider taking another approach than the 4x4 grid.

One way of looking at a tetris-piece is seeing it as a center block with three blocks around it(or if you're set on having five blocks, there'll be four blocks around it). Because you have a center block you can pivot around, calculations to rotate the piece are that much easier.

example:
the "EL"
#
*
##

* would be the center block and # the surrounding ones.
In a vector you'd put the coordinates of the center block in the first elements (which is always (0,0)) and the remaining blocks, by there relative coordinates to the center block, in the following elements((0,1) (0,-1) (1,-1))

Now, to rotate a piece you just have to figure out where all the possible relative coordinates go(eg. (0,1) becomes (1,0), (0,-1) becomes (-1, 0) and (1, -1) becomes (-1,-1))

I hope that made the slightest piece of sense... ^^
Anyway, DO realise that Homers Pal's method is easier to implement.

Share this post


Link to post
Share on other sites
x2<y
y2<-x
performs a 90 degree rotation on a 2D vector and
y2<x
x2<-y
rotates the other way
if you store the pieces as a set of coordinates relative to a center(as @root suggested) then it is very easy to rotate by applying the above equations.
Also, there is no such thing as a 4X4 vector. As defined by dictionary.com a vector is: (Computers) an array of data ordered such that individual items can be located with a single index or subscript.
If it has two subscripts it's a matrix.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alrecenk
x2<y
y2<-x
performs a 90 degree rotation on a 2D vector and
y2<x
x2<-y
rotates the other way
if you store the pieces as a set of coordinates relative to a center(as @root suggested) then it is very easy to rotate by applying the above equations.
Also, there is no such thing as a 4X4 vector. As defined by dictionary.com a vector is: (Computers) an array of data ordered such that individual items can be located with a single index or subscript.
If it has two subscripts it's a matrix.


You most have misunderstood what I meant with vector. I am referring to std::vector which is used as an array.

Edit:
seems like everyone misunderstood what I meant with vector. Well, I managed to get a decent idea from your replies so it doesn't matter.

Share this post


Link to post
Share on other sites
No I understood fine what you meant by vectors, but even then you don't need to do that, just index into a list of 4xshape1, 4xshape2, 4xshape3 egt to get to the right sequence of data you want for a particular shape. Or store 4 elements of an array or vecotr, in a single or 2D array.


have fund.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this