Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Help rotating an image  (Read 1597 times)
0 Members and 1 Guest are viewing this topic.
Offline leon945

Senior Newbie





« Posted 2010-05-12 19:44:07 »

Hi people..
Im developing my first game.. and i have run into a problem i dont quite know how to solve, which is rotating an image (sounds so easy).

For now i have this.. which kind of works..
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void rotateAndDraw(Graphics g, int x, int y, double degreesToRotate) {
        double radians = Math.toRadians(degreesToRotate);
        Graphics2D g2d = (Graphics2D) g; // Create a Java2D version of g.
       double centerx = image.getWidth(null)/2;
        double centery = image.getHeight(null)/2;
        g2d.translate(centerx,centery); // Translate the center of our coordinates.
       g2d.rotate(radians);  // Rotate the image by 1 radian.
       g2d.drawImage(image, x, y, image.getWidth(null), image.getHeight(null), null);
        g2d.rotate(-radians);  // Rotate the image by 1 radian.
       g2d.translate(-centerx,-centery); // Translate the center of our coordinates.
   }

The thing is, i dont actually understande what the translate method does..
And the problem im having is that, although the image IS actually rotating, the pivot point seems to be outside my image.

I though (based on some code i saw on a forum) that the translate method repositions the "pivot" point, but i dont know.. i cant seem to get it right..

any help will be greatly appreciated..
thanks in advance

-----------------------
update
i found this link.. which is useful..
http://www.glyphic.com/transform/plugin/1intro.html

at least i  think i understand what the translate does now.
Offline leon945

Senior Newbie





« Reply #1 - Posted 2010-05-12 22:16:39 »

Ok, sorry.. i think i solved this myself..

this is what ended up working for my purposes..

Smiley
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void rotateAndDraw(Graphics g, int x, int y, double degreesToRotate) {
        double radians = Math.toRadians(degreesToRotate);
        Graphics2D g2d = (Graphics2D) g; // Create a Java2D version of g.
       double centerx = x;
        double centery = y;
        g2d.translate(centerx,centery); // Translate the center of our coordinates.
       g2d.rotate(radians);  // Rotate the image by 1 radian.
       g2d.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
        g2d.rotate(-radians);  // Rotate the image by 1 radian.
       g2d.translate(-centerx,-centery); // Translate the center of our coordinates.
   }

Offline leon945

Senior Newbie





« Reply #2 - Posted 2010-05-13 01:46:27 »

This is the final version of the method, in case anyone is interested..
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
/**
     * Draw the sprite onto the graphics context provided with rotation applied.
     *
     * @param g The graphics context on which to draw the sprite
     * @param x The x location at which to draw the sprite
     * @param y The y location at which to draw the sprite
     * @param pivotX The x location of the rotation pivot point
     * @param pivotY The y location of the rotation pivot point
     * @param degreesToRotate Number of degrees to rotate the image.
     */

    public void rotateAndDraw(Graphics g, int x, int y,int pivotX, int pivotY, double degreesToRotate) {
        double radians = Math.toRadians(degreesToRotate);
        Graphics2D g2d = (Graphics2D) g;
        double centerx = x + pivotX;
        double centery = y + pivotY;
        g2d.translate(centerx,centery);
        g2d.rotate(radians);
        g2d.drawImage(image, -pivotX, -pivotY, image.getWidth(null), image.getHeight(null), null);
        g2d.rotate(-radians);
        g2d.translate(-centerx,-centery);
    }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2010-05-13 03:59:32 »

You can also use:

        g2d.rotate(radians, centerx, centery);

and then draw the image at (0,0)

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

Junior Member





« Reply #4 - Posted 2010-05-20 14:55:16 »

Hay, great, I was just about to ask a similar question  Smiley

I'm haveing trouble getting rotation and scaling to work. The way I'm doing things is slightly different, and it may be that you're way is better.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public void Render(Graphics2D g)
{
    // save the current transformation of the graphics device
   AffineTransform at = g.getTransform();

    // rotate g around my sprite's position
   g.setRotate(rotation, this.x, this.y);

    // scale my sprite (i use the same variable for x and y for uniform scaling - not needed to stretch a sprite yet)
   g.setScale(this.scale, this.scale);

    // draw the sprite
   g.drawImage( ... cant remember the exact method paramiters :P ...);

    // reset the graphics transformation back to the origional
   g.setTransform(at);

// end of method
}


now this seems to work well for rotation of individual sprites, but when I'm scaling them it seems ALL sprites are scaled, but keep their reletive positions. For example, if sprite A and B are 500px apart, and I scale A up to 2, whay I expect tog et it A is now twice as big as B, and both are still 500px apart.

However, what i actualy get is EVERY sprite is 2 times bigger, but the 500px sepperation is still the same.

Initialy I assumed that i was somehow setting the scale of my graphics2d during a Render() method somewhere, and then not resetting it - but this should mean that A and B would be scaled to being 1000px apart, right, because the ENTIRE graphics has been scaled. (I use this for my camera panning and zooming).

However, this doesnt seem to be the case, so I'm a bit baffled Grin

you can find more of my code here

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-05-20 15:14:22 »

For this case to work, you have to draw the image using:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
// move to rotation origin
g.translate(x, y);

// rotate relative to origin
g.rotate(theta);

// scale relative to rotated origin
g.scale(s, s);

// draw the image centered at the scaled rotated origin
int w = img.getWidth(null);
int h = img.getHeight(null);
g.drawImage(img, -w/2, -h/2 ... OR ... 0, null);

// restore transform

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

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

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

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

Riven (13 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
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

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!