need help on tetris rotation algorithm
Author |
Message |
fishtastic
|
Posted: Thu Mar 22, 2007 3:34 pm Post subject: need help on tetris rotation algorithm |
|
|
how do i rotate blocks in tetris with a rotate function
instead of writing 4 lines for one shape
block(1)(1):="1111000000000000"
block(1)(2):="1000100010001000"
block(1)(3):="1111000000000000"
block(1)(4):="1000100010001000" |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Clayton
|
Posted: Thu Mar 22, 2007 3:49 pm Post subject: Re: need help on tetris rotation algorithm |
|
|
It's kind of hard to judge right now without seeing more of your code. Basically though, find the centermost block, and then base the rest of the "spin" off of that. |
|
|
|
|
|
fishtastic
|
Posted: Fri Mar 23, 2007 8:52 pm Post subject: Re: need help on tetris rotation algorithm |
|
|
Freakman @ Thu Mar 22, 2007 2:49 pm wrote: It's kind of hard to judge right now without seeing more of your code. Basically though, find the centermost block, and then base the rest of the "spin" off of that.
ok. ok.
i guess it's hard to understand without explaining the code
this is what i'm trying to do:
i use 1 to represent block and 0 to represent nothing.
for example to store the 'stick' shape
1000
1000
1000
1000
i wrote
block(1)(1):="1000100010001000"
and for a 'L' shape
1000
1000
1100
0000
i wrote
block(2):="1000100011000000"
when i load the shape, i pick a point to start drawing and put it into an array
but since there is rotation i will need 4 lines to represent a single shape
cuz
for example after you rotate the L shape
it looks something like this
0000
0010
1110
0000
then i'll have to write block(2)(2):="0000001011100000"
so instead of writing 4 lines for each shape, is there a way to write a procedure for rotating block?
that work as
procedure rotate(block:array 1..288 of int , time:int)
for i:1..times
rotate(block)
end for
end procedure |
|
|
|
|
|
ericfourfour
|
Posted: Fri Mar 23, 2007 9:23 pm Post subject: Re: need help on tetris rotation algorithm |
|
|
Try to set it up this way. It will be much easier to understand.
You should load all blocks into the memory, in all of their rotated forms. This should occur before you start playing. That way, the user won't have to wait for it to load every time they rotate.
You can either hard code all of the rotated blocks, or you can use some simple trigonometry. To do either of these, take the centre-most square and treat it as the origin (0, 0). Rotate everything around it 90 degrees clockwise.
Ex.
Consider this block (pretend O's are squares):
Rotated 90 degrees clockwise, it would look like this:
To get that, take the centre-most square and find all of the coordinates of the squares around it. It would look like this:
code: | {(-1, 0), (0, 1), (0, 0), (1, 0)} |
The coordinates for the block rotated 90 degrees clockwise are:
code: | {(0, 1), (0, 0), (0, -1), (1, 0)} |
I figured those out by drawing out the squares and writing in all of the coordinates. This can take a very long time to do. That is why you should use trigonometry instead.
If you set up your tetris program this way, you should find it very easy to work with. |
|
|
|
|
|
richcash
|
Posted: Fri Mar 23, 2007 11:32 pm Post subject: Re: need help on tetris rotation algorithm |
|
|
I made a tetris game a long time ago. I used sine and cosine to help me rotate the shape to its new position. I would suggest you not store the positions as strings, I don't see the advantage of that. It's probably more flexible and easy to interpret if it's in an array of integers.
My code is not a perfect model (it was made a while ago), but it does use the same kind of rotation I think you're looking for (since you want to store the orginal positions and get the rotated ones based on these). Here it is. |
|
|
|
|
|
fishtastic
|
Posted: Sat Mar 24, 2007 12:14 pm Post subject: Re: need help on tetris rotation algorithm |
|
|
richcash @ Fri Mar 23, 2007 10:32 pm wrote: I made a tetris game a long time ago. I used sine and cosine to help me rotate the shape to its new position. I would suggest you not store the positions as strings, I don't see the advantage of that. It's probably more flexible and easy to interpret if it's in an array of integers.
My code is not a perfect model (it was made a while ago), but it does use the same kind of rotation I think you're looking for (since you want to store the orginal positions and get the rotated ones based on these). Here it is.
cool! your code is much shorter than mine.
i should limit my lines to 250 lines. if i use trig for rotating i might be able to make my code shorter.
but now i need to rewrite all the code for blocks.
btw, if i finish it i will submit it. the game control is really smooth. |
|
|
|
|
|
fishtastic
|
Posted: Sat Mar 24, 2007 1:20 pm Post subject: RE:need help on tetris rotation algorithm |
|
|
no!!
trig is not a good way to make rotation.
for example
lets say c is the center and x represent block
0x00
0xc0
00x0
0000
if you rotate is once around the center you get
0000
00cx
0xx0
0000
but if you rotate again you wont get the same shape as the first one
you will get
00x0
00cx
000x
0000
i guess string is the best way. |
|
|
|
|
|
ericfourfour
|
Posted: Sat Mar 24, 2007 1:49 pm Post subject: RE:need help on tetris rotation algorithm |
|
|
0 degrees:
0x00
0xc0
00x0
0000
90 degrees:
00xx
0xc0
0000
0000
180 degrees:
00x0
00cx
000x
0000
270 degrees:
0000
00cx
0xx0
0000
360 or 0 degrees:
0x00
0xc0
00x0
0000
It works fine for me. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
richcash
|
Posted: Sat Mar 24, 2007 2:24 pm Post subject: Re: need help on tetris rotation algorithm |
|
|
Well, I think fishtastic means that he feels that shape should be in the same position after 180 degrees and after 360 degrees.
But I agree with ericfourfour, most of the tetris games I've played have a piece like this in a different position after 180 and 360 degree turns (though I have seen it your way, and it does look just as nice). I think as long as the piece ends up in the original position after 360 degrees, it's fine.
If you really want it to be the other way, then I think you can still use trig to shorten the rotations. You'll just have to choose a different center of rotation for certain pieces (like the "S", "Z", etc.), which you can store in a parallel array or something. |
|
|
|
|
|
ericfourfour
|
Posted: Sat Mar 24, 2007 6:38 pm Post subject: Re: need help on tetris rotation algorithm |
|
|
If you want to avoid the whole trigonometry thing, here is another suggestion for a rotation algorithm.
Here is a list of all possible coordinates of the squares of the blocks in tetris (excluding the origin and diagonally from the origin since it will work with those as well):
Up: (0, 1) or (0, 2)
Right: (1, 0) or (2, 0)
Down: (0, -1) or (0, -2)
Left: (-1, 0) or (-2, 0)
I listed those in clockwise order (since that is the direction the blocks rotate in tetris). Do you notice a pattern?
Let me line up the coordinates so you can see them better:
code: | ( 0, 1)
( 1, 0)
( 0, -1)
(-1, 0)
or
0 1
1 0
0 -1
-1 0 |
The x coordinate is always equal to the y from the previous coordinate. The y coordinate is always equal to -x from the previous coordinate. This only works with 90 degree rotation.
So now instead of worrying about trigonometry, all you have to is swap the coordinates and slap in a negative sign in front of the y.
code: | (x, y) rotated 90 degrees = (y, -x) |
I guess you can say it is the inverse of -f(x) (is there a way to superscript?). |
|
|
|
|
|
fishtastic
|
|
|
|
|
fishtastic
|
Posted: Sat Mar 24, 2007 8:28 pm Post subject: RE:need help on tetris rotation algorithm |
|
|
richcash is right. i want some specific shapes to not to follow the trig rule.
you dont want to rotate a block form
this
0000
0xx0
0xx0
0000
to
0000
xx00
xx00
0000
because its not suppose the be rotate-able
my conclusion: Tetris does have the rotate function but it does not follow the rotating rule |
|
|
|
|
|
Drakain Zeil
|
Posted: Thu Mar 29, 2007 6:16 pm Post subject: RE:need help on tetris rotation algorithm |
|
|
instead of moving a 4x4 grid, move a set of co-ordinates,
eg, the L-shape has the relative co-ordinates:
(x+1,y+0),(x+2,y+0),(x+3,y+0),(x+3,y+1)
Where X and Y are the _object's_ location, with sub-locations for each block.... you can modify relative location to adjust the center of rotation, which can be found using some simple math to see the largest/smallest X/Y values, and finding the midpoint.
This allows you to define whatever shapes you want, bigger or smaller than 4x4, more efficent, and simply easier.
Run a check on each point to see if it's ontop of another block after each move.
To rotate, use trig math, and the object position as the point's center of rotation:
http://www.answers.com/topic/rotation-mathematics
^not as hard as it may look; grade 10/11 math, and you've really ony got 4 directions to worry about (but if you wanted, you could in theory go as many degrees of rotation, to make some other game). |
|
|
|
|
|
ericfourfour
|
Posted: Thu Mar 29, 2007 7:25 pm Post subject: RE:need help on tetris rotation algorithm |
|
|
Drakain Zeil its already been solved. There is no point in using trigonometry functions when you are only rotating 90 degrees clockwise.
(x, y) rotated 90 degrees clockwise = (-y, x)
fishtastic you are sort-of right. The blocks are actually rotated around the exact centre of the block. We were just suggesting the block closest to the centre because it is simple.
I guess if you want to do this the "math" way, you only need the 0 and 90 degree rotation for the symmetrical blocks (1 rotation) and the 0, 90, 180, and 270 degree rotation for the asymmetrical blocks (3 rotations). |
|
|
|
|
|
Drakain Zeil
|
Posted: Fri Mar 30, 2007 8:13 am Post subject: RE:need help on tetris rotation algorithm |
|
|
Yes it may have _A_ solution, but I am offering another method. Are these threads not searched for help?
(x,y) >> (-Y,X) is around the origin, and can be awkard.
http://homepages.inf.ed.ac.uk/rbf/HIPR2/rotate.htm
Using trig you can rotate both directions, any degree, at any point of rotation, where as modifying a 2 dimentional array can be tricky, time-consuming in debugging, and you have more work to do in the long run. YES you can do it the grid-block way, but point-block is a bit more math, less code, and faster execution. You can do much more than standard tetris with this method.
If you are going to rotate a matrix, use matrix rotation, and keep the point of rotation. It's still more work however.
http://www.euclideanspace.com/maths/algebra/matrix/orthogonal/rotation/index.htm
The point in using an array of points? They are more modular than an array of O's and X's, you can make the game more versitile: two objects, the falling block, and the pit's mega-block. Merge the falling block's points into the mega-block's array. Seperate the mega-block when a line is formed, and drop the higher one (now a falling block) down a level. You can even have many falling blocks, and different directions. If the falling blocks colide they could even form a new block.
It's easier to say: add these 4 points, instead of: add these 16 characters into the array, and do this if char at (x,y) is this, because while it is a 4x4 array, my block is 1x4.
In a point-system, you check all blocks in the falling block for being ontop of the mega-block. You can easily optimize the code to do this:
block[L][W] (as a structure with .x and .y as ints), where L and W are the witdth of your game zone. Check at L-1, accross W. If it's ontop of one of the blocks, merge them all into the new one, and send off a new block.
If you are rotating around the center of the block, and not one of the X's in an array, you don't have to worry about the cube object moving on rotate, you simply rotate it around it's geometic center, and it does rotate, but stays the same. |
|
|
|
|
|
|
|