Tower Defense: Rotating Tower

No I am not using slick… just the build in java libraryu\

Zounds, that would be why it says Graphics2D. I’m such a dummy. :emo: (We need a :facepalm: emoticon).

Well pastebin anyway, I know a little more about transforms in Java2D, but not enough.

Ok, its a very large program though, here is the tower class and the cannon tower class: http://pastebin.com/qi7eNAde

Change


  g2d.translate(pixX, pixY);
  g2d.rotate(angle);

to


  g2d.rotate(angle);
  g2d.translate(pixX, pixY);

let me know how that goes.

Almost


Graphics2D g2d = (Graphics2D) g.create();
double angle = Math.atan2(targetY - pixY, targetX - pixX) * 180 / Math.PI;
- g2d.rotate(Math.toRadians(angle));
- g2d.drawImage(Screen.tileset_air[id], pixX, pixY, Screen.room.blockSize, Screen.room.blockSize, null);
+ g2d.translate(pixX + Screen.room.blockSize/2, pixY + Screen.room.blockSize/2);
+ g2d.rotate(angle);
+ g2d.drawImage(Screen.tileset_air[id], -Screen.room.blockSize/2, -Screen.room.blockSize/2, Screen.room.blockSize, Screen.room.blockSize, null);
g2d.dispose();

Im sure this can be optimized, but it will work.

well the image is on the tile its supposed to be on! the only problem is that it just spins around instead of pointing at the mob…

I think g2d.rotate(angle) should be called in radians, as you had before.

See:

http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Graphics2D.html#rotate(double)

Hmmm. Well it is obviously rotating a lot slower now, its actually about 45 degrees off though haha I will try and fix this

Ok, well if I use

		double angle = Math.atan2(targetY - pixY, targetX - pixX) * 270 / Math.PI;
[\code]

instead, it locks on fine, buuut it doesnt stay right on target..

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.