Image Rotating Around Mouse
I'm trying to make an overhead shooter and I made some code to test my getAngle() method but its not working. It would be awesome if anybody could shed some light as to why the image doesn't rotate correctly.
Code:
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.BLUE);
AffineTransform t = new AffineTransform();
t.rotate(turn,origin.x,origin.y);
g2.transform(t);
g2.fillRect(origin.x-25,origin.y-25,50,50);
g2.fillRect(origin.x+25,origin.y-5,50,10);
}
public void mouseMoved(MouseEvent e) {
turn=getAngle(new Point(e.getX(),e.getY()));
repaint();
}
public double getAngle(Point p) {
double rotate;
int quad = 0;
int width = 0, height = 0;
if(p.x==origin.x && p.y>origin.y) {
rotate = 90;
}else if(p.x==origin.x && p.y<origin.y) {
rotate = -90;
}else if(p.x>origin.x && p.y==origin.y) {
rotate = 0;
}else if(p.x<origin.x && p.y==origin.y) {
rotate = 180;
}else {
if(p.x<origin.x && p.y<origin.y) {
quad = 1;
width = origin.x-p.x;
height = origin.y-p.y;
}else if(p.x>origin.x && p.y<origin.y) {
quad = 2;
width = p.x-origin.x;
height = origin.y-p.y;
}else if(p.x<origin.x && p.y>origin.y) {
quad = 3;
width = origin.x-p.x;
height = p.y-origin.y;
}else if(p.x>origin.x && p.y>origin.y) {
quad = 4;
width = p.x-origin.x;
height = p.y-origin.y;
}
rotate = Math.atan(height/width);
if(quad==1) {
rotate -= (rotate*2) + 90;
}else if(quad==2) {
rotate -= (rotate*2);
}else if(quad==3) {
rotate += 90;
}else if(quad==4) {
}
}
turn = rotate;
return rotate;
}
Here's the code.