Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Help rotating an image  (Read 2607 times) 0 Members and 1 Guest are viewing this topic.
leon945

Senior Newbie

 « Posted 2010-05-12 17: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..

-----------------------
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.
leon945

Senior Newbie

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

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

this is what ended up working for my purposes..

 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.    }`

leon945

Senior Newbie

 « Reply #2 - Posted 2010-05-12 23: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);    }`
Riven

« JGO Overlord »

Medals: 1336
Projects: 4
Exp: 16 years

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

You can also use:

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!
Dreamcatchermatt

Junior Devvie

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

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

you can find more of my code here

Riven

« JGO Overlord »

Medals: 1336
Projects: 4
Exp: 16 years

 « Reply #5 - Posted 2010-05-20 13: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 origing.translate(x, y); // rotate relative to origing.rotate(theta);// scale relative to rotated origing.scale(s, s);// draw the image centered at the scaled rotated originint 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

 nelsongames (16 views) 2018-04-24 18:15:36 nelsongames (12 views) 2018-04-24 18:14:32 ivj94 (587 views) 2018-03-24 14:47:39 ivj94 (49 views) 2018-03-24 14:46:31 ivj94 (400 views) 2018-03-24 14:43:53 Solater (64 views) 2018-03-17 05:04:08 nelsongames (110 views) 2018-03-05 17:56:34 Gornova (175 views) 2018-03-02 22:15:33 buddyBro (723 views) 2018-02-28 16:59:18 buddyBro (93 views) 2018-02-28 16:45:17
 KaiHH 13x ByerN 11x SHC 10x NuclearPixels 10x Zemlaynin 10x Guerra2442 9x Damocles 6x VaTTeRGeR 5x ags1 4x Spasi 4x orangepascal 4x philfrei 4x mesterh 3x princec 3x ndnwarrior15 3x Springrbua 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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