Help rotating an image

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…


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.

Ok, sorry… i think i solved this myself…

this is what ended up working for my purposes…

:slight_smile:

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

This is the final version of the method, in case anyone is interested…

/**
     * 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);
    }

You can also use:

    g2d.rotate(radians, centerx, centery);

and then draw the image at (0,0)

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

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.


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 ;D

you can find more of my code here

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


// 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