gpt4 book ai didi

java - 如何在 JavaFX 3D 中制作第一人称相机?

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:58 24 4
gpt4 key购买 nike

所以我一直在尝试使用偏航和俯仰概念,并且尝试通过 x 和 y 的变化来调整角度。不幸的是,相机的角度有时会超出范围,我费心添加了角度测量的控件。我做错了什么?

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;

?
});
}
}

最佳答案

调用“setOnMouseMoved”时,您的 newX 和 newY 未初始化。

  1. oldX 和 oldY 将获得一些值,但不是实际的鼠标位置。
  2. newX 和 newY 获取的是屏幕坐标值,而不是场景坐标。

  3. 计算 deltaX 和 deltaY 时切换新旧

  4. 将翻译值乘以步长。步长越小,立方体移动越慢。

尝试这样的事情:

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

希望这对您有帮助。

关于java - 如何在 JavaFX 3D 中制作第一人称相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35021784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com