javajavafx3djavafx-3d

Moving in 3D Space using mouse along with keyboard [JavaFX3D]


Introduction:

First of all, i should say that i am quite a newbie when it comes to 3D Graphics in programming, generally.. so i would need some more explenation on how things work if possible [...]

Problem:

My Problem is, that i don't know How using the mouse along with keyboard to move is possible, because when i move as seen in the Image below, i am always Stuck in The center

Goal:

My goal is to make a 3D first person camera, that is capable of moving inside a 3D space, filled of components like buttons, Circles or etc., such as i can navigate with my mouse and keyboard along XYZ axis.

Progress:

I have found some similar links that helped me like this one! but still i can't grasp the idea of how i can move my mouse and walk towords a specific point (not that i dont get the idea of how 3D projection in 2Dimentions works, but just how using the mouse along with keyboard to move). At this moment, i am stuck trying random things:

Stuck in The Center

enter image description here

Button NewButton1 = new Button();

NewButton1.setId("Button1");
NewButton1.setText("test");
NewButton1.setPrefWidth(150);
NewButton1.setPrefHeight(50);
NewButton1.setTranslateX(-140);
NewButton1.setTranslateY(0);
NewButton1.setTranslateZ(0);


PerspectiveCamera camera = new PerspectiveCamera(true);  

camera.setFarClip(9000);
camera.setTranslateX(0);  
camera.setTranslateY(0);  
camera.setTranslateZ(-10);  

//setting group and stage   
Group SubRootGroup = new Group();  
SubRootGroup.getChildren().addAll(NewButton1);  


SubScene1 = new SubScene(SubRootGroup, 0, 0, true, SceneAntialiasing.BALANCED);
SubScene1.setFill(Color.GRAY);
SubScene1.heightProperty().bind(TabPane1.heightProperty());
SubScene1.widthProperty().bind(TabPane1.widthProperty());
SubScene1.setCamera(camera); 


TabPane1.getTabs().get(0).setContent(SubScene1);

 TabPane1.setOnKeyPressed(e -> { switch (e.getCode()) { case W:
    SubRootGroup.setTranslateZ(SubRootGroup.getTranslateZ() + 10); break; case S:
    SubRootGroup.setTranslateZ(SubRootGroup.getTranslateZ() - 10); break; case A:
    SubRootGroup.setTranslateX(SubRootGroup.getTranslateX() - 10); break; case D:
    SubRootGroup.setTranslateX(SubRootGroup.getTranslateX() + 10); break; case Q:
    SubRootGroup.setTranslateY(SubRootGroup.getTranslateY() + 10); break; case E:
    SubRootGroup.setTranslateY(SubRootGroup.getTranslateY() - 10); break;
    }});

    SubScene1.setOnMousePressed((MouseEvent e) -> {
         pressed = true;
         newX = e.getSceneX();
         newY = e.getSceneY();
    });

    SubScene1.setOnMouseMoved((MouseEvent e) -> {
        if(pressed){
             oldX = newX;
             oldY = newY;
             newX = e.getSceneX();
             newY = e.getSceneY();
             dx = newX -oldX;
             dy = newY -oldY;

             //SubRootGroup.getRotate().add(new Rotate(45));
             SubRootGroup.setTranslateX(SubRootGroup.getTranslateX() + dx*2);// * sensitivity
             SubRootGroup.setTranslateY(SubRootGroup.getTranslateY() + dy*2);      
        }           
    });

Thanks in advance for any reply, Any help would be greatly appreciated,

George.

PS. I am New to javafx too..


Solution

  • Intro:

    First of all i want to thanks Everyone for helping me out and especially @Birdasaur for bringing to the surface the SimpleFPSCamera class [...]

    The Answer That Worked For My Problem :

    Step 1 : Add those 2 classes to your project :

    (of course change package name ..)

    Step 2: SetUp & Edit "SimpleFPSCamera.java" As Seen In The Image Above by Red & Green :

    Edit SimpleFPSCamera.java

    Step 3: It's Done! Just Run It:

    enter image description here

    And Again, Thanks Everyone for helping me out (:

    Update:

    Answered - JavaFX: SubScene won't focus inside TabPane when clicked?