How would I make a first person camera in JavaFX 3

2019-08-13 12:14发布

问题:

So I've been trying to use the yaw and pitch concept, and I've tried adjusting the angle by the change in x and y. Unfortunately, the angle of the camera sometimes goes out of bounds, and I've bothered to add in controls for the angle measurements. What am I doing wrong?

public class Main extends Application {

    private Group group;
    private Scene scene;

    private double oldX;
    private double oldY;

    private double newX;
    private double newY;

    private double dx;
    private double dy;

    private Rotate yaw;
    private Rotate pitch;
    private double xSen = 800;
    private double ySen = 600;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        group = new Group();
        scene = new Scene(group, 800, 600);

        primaryStage.setScene(scene);
        primaryStage.show();

        yaw = new Rotate(0, Rotate.Y_AXIS);
        pitch = new Rotate(0, Rotate.X_AXIS);

        Box box = new Box(5, 5, 5);
        box.setMaterial(new PhongMaterial(Color.BLUE));
        box.setTranslateZ(40);

        group.getChildren().add(box);

        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.getTransforms().addAll(yaw, pitch);
        scene.setCamera(camera);

        xSen = 1;
        ySen = 1;
        scene.setOnKeyPressed(event -> {
            switch (event.getCode()) {
                case A:
                    xSen -= 1;
                    break;
                case D:
                    xSen += 1;
                    break;
                default:
                    break;
            }

            System.out.println(xSen + " " + ySen);
        });

        scene.setOnMouseMoved(event -> {
            oldX = newX;
            oldY = newY;
            newX = event.getX();
            newY = event.getY();
            dx = oldX - newX;
            dy = oldY - newY;

            ?
        });
    }
}

回答1:

your newX and newY is not initialized when "setOnMouseMoved" is called.

  1. oldX and oldY will get some value but not the actual mouseposition.
  2. newX and newY get the values of screen coordinates not the coordinates of the scene.

  3. switch new and old when calculating deltaX and deltaY

  4. multiply your translate value with a stepsize. the lower the stepsize the slower the cube moves.

Try something like this:

    boolean pressed = false;  

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

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

             camera.setTranslateX(camera.getTranslateX() - dx*0.1);
             camera.setTranslateY(camera.getTranslateY() - dy*0.1);      
        }           
    });

Hope this helped You.