title: JavaFX Game Animation How to move ImageView in X-axis and Y-axis according to angle
body: <p>I want bullets to go straight to the target. I know how to change players direction according to the target location and I also know I need to decrees X-axis and Y-axis according to the angle but.. don't know how?? anyone has any idea how I can do it. Also, a target is gonna be moving down on x-axis I want target ship to go toward the player. Same problem. My Math is not that good I have no idea what kind of trigonometric approach I need to take.</p>
  
  <p>There are only 2 class files with these u can run code yourself</p>
  
  <p><a href="https://i.stack.imgur.com/xHYhi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xHYhi.png" alt="enter image description here"></a></p>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>    package bullitsrunning;
  
  import java.util.ArrayList;
  import javafx.animation.Animation;
  import javafx.animation.KeyFrame;
  import javafx.animation.Timeline;
  import javafx.application.Application;
  import javafx.event.EventHandler;
  import javafx.geometry.Point2D;
  import javafx.stage.Stage;
  import javafx.util.Duration;
  import javafx.scene.Scene;
  import javafx.scene.input.KeyEvent;
  import javafx.scene.layout.Pane;
  
  
  public class Main extends Application {
  
      private final int HEIGHT = 600, WIDTH = 600;
  
      public static Pane root  = new Pane();;
  
      ArrayList&lt;ImageObject&gt; rocket_list = new ArrayList&lt;&gt;();
      ArrayList&lt;ImageObject&gt; enemy_rocket_list = new ArrayList&lt;&gt;();
  
  
      boolean up = false,down = false,left = false,right = false, rotateLeft = false, rotateRight= false;
  
      ImageObject player = new ImageObject(300, 450, 100, 100, "alienship2.png");
  
      ImageObject enemy = new ImageObject(100, 10, 50, 50, "animemonster.png");
  
  
  
      @Override
      public void start(Stage stage) {
          try {
  
  
  
              Timeline tl = new Timeline(new KeyFrame(Duration.millis(5), e-&gt; {
  
  
                  playerUpdate();
  
                  enemyUpdate();
  
                  for(ImageObject rock : new ArrayList&lt;ImageObject&gt;(rocket_list)) {
  
  
  
                      rock.setLayoutY(rock.getLayoutY() - 2);     
  
  
  
  
                      if(rock.getLayoutY() &lt; 0) {  
  
                          if(rock.getBound().intersects(enemy.getBound())) 
                          {
                              //enemy.delete();
                          }
  
  
                          System.out.println(rocket_list.size());
                          rock.delete();  
                          rocket_list.remove(rock);
  
                      }
                  }
  
  
              }));
              tl.setCycleCount(Animation.INDEFINITE);
              tl.play();
  
  
  
  
  
              Scene scene = new Scene(root,600,600); 
  
              scene.setOnKeyPressed(new EventHandler&lt;KeyEvent&gt;() {
  
                  @Override
                  public void handle(KeyEvent e) 
                  {
  
                      switch (e.getCode()) {
                      case W:
                          up = true;
  
                          break;
                      case S:
  
                          down = true;
                          break;
                      case A:
  
                          left = true;
                          break;
                      case D:
  
                          right = true;
                          break;
  
                      case LEFT:
                          System.out.println("Rotate Left");
                          rotateLeft = true;
                          break;
  
                      case RIGHT:
                          System.out.println("Rotate Right");
                          rotateRight = true;
                          break;
  
  
                      case SPACE:
  
                          ImageObject io = new ImageObject(player.getX() + 38, player.getY(), 25, 25, "blackbullet.png"); 
                          rocket_list.add(io);
  
  
                          break;
  
  
                      }
                  }
              });
  
  
  
              scene.setOnKeyReleased(new EventHandler&lt;KeyEvent&gt;() {
  
                  @Override
                  public void handle(KeyEvent e) 
                  {
  
                      switch (e.getCode()) {
                      case W:
                          up = false;
  
                          break;
                      case S:
  
                          down = false;
                          break;
                      case A:
  
                          left = false;
                          break;
                      case D:
  
                          right = false;
                          break;
  
                      case LEFT:
                          System.out.println("Rotate Left");
                          rotateLeft = false;
                          break;
  
                      case RIGHT:
                          System.out.println("Rotate Right");
                          rotateRight = false;
                          break;
  
  
  
                      }
                  }
              });
  
  
  
              stage.setScene(scene);
  
              stage.show();
  
  
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  
  
  
  
      private void enemyUpdate() {
  
  
  
  
          for(ImageObject rock : new ArrayList&lt;ImageObject&gt;(enemy_rocket_list)) {
              rock.setLayoutY(rock.getLayoutY() + 2);     
              if(rock.getLayoutY() &gt; HEIGHT) {  
  
  
  
                  System.out.println(rocket_list.size());
                  rock.delete();  
                  rocket_list.remove(rock);
  
              }
          }
  
  
      }
  
  
  
  
  
  
  
  
      private void playerUpdate() {
  
  
  
  
          Point2D ene = new Point2D(enemy.getLayoutX(), enemy.getLayoutY());
          Point2D play = new Point2D(player.getLayoutX(), player.getLayoutY());
          Point2D p3 = play.subtract(ene);
          Point2D p4 = new Point2D(1, 0);
  
          System.out.println( "Point2D: " + p4.angle(p3));
  
          player.setRotate(p4.angle(p3) - 90);
  
          System.out.println(Math.sin(p4.angle(p3)));
  
  
  
  
  
  
  
  
  
  
  
  
  
          //So Player Can't move outside of the border
          if(player.getX() &lt; 0 ) {
              player.setLayoutX(0);
  
          } else if(player.getX() &gt; WIDTH- player.getWidth()) 
          {
              player.setLayoutX(WIDTH - player.getWidth());
          }else if(player.getY() &lt; 0 ) 
          {
              player.setLayoutY(0);
          }else if(player.getY() &gt; HEIGHT - player.getHeight()) 
          {
              player.setLayoutY(HEIGHT - player.getHeight());
          }else
          {
  
          //Player movement UP, DOWN, LEFT, RIGHT
              if(up) 
              {
                  player.setLayoutY(player.getLayoutY() - 1);
  
              }
              if(down) 
              {
                  player.setLayoutY(player.getLayoutY() + 1);
              }
              if(left)
              {
                  player.setLayoutX(player.getLayoutX() - 1);
              }
              if(right)
              {
                  player.setLayoutX(player.getLayoutX() + 1);
  
              }
  
              if(rotateLeft)
              {
  
                  player.setRotate(120 - 90);
  
  
              }
              if(rotateLeft)
              {
                  player.setRotate(60 - 90);
  
  
              }
  
  
  
  
          }
  
  
  
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p><strong>ImageObject.java</strong></p>
  
  <pre><code>package bullitsrunning;
  
  import javafx.geometry.Bounds;
  import javafx.scene.image.ImageView;
  
  public class ImageObject {
  
      private String folder_location ="File:Images/";
      private ImageView image_view;
      private boolean alive = true;
      public ImageObject(double x,double y,double w,double h,String image) 
      {
  
  
          image_view = new ImageView(folder_location+image);
          image_view.setCache(true); //help in performance
  
          image_view.setFitHeight(h );
          image_view.setFitWidth(w);
  
          image_view.setLayoutX(x);
          image_view.setLayoutY(y);
  
          image_view.setSmooth(true);
  
  
  
          Main.root.getChildren().add(image_view);
  
      }
  
      public double getX(){
  
          return image_view.getLayoutX();
      }
  
      public double getY() {
          return image_view.getLayoutY();
      }
  
  
  
      public void setRotate(double rot) {
          image_view.setRotate(rot);
      }
  
      public double getHeight() 
      {
          return image_view.getFitHeight();
      }
  
  
      public double getWidth() 
      {
          return image_view.getFitWidth();
      }
  
      public void setHeight(double h) 
      {
          image_view.setFitHeight(h);
  
      }
      public void setWidth(double w) 
      {
          image_view.setFitWidth(w);
      }
  
      public void setSize(double h,double w) 
      {
          image_view.setFitHeight(h);
          image_view.setFitWidth(w);
      }
  
      //Axis
      public double getLayoutX() 
      {
          return image_view.getLayoutX();
      }
      public double getLayoutY() 
      {
          return image_view.getLayoutY();
      }
  
      public void setLayoutX(double x) 
      {
  
          image_view.setLayoutX(x);
      }
  
      public void setLayoutY(double y) 
      {
          image_view.setLayoutY(y);
      }
  
      public void setPos(double x,double y) 
      {   image_view.setLayoutX(x);
          image_view.setLayoutY(y);
      }
  
  
      public Bounds getBound() 
      {
          return image_view.getBoundsInParent();
  
      }
  
      public ImageView getView() 
      {
          return image_view;
      }
  
      public void delete() 
      {
          Main.root.getChildren().remove(image_view);
      }
  
  
      public boolean isAlive() {
          return alive;
      }
  
  
      public void setAlive(boolean alive) {
          this.alive = alive;
      }
  
  
  }
  </code></pre>