0

I am writing a Tetris Clone, it is almost done, except for the collisions. For example In order to move the Piece Z I use a method:

void PieceZ::movePieceDown()
{ 
  drawBlock (x1,y1++);
  drawBlock (x2,y2++);
  drawBlock (x3,y3++);
  drawBlock (x4,y4++);
}

and in order to rotate a Piece I use a setter (because coordinates are private). For rotation I use a 90 degree clockwise rotation matrix. For example if I want to move (x1,y1) and (x2, y2) is my origin, to get x and y of a new block:

newX = (y1-y2) + x2;
newY = (x2-x1) + y2 + 1;

That works to some extent, it starts out as:

0 0 0 0
0 1 1 0
0 0 1 1
0 0 0 0

Then as planned it rotates to:

0 0 0 1
0 0 1 1
0 0 1 0
0 0 0 0

And then it rotates to Piece S:

0 0 0 0
0 0 1 1
0 1 1 0
0 0 0 0

And then it just alternates between the second and the third stages.

My calculations are wrong but I can't figure out where, I just need a little hint.

  • To verify that transformation is correct rotate the origin block itself. The result should be that the block does not move. In your case `newX` remains `x2`, but `newY` becomes `y2 + 1`, instead of remaining `y2`. – Dialecticus Apr 26 '16 at 23:04
  • Also, different coordinates should not exist in the same expression. Since you are rotating a piece then all values that affect `x` coord must be of `y` kind, and all that affect `y` must be `x` in nature. – Dialecticus Apr 26 '16 at 23:08
  • The block you use as the origin, is updated after you calculate , but before you calculate and , so not all blocks are using the same origin coordinates. You should save a copy of , which will stay consistent throughout the entire transform calculation. – Christopher Oicles Apr 26 '16 at 23:14
  • @Dialecticus Thank you very much for your suggestion, I will make sure to verify my epxression like that! –  Apr 27 '16 at 13:15
  • @ChristopherOicles That is exactly what my problem was, that is why I add 1 to the y coordinate because otherwise the blocks are not in place –  Apr 27 '16 at 13:16

1 Answers1

0

Ok here is how it should go (somewhat):

  • Determine where you want to rotate the piece (this could be the upper or lower corner or the center) and call it origin
  • Calculate the new x newX = y - origin.y;
  • Calculate the new y newY = -x + origin.x;

This should work (I got this idea from wikipedia and rotation matrixes: https://en.wikipedia.org/wiki/Transformation_matrix)

DarthRubik
  • 3,927
  • 1
  • 18
  • 54
  • Thank you very much for your time! It's clear that mathematically expressions are correct, but they don't work for me. As **ChristopherOicles** mentioned it is probably because my origin value always changes, because I am implementing it. But fixing it doesn't fix it at all. I guess I have to play with coordinates to figure it out. –  Apr 27 '16 at 14:51