Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (652) 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 1801 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
« League of Dukes »

« JGO Overlord »

Medals: 1019
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
« League of Dukes »

« JGO Overlord »

Medals: 1019
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

You cannot reply to this message, because it is very, very old.

 SHC (24 views) 2015-08-01 03:58:20 Jesse (19 views) 2015-07-29 04:35:27 Riven (40 views) 2015-07-27 16:38:00 Riven (21 views) 2015-07-27 15:35:20 Riven (24 views) 2015-07-27 12:26:13 Riven (14 views) 2015-07-27 12:23:39 BurntPizza (36 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (28 views) 2015-07-24 06:06:53 NoxInc (36 views) 2015-07-22 22:16:53
 theagentd 51x wessles 47x basil_ 35x KaiHH 26x orangepascal 26x ags1 21x Riven 19x mooman219 17x bornander 14x KudoDEV 13x Jesse 11x CelestialCreator 11x princec 11x klaus 11x pquiring 9x philfrei 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 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