Search code examples
javafxjavafx-8game-physics

JavaFX Game Animation How to move ImageView in X-axis and Y-axis according to angle


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.

There are only 2 class files with these u can run code yourself

enter image description here

Main.java

    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<ImageObject> rocket_list = new ArrayList<>();
    ArrayList<ImageObject> enemy_rocket_list = new ArrayList<>();


    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-> {


                playerUpdate();

                enemyUpdate();

                for(ImageObject rock : new ArrayList<ImageObject>(rocket_list)) {



                    rock.setLayoutY(rock.getLayoutY() - 2);     




                    if(rock.getLayoutY() < 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<KeyEvent>() {

                @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<KeyEvent>() {

                @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<ImageObject>(enemy_rocket_list)) {
            rock.setLayoutY(rock.getLayoutY() + 2);     
            if(rock.getLayoutY() > 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() < 0 ) {
            player.setLayoutX(0);

        } else if(player.getX() > WIDTH- player.getWidth()) 
        {
            player.setLayoutX(WIDTH - player.getWidth());
        }else if(player.getY() < 0 ) 
        {
            player.setLayoutY(0);
        }else if(player.getY() > 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);
    }
}

ImageObject.java

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;
    }


}

Solution

  • I don't know JavaFX and I don't go into your implementation details. But the core idea is as follows:

    You need to create a direction vector from the source location (current player location) to the destination (the enemy, I guess) as:

    direction   = new Vector()
    direction.x = des.x-src.x;
    direction.y = des.y-src.y;
    

    Now you should normalize the vector like:

    double direction_magnitude = Math.sqrt(direction.x*direction.x + direction.y*direction.y)
    direction.x = direction.x/direction_magnitude;
    direction.y = direction.y/direction_magnitude;
    

    Now just update the player location gradually according to the direction vector like:

    void update(float deltaTime) {
      // recalculate `direction` if your destination is moving
      player.x = player.x + (direction.x * deltaTime);
      player.y = player.y + (direction.y * deltaTime);
      player.rotation = Math.atan2(direction.y, direction.x)
    }
    

    The very last line rotates the player facing toward the destination (learn more about this line here).