【问题标题】:How do I get a JavaFX camera orbit an object like a satelite?如何让 JavaFX 相机围绕卫星等物体运行?
【发布时间】:2020-03-23 19:54:44
【问题描述】:

在 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 是变换旋转。

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

【问题讨论】:

  • 这不是一个编码问题,而是更多关于翻译的数学问题。您可能会在这里得到更好的回应:math.stackexchange.com
  • 尝试将旋转的轴心设置为您旋转的对象的位置。

标签: java javafx camera javafx-3d


【解决方案1】:

我不明白您为什么要为任何数学而烦恼,因为所有这些都已经在 J​​avaFX 中完成了。不要使用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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2015-03-08
    • 2018-08-03
    • 1970-01-01
    • 2012-12-11
    相关资源
    最近更新 更多