Tower Defense: Rotating Tower

If you want something drawn away from the origin (i.e. most things you draw), you need to translate then rotate. Otherwise you’re rotating the axes you translate on, causing you to revolve instead of rotate.

And my bad, rotate takes radians in java2d. It’s degrees in slick (because OpenGL takes degrees).

Yes it is now rotating, buut not really correctly pointing at the target

Does this work?
Otherwise try + 45 or any correction so it points at an mob.


g2d.rotate(Math.toRadians(angle-45));

I found the perfect number to be - 270 :D! So the next thing: Getting the tower to rotate smoothly back to the next target?

You had to adjust it to -270 because your coordinate system is flipped on the y-axis… if you do

Math.atan2(pixY - targetY, targetX - pixX)

instead of

Math.atan2(targetY - pixY, targetX - pixX)

you don’t need to adjust.

Also, you are needlessly converted to degrees, then back to radians.

No, that doesnt work…

Oh, well that typically works for me. We must have something else different!

ay I suppose, but I gotta go somewhere, thanks for all of you guys’ help!

I think the source of why y is before x is in this alternative implementation of atan2


        if (fy == 0) {
            if (fx < 0) {
                return PI;
            }
            else {
                return 0;
            }
        }
        else if (fx == 0) {
            if (fy < 0) {
                return -ONE_HALF_PI;
            }
            else {
                return ONE_HALF_PI;
            }
        }
        else {
            float answer = atan(abs(fy/fx));
            if (fy > 0 && fx < 0) {
                return PI - answer;
            }
            else if (fy < 0 && fx < 0) {
                return answer - PI;
            }
            else if (fy < 0 && fx > 0) {
                return -answer;
            }
            else {
                return answer;
            }
        }

Note the atan(|y/x|)

I’m sure you guys are over complicating it with this long code. o.O Its not that hard to rotate a sprite. :confused:

Some people don’t like stopping at answering the question, it’s important to understand how things work behind the scenes.