如何让JavaFX相机像卫星一样环绕物体运行



在JavaFX中,我想添加一个像卫星一样围绕物体运行的相机。我希望轨道沿着围绕对象的水平和垂直线移动。我还希望相机始终指向矩阵中心的对象。

目前,我试图通过使用单位圆沿x和y轴移动相机。现在的代码是这样的:

<code>
int r = 10;
Slider nxSlider = new Slider(0, 360, 0);
nxSlider.valueProperty().addListener((observable, oldvalue, newvalue) ->
{
double i = newvalue.doubleValue();
camera.setTranslateX(r * Math.cos(Math.toRadians(i)));
camera.setTranslateY(r * Math.sin(Math.toRadians(i)));
rotateX.setAngle(Math.toDegrees(Math.cos(Math.toRadians(i))));
});
rotateZ.setAngle(0);
rotateY.setAngle(0);
rotateX.setAngle(0);
camera.setTranslateX(r);
camera.setTranslateZ(0);
camera.setTranslateY(0);
</code>

其中,作为rotateX,rotateY和rotateZ是变换旋转。

我觉得我很失落,我已经有这个问题很长时间了。我的代码可能是错误的,如果有人能提出我如何继续的想法,我将不胜感激。

我不明白你为什么要麻烦任何数学,因为所有这些都已经在JavaFX中完成了。不要使用Translate功能,而是使用Rotate。下面是一个用相机绕物体运行的非常简单的小例子:

import javafx.application.Application;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
public class TestCamera extends Application {
Group root = new Group();
Box box = new Box(100, 20, 50);
Camera camera = new PerspectiveCamera(true);
Rotate rotation;
@Override
public void start(Stage primaryStage) throws Exception {
box.setMaterial(new PhongMaterial(Color.BLUE));
root.getChildren().add(box);
Scene scene = new Scene(root, 800, 800, Color.BLACK);
// this "backs you off" from the origin by 1000
camera.setTranslateZ(-1000);
// this is the crucial part here, where you set the pivot point
// of your rotation, in this case 1000 "in front" of you
// (these are "local coordinates" to the camera)
// rotation = new Rotate(0, 0, 0, 1000, Rotate.Y_AXIS);
// while the above works, if you want to rotate around two
// axes at once, you can use do it like this
rotation = new Rotate(0, 0, 0, 1000, new Point3D(1, 1, 0);
camera.getTransforms().add(rotation);
camera.setNearClip(0.01);
camera.setFarClip(10000);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.show();
scene.setOnKeyPressed(event -> {
switch(event.getCode()) {
case LEFT:
rotation.setAngle(rotation.getAngle() - 10);
break;
case RIGHT:
rotation.setAngle(rotation.getAngle() + 10);
break;
}
});
}
public static void main(String[] args) {
launch();
}
}

相关内容

最新更新