4

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

Giorgos Xou
  • 1,461
  • 1
  • 13
  • 32
  • George your question is too broad can you [edit](https://stackoverflow.com/posts/57188752/edit) the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the [How to Ask page](https://stackoverflow.com/help/how-to-ask) for help clarifying this question. Is there a specific error you are getting? Unrelated give java naming conventions a quick google it helps everyone when we all use the same style for naming variables. – Matt Jul 24 '19 at 19:19
  • This "how 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." sounds like you need to find a tutorial and not like you are running into a specific issue – Matt Jul 24 '19 at 19:21
  • Last thing I'm going to say here is instead of setting a variable on pressed and unsetting on released you can check if its pressed from the event thats being passed like so `if(e.isPrimaryButtonDown()){` instead of `pressed = true; if(pressed){...} pressed = false;` less variables to keep track of – Matt Jul 24 '19 at 19:27
  • @Matt Thank you for specifying that it was broad, because I would have had that wrong until now if noone had told me that (: , (for now i splited the before-problem into Goal and Problem) – Giorgos Xou Jul 25 '19 at 06:25
  • "sounds like you need to find a tutorial and not like you are running into a specific issue" kinda yes :/ , but i am ok with any help :D – Giorgos Xou Jul 25 '19 at 06:26
  • "... less variables to keep track of ...", True , Thanks :D ! – Giorgos Xou Jul 25 '19 at 06:28

2 Answers2

1

Fxyz3D has a SimpleFPSCamera class which sounds exactly what you need to get started. Take a look.

Birdasaur
  • 736
  • 7
  • 10
  • thanks you for replying (: ... i 've seen it before and tried it (i just forgot to mention it), it would be ideal if i could find a way to make it work , because it seems like it doesn't work for me, lol... it might be because of my poor knowledge in java generally as i am new to this language and i might not know how to use it correctly. But – Giorgos Xou Jul 25 '19 at 17:25
  • adding this class and its subclasses to my project seems to compile fine without any error but it won't have any impact on movement or anything, like it is not working – Giorgos Xou Jul 25 '19 at 17:25
  • ```java same code ... TabPane1.getTabs().get(0).setContent(SubScene1); SimpleFPSCamera SimpleFPSCam = new SimpleFPSCamera(); SimpleFPSCam.loadControlsForSubScene(SubScene1); /* TabPane1.setOnKeyPressed(e -> { switch (e.getCode()) { case W: ... */ ``` – Giorgos Xou Jul 25 '19 at 17:26
  • 1
    Found What Was The Problem and Now It Works!!! (i will mark it as an answer and i will update the post such as people know what changes to make so they can use it, thanks you for your help and for bringing to the surface the SimpleFPSCamera class, because without you posting it, i might never thought about trying to find what the problem was and use it again after so many hours trying before [...]) Thanks you (: ! – Giorgos Xou Jul 27 '19 at 09:56
  • Oh well. Your follow up answer that captures the details is better than my posted answer anyway! Nice work. – Birdasaur Jul 29 '19 at 14:03
1

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?

Community
  • 1
  • 1
Giorgos Xou
  • 1,461
  • 1
  • 13
  • 32