在javaFX中围绕对象旋转透视相机 [英] rotating perspective camera around an object in javaFX

查看:29
本文介绍了在javaFX中围绕对象旋转透视相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让相机围绕 javaFX 中的 3d 对象旋转?我知道我可以使用

围绕它自己旋转

camera.setRotate(angle);

但我希望一个物体保持静止,相机旋转并指向同一个点,就像旋转轴就是那个物体一样.

解决方案

一般技术定义了以下答案:

在示例中,相机围绕立方体旋转,立方体的中心位于场景坐标 0,0,0 处.动画旋转围绕 y 轴.示例图像显示了不同旋转角度的快照.您可以点击场景中的一个对象,使相机以该对象为中心并围绕它旋转.

import javafx.animation.*;导入 javafx.application.Application;导入 javafx.scene.*;导入 javafx.scene.paint.*;导入 javafx.scene.shape.*;导入 javafx.scene.transform.*;导入 javafx.stage.Stage;导入 javafx.util.Duration;公共类 CameraRotationApp 扩展应用程序 {私有父 createContent() 抛出异常 {球体球体 = 新球体(2.5);sphere.setMaterial(new PhongMaterial(Color.FORESTGREEN));sphere.setTranslateZ(7);sphere.setTranslateX(2);Box box = new Box(5, 5, 5);box.setMaterial(new PhongMaterial(Color.RED));翻译枢轴 = 新翻译();旋转 yRotate = new Rotate(0, Rotate.Y_AXIS);//创建并定位相机PerspectiveCamera 相机 = new PerspectiveCamera(true);camera.getTransforms().addAll (枢,y旋转,新旋转(-20,旋转.X_AXIS),新翻译(0, 0, -50));//动画相机位置.时间线时间线 = 新时间线(新的关键帧(持续时间.秒(0),新键值(yRotate.angleProperty(),0)),新的关键帧(持续时间.秒(15),新键值(yRotate.angleProperty(),360)));timeline.setCycleCount(Timeline.INDEFINITE);时间线.播放();//构建场景图组根 = new Group();root.getChildren().add(camera);root.getChildren().add(box);root.getChildren().add(sphere);//根据鼠标点击对象设置相机位置动画的枢轴root.getChildren().stream().filter(node -> !(node instanceof Camera)).forEach(节点 ->node.setOnMouseClicked(事件 -> {pivot.setX(node.getTranslateX());pivot.setY(node.getTranslateY());pivot.setZ(node.getTranslateZ());}));//使用子场景SubScene subScene = new SubScene(根,300,300,真的,SceneAntialiasing.BALANCED);subScene.setFill(Color.ALICEBLUE);subScene.setCamera(相机);组组=新组();group.getChildren().add(subScene);返回组;}@覆盖公共无效开始(阶段阶段)抛出异常{stage.setResizable(false);场景场景 = 新场景(createContent());stage.setScene(场景);舞台表演();}公共静态无效主(字符串 [] args){发射(参数);}}

How can I make camera rotate in circle around a 3d object in javaFX? I know i can rotate it around itself using

camera.setRotate(angle);

but i want an object to be still and the camera to rotate and point to the same spot like the rotation axis is that object.

解决方案

The general technique is defined the answers to: RotateTransition around a pivot? You define a rotate transform and then use a timeline (or animation timer) to animate the angle of the rotate transform as appropriate. If you want the object centered, then you can translate the camera to the origin of the object before rotating.

The sample here just demonstrates how to do this for a 3D app:

In the sample the camera is rotating around the cube, the center of which is at the scene co-ordinates 0,0,0. The animated rotation is around the y-axis. The sample images show snapshots at various degrees of rotation. You can click on an object in the scene to center the camera on the object and rotate around it.

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.*;
import javafx.stage.Stage;
import javafx.util.Duration;

public class CameraRotationApp extends Application {

    private Parent createContent() throws Exception {
        Sphere sphere = new Sphere(2.5);
        sphere.setMaterial(new PhongMaterial(Color.FORESTGREEN));

        sphere.setTranslateZ(7);
        sphere.setTranslateX(2);

        Box box = new Box(5, 5, 5);
        box.setMaterial(new PhongMaterial(Color.RED));

        Translate pivot = new Translate();
        Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);

        // Create and position camera
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.getTransforms().addAll (
                pivot,
                yRotate,
                new Rotate(-20, Rotate.X_AXIS),
                new Translate(0, 0, -50)
        );

        // animate the camera position.
        Timeline timeline = new Timeline(
                new KeyFrame(
                        Duration.seconds(0), 
                        new KeyValue(yRotate.angleProperty(), 0)
                ),
                new KeyFrame(
                        Duration.seconds(15), 
                        new KeyValue(yRotate.angleProperty(), 360)
                )
        );
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();

        // Build the Scene Graph
        Group root = new Group();       
        root.getChildren().add(camera);
        root.getChildren().add(box);
        root.getChildren().add(sphere);

        // set the pivot for the camera position animation base upon mouse clicks on objects
        root.getChildren().stream()
                .filter(node -> !(node instanceof Camera))
                .forEach(node ->
                        node.setOnMouseClicked(event -> {
                            pivot.setX(node.getTranslateX());
                            pivot.setY(node.getTranslateY());
                            pivot.setZ(node.getTranslateZ());
                        })
                );

        // Use a SubScene
        SubScene subScene = new SubScene(
                root,
                300,300,
                true,
                SceneAntialiasing.BALANCED
        );
        subScene.setFill(Color.ALICEBLUE);
        subScene.setCamera(camera);
        Group group = new Group();
        group.getChildren().add(subScene);

        return group;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setResizable(false);
        Scene scene = new Scene(createContent());
        stage.setScene(scene);
        stage.show();
    }

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

这篇关于在javaFX中围绕对象旋转透视相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆