Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (778) Games in Android Showcase (231) games submitted by our members Games in WIP (856) 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 2945 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..
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.
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);    }`
 Games published by our own members! Check 'em out!
Riven
Administrator

« JGO Overlord »

Medals: 1357
Projects: 4
Exp: 16 years

Hand over your head.

 « Reply #3 - Posted 2010-05-13 01: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!
Dreamcatchermatt

Junior Devvie

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

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

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
Administrator

« JGO Overlord »

Medals: 1357
Projects: 4
Exp: 16 years

Hand over your head.

 « 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

 hadezbladez (350 views) 2018-11-16 13:46:03 hadezbladez (183 views) 2018-11-16 13:41:33 hadezbladez (349 views) 2018-11-16 13:35:35 hadezbladez (85 views) 2018-11-16 13:32:03 EgonOlsen (2179 views) 2018-06-10 19:43:48 EgonOlsen (2221 views) 2018-06-10 19:43:44 EgonOlsen (1379 views) 2018-06-10 19:43:20 DesertCoockie (2012 views) 2018-05-13 18:23:11 nelsongames (1671 views) 2018-04-24 18:15:36 nelsongames (2309 views) 2018-04-24 18:14:32
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 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