views:

807

answers:

3

I'm working on a roguelike style cellphone game that operates on a grid. Attacks/Items in this game operate by targeting tiles in a pattern relative to the user. The paturn is usually relative to the direction the user is aiming in as well, For instance in this screenshot the pattern

Point2d[] damageTiles = new Point2d[4];

damageTiles[0] = new Point2d(0, -1);
damageTiles[1] = new Point2d(0, -2);
damageTiles[2] = new Point2d(1, -2);
damageTiles[3] = new Point2d(-1, -2);

is show relative to a temporary enemy(the yellow box) aiming "up".

I currently have simple code to rotate a pattern array by 90 degree increments, as seen here. My question is, Is there an easy way to rotate an array of 2d points by 45 degree increments, allowing my Attacks/Items to fire on the diagonal, preferably without using floating point math, as it tends to run slowly on many phones(or so i hear). This is probably a trivial question to anyone familiar with graphics programming, but I've been hit with a case of coder's-block.

My current rotation code is shown below. I now realize that a better way to do it would be to take an angle instead of a "direction" and rotate the points by that angle (rejecting angles that are not multiples of 45 of course).

private Point2d[] rotateList(Point2d[] points, int direction) {
    for (int i = 0; i < points.length; i++) {

        if (direction == ROTATE_LEFT) {
            int temp = points[i].x;
            points[i].x = points[i].y;
            points[i].y = -1 * temp;
        }
        else if (direction == ROTATE_RIGHT) {
            int temp = points[i].x;
            points[i].x = -1 * points[i].y;
            points[i].y = temp;
        }

    }

    return points;
}
+3  A: 

You can use a simple rotation matrix to rotate each point: http://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm

wchung
A: 

sure, rotation counterclockwise in 2D (with positive x,y pointing to upper right) is just this (see rotation matrix in wikipedia)

[x']   [cos(theta)  -sin(theta)] [x]
[y'] = [sin(theta)   cos(theta)] [y]

for theta = 45 degrees, this becomes

 x' = 0.7071*x - 0.7071*y
 y' = 0.7071*x + 0.7071*y

(replace 0.7071 with sqrt(2)/2, I'm only using 4 digits here)

Jason S
+1  A: 

The T-shaped attack you show would be difficult to rotate at 45 degrees--things don't really translate from 90 to 45.

original:
.....
.###.
..#..
..o..

alternative A:
.....
.##..
.##..
...o.

alternative B:
..#..
.#...
#.#..
...o.

There could easily be others for that pattern.

I recommend you create a 45 degree "Pattern" to match you 90 degree pattern, then rotate the 45degree pattern exactly the same way you rotate your 90 degree patterns.

Bill K
I've just come to the same realization myself while trying out the rotation math in the other answers. I'll have to come up with a "diagonal" version of the patterns that need both, thanks for the help.
LoginError