//Note: I’m actually coding this in Java (with which I have just a few days experience), but as far as the logic goes, I thought this would still be an appropriate place.
I’m making a Tetris-eque game and having trouble with rotation. Each shape (rectangle, L, “step”, etc) consists of 4 or 5 smaller squares (image below), and I want to be able to rotate the pattern of squares 0, 90, 180 and 270 degrees around a designated point. I found this tutorial,
http://www.kirupa.com/developer/actionscript/trig_multiple_axis.htm
which contains the following formulas:
x = originX + [COLOR=navy]cos/COLOR * radius;
y = originY + [COLOR=navy]sin/COLOR * radius;
I thought I understood these, but apparently not, since I can’t seem to get it working…
For each shape, I would declare the x, y coordinates for each of its smaller squares, relative to one another. Then, I’d send these values to a function that should take the pattern and rotate it according to the specified angle. In Java (to the best of my knowledge), the upper-left is always the “registration point” for graphics. So, I set the relative coordinates for the squares of each shape such that the left side of the leftmost square is at x = 0 and the top side of the uppermost square is at y = 0. Here’s an example, the shape of a plus sign (+).
//top-center
x = squareWidth;
y = 0;
adjustAndBuildSquare(x, y, 0);
//middle-left
x = 0;
y = squareWidth;
adjustAndBuildSquare(x, y, 1);
//middle-center
x = squareWidth;
y = squareWidth;
adjustAndBuildSquare(x, y, 2);
//middle-right
x = 2 * squareWidth;
y = squareWidth;
adjustAndBuildSquare(x, y, 3);
//bottom-center
x = squareWidth;
y = 2 * squareWidth;
adjustAndBuildSquare(x, y, 4);
void adjustAndBuildSquare(int tempX, int tempY, int instance){
int x, y;
x = originX + cos(rotation) * tempX;
y = originY + sin(rotation) * tempY;
//other stuff
}
I tried to use the formula from the Kirupa tutorial. The only part that seems iffy, though, is the radius. Since the registration/rotation point is (relatively) 0,0, I thought using tempX and tempY seemed logical, but now I’m thinking I was wrong. I know I’ll need to make some adjustments because of the “top-left” orientation of the squares, but this looks way off.
This seems like a pretty simple problem, and I bet it’s something dumb that I’m missing. If anyone would be willing to help me get through this, I’d be very happy. Thanks for any efforts.
no rotation (just the shapes)
and this is what happens when I implement the rotation adjustment