I am making my own version of space invaders. I have a shooter at the bottom of a screen and enemies that are approaching from above. My shooter can move along the x-axis at the bottom of the screen perfectly fine. However I have to give it a turret that rotates about the shooter's center based on keys pressed.
Taking the x-axis as 0 degrees rotation(with a positive angle of rotation towards the positive y-axis):
The turret should start at the top of the shooter's head (i.e. 90 degrees). If I press A it should keep on rotating to the left(counterclockwise) and if I press D it should keep on rotating right(clockwise). If I press S it should stop rotating. A maximum rotation of 180 degrees is allowed (from positive x-axis to negative x-axis).
With the code I have thus far my turret starts at 0 degrees (pointing in the positive x-direction). If I press A it rotates to 90 degrees (vertical), if I press D it rotates all the way to -90 degrees (vertical downwards). If I press S it stops.
My question is where would I have to make my changes to correct this rotation?
This is how it should start, and it should be able to rotate all the way horizontal to the left and right and stop at either side:

Thank you for any help!
Edited code:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
public class Turret{
private int omega; //rotation speed
private int width = 16;
private int height = 12;
private int currAngle;
private Player playa;
public Turret(Player p){
omega = 0;
playa = p;
currAngle = 0;
}
public void keyHasBeenPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_A){
omega = 1; //rotate anti-clockwise
}
if(key == KeyEvent.VK_D){
omega = -1; //rotate clockwise
}
if(key == KeyEvent.VK_S){
omega = 0;
}
}
public void rotate(){
//angle of rotation is between 90 (negative x-axis) and -90 ( positive x-axis)
if(currAngle + omega < 90 && currAngle + omega > -90){
currAngle += omega;
}
}
public void show(Graphics2D g2d){
g2d.setColor(Color.GREEN);
AffineTransform old = g2d.getTransform();
g2d.translate(playa.getCenterXcoord(),playa.getCenterYcoord());
g2d.rotate(Math.toRadians(-currAngle));
g2d.translate(-playa.getCenterXcoord(), -playa.getCenterYcoord());
g2d.fillOval(playa.getCenterXcoord() - width/2,
playa.getYcoord() - height/2, width, height);
g2d.setTransform(old);
}
public int getAngle() { return currAngle; }
}
I got it fixed but some of the angles still don't make sense to me.