Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tower Image Rotation Fixes  (Read 1790 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-01-27 18:44:18 »

If you have not read the previous topic I posted on this you may want to check it out, but that shouldnt be required. Anyway for my Tower Defense Game I can rotate a tower using this code:
1  
2  
3  
4  
5  
         double angle = Math.atan2(targetY - pixY, targetX - pixX) * 180 / Math.PI;
         g2d.translate(x + Screen.room.blockSize / 2, y + Screen.room.blockSize / 2);
         g2d.rotate(Math.toRadians(angle - 270));
         g2d.drawImage(Screen.tileset_air[airID], -Screen.room.blockSize / 2, -Screen.room.blockSize / 2, Screen.room.blockSize, Screen.room.blockSize, null);
         g2d.dispose();


but something strange happens. It will rotate just fine at certain angles for example if the mob is coming from the left, but if it is coming from the top or right, the tower wont stay on target and will just slightly go up and then down and so on. Can anyone see any faults in this code?
Thanks, cMp

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline StumpyStrust
« Reply #1 - Posted 2013-01-27 20:17:53 »

This is what I did in my particle system for Java2D and it work fine for rotation.

1  
2  
        Graphics2D g2d = (Graphics2D)g.create();
        g2d.setTransform(AffineTransform.getRotateInstance(rotation.x, loc.x, loc.y));


And of course dispose of the g2d. I never use the translate and rotation. Try that maybe?

Offline Slyth2727
« Reply #2 - Posted 2013-01-27 20:26:06 »

What would fade, rotation.x, loc.x, loc.y be in my game? X and Y coords of tower, mob I am guessing, but what about fade?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #3 - Posted 2013-01-27 20:58:41 »

What would fade, rotation.x, loc.x, loc.y be in my game? X and Y coords of tower, mob I am guessing, but what about fade?

The 'fade' variable stands for the level of ALPHA.
Ranges from 0.0 - 1.0 if I'm correct.
Has nothing to do with your issue though, from your opening post it doesn't look like you have alpha implemented.

Offline Slyth2727
« Reply #4 - Posted 2013-01-27 20:59:03 »

ah, thanks!

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #5 - Posted 2013-01-27 21:05:19 »

If you have not read the previous topic I posted on this you may want to check it out, but that shouldnt be required. Anyway for my Tower Defense Game I can rotate a tower using this code:
1  
2  
3  
4  
5  
         double angle = Math.atan2(targetY - pixY, targetX - pixX) * 180 / Math.PI;
         g2d.translate(x + Screen.room.blockSize / 2, y + Screen.room.blockSize / 2);
         g2d.rotate(Math.toRadians(angle - 270));
         g2d.drawImage(Screen.tileset_air[airID], -Screen.room.blockSize / 2, -Screen.room.blockSize / 2, Screen.room.blockSize, Screen.room.blockSize, null);
         g2d.dispose();


but something strange happens. It will rotate just fine at certain angles for example if the mob is coming from the left, but if it is coming from the top or right, the tower wont stay on target and will just slightly go up and then down and so on. Can anyone see any faults in this code?
Thanks, cMp

Cast to radians to degrees and then degrees to radians?
Does not seem an good implementation.

1  
2  
3  
4  
5  
6  
7  
                        private static float correction = Math.toDegrees(270);  //Rotation fix

         double angle = Math.atan2(targetY - pixY, targetX - pixX);
         g2d.translate(x + Screen.room.blockSize / 2, y + Screen.room.blockSize / 2);
         g2d.rotate(angle - correction);
         g2d.drawImage(Screen.tileset_air[airID], -Screen.room.blockSize / 2, -Screen.room.blockSize / 2, Screen.room.blockSize, Screen.room.blockSize, null);
         g2d.dispose();


Offline GabrielBailey74
« Reply #6 - Posted 2013-01-27 21:06:20 »

This is how I usually handle mine. Grin

Handles:
full 360D rotation if I'm correct.

Method for calculating the angle:
1  
2  
3  
4  
5  
public double getAngleToTarget(double playerX, double playerY, double targetX, double targetY) {
   final double deltaX = (targetX - playerX);
   final double deltaY = (targetY - playerY);
   return Math.atan2(deltaY, deltaX);
}

Method for obtaining the angle:
1  
2  
3  
final int centerX = absX + width / 2;
final int centerY = absY + height /2;
final double angle = getAngleToTarget(centerX, centerY, mouseX, mouseY);

Method for rendering with correct rotation:
1  
2  
3  
4  
5  
6  
7  
final double theta = Math.toRadians(angle);
// Apply the rotation.
g2d.rotate(theta, x, y);
// Draw the image.
g2d.drawImage(img, x, y, null);
// Restore the rotation back to normal for other rendering.
g2d.rotate(-theta, x, y);

So for yours I'd do something like:
1  
2  
3  
4  
5  
6  
7  
// Maybe (targetX, targetY, pixX, pixY)
final double angle = getAngleToTarget(pixX, pixY, targetX, targetY);
final double theta = Math.toRadians(angle);
g2d.rotate(theta, x, y);
g2d.drawImage(Screen.tileset_air[airID], -Screen.room.blockSize / 2, -Screen.room.blockSize / 2, Screen.room.blockSize, Screen.room.blockSize, null);
g2d.rotate(-theta, x, y);
g2d.dispose();

Hope this helps mate.

Offline StumpyStrust
« Reply #7 - Posted 2013-01-27 21:31:52 »

Ehh do not need that much code.

The rotation.x is the number of degrees you want to rotate by. Then the other two or the center coordinates of the sprite you are rotating. Once you find your angle in degrees just plug it into that method with your sprites location and bam. It does the translations I believe itself.

Offline Slyth2727
« Reply #8 - Posted 2013-01-27 21:35:01 »

alright, in the rotate method, which x and y do you mean? The center of the tower?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Slyth2727
« Reply #9 - Posted 2013-01-27 21:36:05 »

I think I am going to stick with rotate because I know nothing of affine rotation and that. Thanks for trying though Smiley

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #10 - Posted 2013-01-27 21:41:06 »

alright, in the rotate method, which x and y do you mean? The center of the tower?

I guess your X and Y are both:
1  
-Screen.room.blockSize / 2

According to:
1  
g2d.drawImage(Screen.tileset_air[airID], -Screen.room.blockSize / 2, -Screen.room.blockSize / 2, Screen.room.blockSize, Screen.room.blockSize, null);


In my example my 'x' stood for my Images 'x'.

lol, any further help needed? Shocked
Didn't understand what you were saying, you got it figured out, or you just don't want to use transforming.

Note: I don't think I ever use graphics.translate() when dealing with rotation, I just revert the rotation back to normal.

Offline Slyth2727
« Reply #11 - Posted 2013-01-27 21:43:58 »

Nah the x and y coords of tower is pixX and pixY, Screen.room.blocksize / 2 was used for positioning the image because rotate moves the image.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline StumpyStrust
« Reply #12 - Posted 2013-01-27 21:49:57 »

The location on the screen. So if you have an image at (200,200) you would use that.

AphineTransform just means that you are doing the rotation around that object as if the point you are giving it is the origin. What you are doing with the translate is basically an AphineTranform as you want the image to rotate around a different origin then the screen.

Here.

1  
2  
3  
4  
        Graphics2D g2d = (Graphics2D)g.create();
        double yourAngle = Math.atan2(targetY - pixY, targetX - pixX) * 180 / Math.PI;
        g2d.setTransform(AffineTransform.getRotateInstance(angle, pixX,pixY));
        g2d.dispose();


This assumes that your angle calculation gives the right angle.

Offline Slyth2727
« Reply #13 - Posted 2013-01-27 22:08:25 »

This is funny, that code does the exact same thing as the old code did, rotates it fine from bottom and left, but when the mob comes from the top or right it just goes back and forth for about 45 degrees. Any ideas?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline StumpyStrust
« Reply #14 - Posted 2013-01-27 22:33:43 »

hmm...angle calc is off is my guess.

1  
2  
         rotation = (float) (FastMath.atan2(velocity.y, velocity.x) * (180/FastMath.PI));
    rotation += 180;


Try this. FastMath is just a class for faster trig calls. You may need to change how much you add after the rotation. I do 180 as I want the particles to point from the left of the image. So if you have an image like this

[-----]
[-<--]
[-----]

it would keep the arrow pointing in the direction the image is going. If you have an image that is not pointing either left, right, up, or down then you will not get exact results as the source image is off. This is why if you look at most AAA games 2D textures they point in one direction not to an angle.

Offline Slyth2727
« Reply #15 - Posted 2013-01-27 22:37:05 »

why are you using float when affinetransform uses doubles? I thought it was a mistake in your first post, but now I guess its for a reason

EDIT: I am the most stupid person on this earth. I was putting angle as a double data type. Good lord. Thank you very much for your help you have solved a problem I have been trying to fix for a VERY long time Smiley

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Slyth2727
« Reply #16 - Posted 2013-01-27 22:47:40 »

If you wouldn't mind, off the top of your head what to you think a good way to rotate the image back to its starting position or next target once the mob is dead or out of range is?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline StumpyStrust
« Reply #17 - Posted 2013-01-27 23:16:35 »

Leave it  Grin

Or if there is no mob in range just pass in 90, 180, 270 or w/e for its rotation.

Edit:
In that post I did it because that is actually opengl which takes floats.

Offline Slyth2727
« Reply #18 - Posted 2013-01-27 23:42:20 »

haha ok man thanks for all your help

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (26 views)
2014-11-22 12:13:56

xFryIx (69 views)
2014-11-13 12:34:49

digdugdiggy (48 views)
2014-11-12 21:11:50

digdugdiggy (42 views)
2014-11-12 21:10:15

digdugdiggy (36 views)
2014-11-12 21:09:33

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (64 views)
2014-11-03 22:04:50

BurntPizza (62 views)
2014-11-03 18:54:52

moogie (77 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!