所以我一直在尝试使用偏航和俯仰概念,我尝试通过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;
?
});
}
}
答案 0 :(得分:0)
当" setOnMouseMoved"时,你的newX和newY没有被初始化。被称为。
newX和newY获取屏幕坐标值而不是场景坐标。
在计算deltaX和deltaY
尝试这样的事情:
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);
}
});
希望这有助于你。