I'm creating a Tetris clone right now. I've got the tiles of tetromino
stored as an std::array<tile, 4>
. Each tile
holds its (x,y) coordinates relative to the piece. For example, the t-block
is:
(-1, 0)
( 0, 0)
( 1, 0)
( 0,-1)
I want to create a function: rotate(const bool clockwise)
that applies a 2D rotation matrix to the tiles to rotate the block either 90 degrees clockwise, or 90 degrees counter-clockwise.
After doing some reading on rotation matrixes, I've found that a 2D rotation matrix looks like this:
cos(angle), sin(angle)
-sin(angle), cos(angle)
Since I'm doing 90 degrees only, I figure it should look like this:
//clockwise
0, 1
-1, 0
//counter-clockwise
0,-1
1, 0
After doing some more reading on how to multiply matrixes, multiplying the tile position by the matrix gives me this:
//(-1, 0) should end up as (0, 1) if rotated clockwise
-1 0, 1 1,-1
0 /*multiplied by*/ -1, 0 /*gives me*/ 0, 0
My question is how do I get an (x,y) position out of that result? Am I missunderstanding how matrixes work? Is there a more trivial way to rotate coordinates around the axis by 90 degrees?