I am making a game similar to Asteroids. The ship always points toward the mouse cursor. I want to know how to move towards the mouse so if I press w
I will drive towards the mouse and if I press s
I will move away from the mouse. How can I do this? Here is my code so far:
public class player {
public int x,y;
private Image img;
float xDistance;
float yDistance;
double rotationAngle;
public player(int x,int y){
this.x = x;
this.y = y;
}
public void tick(){
xDistance = Comp.mx - x;
yDistance = Comp.my - y;
rotationAngle = Math.toDegrees(Math.atan2(yDistance, xDistance));
handleinput();
}
public void render(Graphics g){
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g2.setRenderingHints(rh);
ImageIcon i2 = new ImageIcon("res/player.png");
img = i2.getImage();
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.rotate(Math.toRadians(rotationAngle) ,20,20);
g2.drawImage(img, at, null);
}
public void handleinput(){
if(Keys.isPressed(Keys.w)){
}
}
}