Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Java 2D Rotation Question  (Read 2830 times)
0 Members and 1 Guest are viewing this topic.
Offline Vladiedoo
« Posted 2012-05-05 02:54:09 »

Hi, I currently use g.rotate to rotate the main player (currently a square), my problem is that g.rotate rotates everything drawn.

For example: g.rotate(myCharector.getRoation(mouseX, mouseY),
         (int) myCharector.getXCoordinate() + 10,
         (int) myCharector.getYCoordinate() + 10); //Rotation works as intended

                  g.fillRect((int) myCharector.getXCoordinate(),
         (int) myCharector.getYCoordinate(), 20, 20);
         
             if (characterMenu == true) {
                        //draws a characterMenu
                  }

I want the characterMenu to be drawn above the character but I don't want the characterMenu to be rotated along with the square. To fix the problem I had to call g.rotate with the opposite value so the rotation would cancel out. I feel as if this isn't the most efficient way of handling the problem though. Does anyone have any suggestions?

Thanks in advance for any help you provide, I really appreciate it!
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-05-05 03:39:25 »

You can rotate back, or you can get the current transform, create a new AffineTransform, rotate that, set the current transform to that, draw, then set it back to the original transform.   You can keep the rotation transform around so doing that is actually cheaper.

I'd show you code, but I don't do enough Java2D to actually translate it in my head.
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-05-05 03:47:53 »

Rotating the negative the value is the correct and most efficient way to do it.

You could go with creating a new temporary AffineTransform, but that requires the creation of an object.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 755
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-05-05 03:50:23 »

Rotating the negative the value is the correct and most efficient way to do it.

You could go with creating a new temporary AffineTransform, but that requires the creation of an object.
Rotating back introduces rounding errors in your matrix. If Java2D is in software rendering mode (or any mode, if you believe the rumors) then that tiny rotation will cause a huge drop in performance, for everything else you're going to render after that.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sproingie

JGO Kernel


Medals: 202



« Reply #4 - Posted 2012-05-05 03:50:33 »

Oh silly me, you can just grab the transform once, no need to explicitly create one.  Or just rotate back, though that does get annoying if you start stacking multiple transforms.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
+ AffineTransform t = g.getTransform()
g.rotate(myCharector.getRoation(mouseX, mouseY),
            (int) myCharector.getXCoordinate() + 10,
            (int) myCharector.getYCoordinate() + 10); //Rotation works as intended

g.fillRect((int) myCharector.getXCoordinate(), (int) myCharector.getYCoordinate(), 20, 20);
+ g.setTransform(t);
             if (characterMenu == true) {
                        //draws a characterMenu
                 }

Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-05-05 03:52:41 »

Rotating the negative the value is the correct and most efficient way to do it.

You could go with creating a new temporary AffineTransform, but that requires the creation of an object.
Rotating back introduces rounding errors in your matrix. If Java2D is in software rendering mode (or any mode, if you believe the rumors) then that tiny rotation will cause a huge drop in performance, for everything else you're going to render after that.
Oh right, rounding errors! Shocked

Oh silly me, you can just grab the transform once, no need to explicitly create one.  Or just rotate back, though that does get annoying if you start stacking multiple transforms.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
AffineTransform t = g.getTransform()
+ g.setTransform(new AffineTransform());
g.rotate(myCharector.getRoation(mouseX, mouseY),
            (int) myCharector.getXCoordinate() + 10,
            (int) myCharector.getYCoordinate() + 10); //Rotation works as intended

g.fillRect((int) myCharector.getXCoordinate(), (int) myCharector.getYCoordinate(), 20, 20);
g.setTransform(t);
             if (characterMenu == true) {
                        //draws a characterMenu
                 }

A little modification Wink

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 755
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-05-05 03:55:20 »

Once again the lack of push/pop in Graphics becomes obvious.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private LinkedList<Graphics> stack = new LinkedList<Graphics>();

public Graphics push(Graphics g) {
   this.stack.addFirst(g); // save it for later
  return g.create(); // copies the Graphic object
}

public Graphics pop() {
   return this.stack.removeFirst(); // restore it
}


1  
2  
3  
4  
5  
6  
7  
public void paint(Graphics g) {
   ...
   g = this.push(g);
   ...
   g = this.pop();
   ...
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-05-05 03:57:54 »

Ehhhh that's just overkill....

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 755
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-05-05 04:03:41 »

Infinitely better than juggling with setting/unsetting nested AffineTransforms.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Vladiedoo
« Reply #9 - Posted 2012-05-05 23:03:11 »

Thanks everyone, AffineTransform works wonders!

Riven I'll start studying push/pop thanks for bringing it up.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #10 - Posted 2012-05-05 23:11:05 »

Oh silly me, you can just grab the transform once, no need to explicitly create one.  Or just rotate back, though that does get annoying if you start stacking multiple transforms.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
AffineTransform t = g.getTransform()
+ g.setTransform(new AffineTransform());
g.rotate(myCharector.getRoation(mouseX, mouseY),
            (int) myCharector.getXCoordinate() + 10,
            (int) myCharector.getYCoordinate() + 10); //Rotation works as intended

g.fillRect((int) myCharector.getXCoordinate(), (int) myCharector.getYCoordinate(), 20, 20);
g.setTransform(t);
             if (characterMenu == true) {
                        //draws a characterMenu
                 }

A little modification Wink

There's no need to create and set a new transform, getTransform() returns a copy.
Offline Vladiedoo
« Reply #11 - Posted 2012-05-05 23:13:32 »

Yeah I had already gotten rid of that, thank you though for noticing that, I now feel 100% justified in my deletion of it.
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2012-05-05 23:53:56 »

O_o since when did getTransform() return a copy?! I have never known it did Shocked

Offline Vladiedoo
« Reply #13 - Posted 2012-05-06 01:21:19 »

O_o since when did getTransform() return a copy?! I have never known it did Shocked

Well I was referring to this.

AffineTransform t = g.getTransform()
+ g.setTransform(new AffineTransform());

I was actually getting errors with the above two lines of code so I changed it to

AffineTransform t = g.getTransform();

Everything worked as intended then.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #14 - Posted 2012-05-06 01:41:40 »

O_o since when did getTransform() return a copy?! I have never known it did Shocked

See if getTransform() == getTransform().  If it doesn't, then it's a copy.  Otherwise, Graphics2D is implementing transforms by creating a new transform and setting it.  I don't think getTransform() is abstract so the easiest answer is to just ctrl-click and check the source.

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #15 - Posted 2012-05-06 01:52:48 »

O_o since when did getTransform() return a copy?! I have never known it did Shocked

See if getTransform() == getTransform().  If it doesn't, then it's a copy.  Otherwise, Graphics2D is implementing transforms by creating a new transform and setting it.  I don't think getTransform() is abstract so the easiest answer is to just ctrl-click and check the source.

getTransform() is actually implemented in SunGraphics2D, but the fact that it returns a copy is stated in the Javadoc for the method as well.
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2012-05-06 01:53:23 »

Nah I checked the javadoc and it says it returns a copy. Wow I never saw that before. persecutioncomplex

EDIT: LOL beaten by BoBear by 8 seconds Tongue

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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!