17.2.3 Move and Rotate

Now to get your block moving. The Move method takes a direction as an argument. You should use a switch to, depending on the direction, adjust the x or y position. (Note: This position is the grid position, not the position on the screen. Therefore it should be increased by one. Using a time delta is not needed here.)

Once you have adjusted the position of the block, you must also pass the direction to all squares, so that they will be moved also. Add the necessary code to the Move method.

The rotate method is slightly more difficult. We need to rotate all the squares around the position of the block. This works best if the position of the block is (0,0), because rotating around another point is much more complicated. However, the block is not likely to be at that position. Therefore, we will first subtract the position of the block from the position of the square. After that, we exchange the x and y position of the square, and we add the position of the block back to the position of the square. Finally, we put the new position back into the square. The code looks like this:

 
void rotate() 
{ 
   FREPA(squares) 
  { 
5      VecI2 pos = squares[i].getPos(); 
      pos -= T.pos; 
      VecI2 newPos(-pos.y, pos.x); 
      newPos += T.pos; 
      squares[i].setPos(newPos); 
10  } 
}  

Now run your test application again. You should now be able to move and rotate a block. Test with different starting positions to make sure it always works.