Public Group

# Tetris block rotation

This topic is 4369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 on other sites
I guess I will use that method then, thank you.

##### 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 on other sites
Quote:
 Original post by Alrecenkx2

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

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632659
• Total Posts
3007691
• ### Who's Online (See full list)

There are no registered users currently online

×