Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm building Tetris in Java and am trying to use linear algebra to rotate a piece composed of 4 tiles.

My friend was explaining the way to do it is:

He said:

"To clarify, you do need to rotate each point -- that is you need to rotate one point for each Tile in a Piece. But NOT four corners for each Tile in a Piece. The origin is like if you stuck a pencil through a piece of paper and spun the pencil around.. the spot where the pencil is is the origin."

"So if you have a T on your board with Tiles at (7,9) (8,9) (9,9), (8,10) and its origin is at (8,9).."

So I'm doing it with coordinates (1, 3) (1, 2) (1, 1) (2, 2)… with origin (1, 2)

Then he said:

"You translate the Tiles to be relative to the origin. That is, you treat the origin as the new (0, 0) for this rotation. That's as easy as just subtracting the origin from each coordinate, giving you (7-8, 9-9), (8-8, 9-9), (9-8, 9-9), (8-8, 10-9) or (-1, 0) (0, 0) (1, 0) (0, 1)"

Subtract origin (1, 2) from each coordinate

(1-1, 3-2) (1-1, 2-2) (1-1, 1-2) (2-1, 2-2) =

(0, 1) (0, 0) (0, -1) (1, 0)

Then he said:

"Now rotate these four coordinates using the rotation matrix multiplication, like we have been talking about."

enter image description here

Finally he said:

"Then add the origin coordinates back to each resulting coordinate, and now you have your four rotated Tile coordinates."

From the matrix above, I have (0, -1) (0, 0) (0, 1) (-1, 0)… so I add these to the origin coordinates like he says (1-1, 3+0) (1+0, 2+0) (1+0, 1+1) (2-1, 2+0) =

Rotated coordinates: (0, 3) (1, 2) (1, 2) (1, 2)

But, looking on my rotated shape... it's completely wrong:

enter image description here

Any thoughts why?

Thanks!

share|improve this question
1  
Like I said in my answer to your last question, you needn't try to learn matrix rotations gamedev.stackexchange.com/questions/54299/… –  Attackfarm Apr 24 '13 at 18:53
    
@Attackfarm That doesn't help with variable shapes –  Growler Apr 24 '13 at 19:12
    
It actually works not only for non-classic shapes, but for any arbitrary polygon on continuous axes (e.g. A star that isn't composed of blocks). The only case where my answer is inappropriate is with rotations less than 90 degrees. It would even work for 3-dimensional shapes, as long as the rotations were 90 degrees at a time –  Attackfarm Apr 24 '13 at 22:45
add comment

2 Answers

up vote 3 down vote accepted

Your example is stuffed with bugs and inconsistencies, it is really hard to read, but your understanding error seems to be in sentence 4:

add the origin coordinates back to each resulting coordinate

That is not the original coordinates, but the coordinates of the origin that you are rotating around, so (1, 2) in the example case.

By the way, should you desire to rotate a piece around the corner between 4 squares instead of the centre of a square you can use an origin where both the X and the Y component end in .5, the final results will still be integer.

share|improve this answer
1  
+1, 10 seconds faster after the question sat for an hour. –  Byte56 Apr 24 '13 at 17:24
    
Why is it that you need to translate into 1’s and 0’s for it to work (aka: why do I need to subtract the origin from all the coordinates before performing matrix multiplication on it...? Then need to add the origin back to each of the new matrix vectors for it to work? Why can't I just use the original coordinates for the matrix?) I’m not understanding the magic of a matrix. –  Growler Apr 24 '13 at 17:29
    
A matrix can only rotate around (0, 0), but by translating your desired centre of rotation to (0, 0) and back you effectively get a rotation around any desired point. The operation can be optimised to simply perform the rotation on the original matrix, and then add (X+Y, -X+Y) to the result, where (X, Y) is the desired centre of rotation, but that procedure may be slightly harder to grasp. –  eBusiness Apr 24 '13 at 18:03
add comment

(1-1, 3+0) (1+0, 2+0) (1+0, 1+1) (2-1, 2+0)

That's not adding the origin back, that's adding your offset rotation to the initial values.

Adding the origin back looks like:

 (0,-1) (0, 0) (0, 1) (-1, 0)
+(1, 2) (1, 2) (1, 2) (1, 2)
 -----------------------------
 (1, 1) (1, 2) (1, 3) (0, 2)
share|improve this answer
    
Yeap, I got it now. I misread what he wanted me to do. Btw, why is it that you need to translate into 1’s and 0’s for it to work (aka: why do I need to subtract the origin from all the coordinates before performing matrix multiplication on it...? Then need to add the origin back to each of the new matrix vectors for it to work? Why can't I just use the original coordinates for the matrix?) I’m not understanding the magic of a matrix. –  Growler Apr 24 '13 at 17:24
    
It's not translating into 1s and 0s, those just happen to be the values of the positions around the origin. Think of it this way: the matrix rotation rotates around 0,0. So, if you want to rotate your object around its origin, you need to move the object origin to 0,0 first. Then, after it's rotated, you move it back. –  Byte56 Apr 24 '13 at 17:29
1  
You should read through this series of three blog posts, it'll likely help you solidify your understanding. –  Byte56 Apr 24 '13 at 17:31
    
Thanks! That looks awesome –  Growler Apr 24 '13 at 17:34
2  
@Byte56 I already have the goblin on this site, you can get a unicorn or something. –  eBusiness Apr 24 '13 at 19:53
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.