This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
billysback's profile picture

[MATH] Rotate tetris pieces

Started by billysback, 07 October 2012 - 07:11 PM
billysback #1
Posted 07 October 2012 - 09:11 PM
The current tetris piece is stored as a table containing the location of each section of the block, how would I go around making it rotate (I know I should start by getting the upper left corner of the box containing all of the sections, and the code to do that, but I am not sure how I would rotate all the pieces of the block in relativity to that position, mainly because of the randomness of the block shapes)

This is the last thing which needs to be done before my tetris mini-game is 100% complete (kinda) D:
Kingdaro #2
Posted 07 October 2012 - 09:30 PM
For each piece, you could just store a collection of tables of tables that store the rotation frames:

Lpiece = {
{
  {0, 1, 0};
  {0, 1, 0};
  {0, 1, 1};
};
{
  {0, 0, 1};
  {1, 1, 1};
  {0, 0, 0};
};
{
  {1, 1, 0};
  {0, 1, 0};
  {0, 1, 0};
};
{
  {0, 0, 0};
  {1, 1, 1};
  {1, 0, 0};
};
}
But this would take a colossal amount of effort and really isn't worth it XD
Lyqyd #3
Posted 07 October 2012 - 09:44 PM
Store a list of x,y coordinates for each block, from an origin point. Each rotation around the origin is:

[x, y]
[-y, x]
[-x, -y]
[y, -x]
billysback #4
Posted 07 October 2012 - 09:54 PM
Hmmm… I'm not going to store them like that as it would be too much hastle but I could work around it by doing something like:

–> get upper left corner position of the box surrounding the block
–> get a new table displaying the difference of each position and the upper left corner position
–> rotate them accordingly
–> create new table which does upper left corner position + block position for every block.

Thanks :D/>/>