0
\$\begingroup\$

I'm trying to get this character to spin in a circle to aim at the cursor. It all works pretty well, but I can't get the image to rotate around its center. Only around its corner. I've been trying to find a solution be using some crazy trig stuff, but I'm not getting anywhere.

public void draw(Graphics g, double inter)
{
BufferedImage im;
Graphics2D g2d = (Graphics2D)g;
GImage gImage;

gImage = GImages.getImage(p.sprite);
im = gImage.imageFile;
AffineTransform at = new AffineTransform();
at.translate((p.xcord+((float)p.width/2))+(p.xspd*inter), (p.ycord+((float)p.height/2))+(p.yspd*inter));
at.rotate(p.angle+(Math.PI/2));
at.scale((double)gImage.xsize/im.getWidth(), (double)gImage.ysize/im.getHeight());
//at.translate(-(float)p.width/2, -(float)p.height/2);
g2d.drawImage(im, at, null);
g2d.setColor(Color.BLUE);
g2d.drawRect((int)p.xcord,(int)p.ycord,1,1);
//g.setColor(Color.BLACK);
//g.drawString(p.suh, (int)(p.xcord+(p.xspd*inter)), (int)(p.ycord+(p.yspd*inter)));
}

p is my sprite object, the cords make up the point of the corner of the image. The sprite moves around the screen, so I can't use a fixed point method as far as I know. If anyone has any suggestions to make this work.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The rotate method has an overload which takes in an anchor point which will be used as the origin of the rotation. I'm not 100% sure how you calculate the center of your sprite in the framework you are using, but the cal should look something like the following:

at.rotate(your_angle, pos.x + image.width / 2.0f, pos.y + image.height / 2.0f);

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .