I'm trying to rotate my Turret.png (small Gunhead with a laser in front) towards the mouses location on the screen. I tried just using my method for rotating projectiles towards the next step on their trajectory, but this produces unexpected results. Not only does the image not rotate about a fixed point, it also will clip based on the original images bounds.
My goal is for the turret to rotate about a fixed point on the gun head, so that the laser always points towards the mouse and the gun head is at a fixed position.
Here is an example of how I'm trying to get it to rotate; in this example the rotation was 78 degrees done in Illustrator. The red dot represents the origin.
My current code is as follows,
This method updates my Gun Tower and tells the turret the angle to rotate to:
public void setTargetAndDirection(int x, int y)
{
setTarget(x, y);
deltaX = getTargetX() - getX();
deltaY = getTargetY() - getY();
turret.setRotatedImage(turret.getActiveImg(), -getTargetAngle());
}
This is the method that actually handles the rotation:
public void setOriginRotate(BufferedImage imageIn, float angle)
{
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
int transparency = imageIn.getColorModel().getTransparency();
BufferedImage temp = gc.createCompatibleImage(
imageIn.getWidth(), imageIn.getHeight(), transparency );
Graphics2D g2d = temp.createGraphics();
int x = xLoc;
int y = yLoc;
g2d.setColor(Color.RED);
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.setToRotation(Math.toRadians(-angle));
g2d.setTransform(at);
g2d.drawImage(imageIn, 0, 0, null);
g2d.dispose();
displayImg = temp;
dispSizeX = displayImg.getWidth() / sizeMod;
dispSizeY = displayImg.getHeight() / sizeMod;
}
Any help would be greatly appreciated.