PDA

View Full Version : Finding the center of a tetris block...


AsLan^
2005-11-17, 04:13
Here's a good one,

Does anyone know an algorithm to find the center of a tetris block ?

I'm storing my blocks as x, y coordinates in multiples of 20, so the square looks like this:

blockOneX = 0;
blockOneY = 0;

blockTwoX = 20;
blockTwoY = 0;

blockThreeX = 0;
blockThreeY = 20;

blockFourX = 20;
blockFourY = 20;

and the three way looks like this:

blockOneX = 20;
blockOneY = 0;

blockTwoX = 0;
blockTwoY = 20;

blockThreeX = 20;
blockThreeY = 20;

blockFourX = 40;
blockFourY = 20;

Now, when I'm showing the next piece that the player will get, I want it to show up centered in a box on the side. The only problem is, I'm having trouble finding the center of the shape to make the correction when I draw it in my window (short of specifying the correction for each shape individually).

So, anybody come across this before ? I'm going to work on some more mundane parts of the game in the meantime.

Brad
2005-11-17, 04:46
Maybe I'm not seeing something, but it looks as simple as this:

(0.5*(x_min+x_max),0.5*(y_min+y_max))

No? :confused:

AsLan^
2005-11-17, 05:18
Maybe I'm not seeing something, but it looks as simple as this:

(0.5*(x_min+x_max),0.5*(y_min+y_max))

No? :confused:

As SIMPLE as that :) Sometimes I just cant see the forest for the trees, I was adding up all the indices and dividing them trying to come up with some magic number (didn't work).

Thank you Brad, unfortunately I just got tasked with making dinner so I'll have to try it out in an hour or two.

I'll most certainly let you know how it goes.

AWR
2005-11-17, 05:26
Damn you Brad, you beat me to it, again. :lol: :lol: :lol:

AsLan^
2005-11-17, 06:13
Howabout that, it mostly worked !

Seriously though, the lowest value in all cases was zero, so that didn't really do much to be added to the highest.

I had to add twenty to each value, pick the highest one (with twenty added) and add half of that.

I had to add the twenty to allow for odd block lengths to have an oddish offset. For example the square should have an offset of 20 but the three prong should have an offset of 30.

Works great now, thank you gentlemen.

Brad
2005-11-17, 13:39
Excellent!

Now, I expect some quality realism in this version of Tetris. Something along the lines of this (http://videos.somethingawful.com/mega64/tetris.mpg).

AsLan^
2005-11-18, 01:47
Excellent!

Now, I expect some quality realism in this version of Tetris. Something along the lines of this (http://videos.somethingawful.com/mega64/tetris.mpg).


It's for my wife, not quite finished yet...

http://catnipcomputer.net/catnip/Screenshot.png

Brad
2005-11-18, 03:06
Aww, how nice. :)

I trust that you saw the video, though, and realized I was trying to make a funny. :p

AsLan^
2005-11-18, 03:09
Aww, how nice. :)

I trust that you saw the video, though, and realized I was trying to make a funny. :p

My bad, it was a pretty funny video :)