2017-05-03 34 views
3

javaFXの3Dオブジェクトの周りを円でカメラを回転させるにはどうすればよいですか?私は私がjavaFXのオブジェクトの周りにパースペクティブカメラを回転する

camera.setRotate(angle); 

使用して自分自身の周りに回転させることができます知っているが、私はオブジェクトがまだになりたいとカメラが回転すると、回転軸のように同じ場所にポイントがそのオブジェクトです。

答えて

7

RotateTransition around a pivot?一般的なテクニックは、回転変換を定義し、次にタイムライン(またはアニメーションタイマー)を使用して回転変換の角度を適切にアニメートします。オブジェクトを中央揃えにしたい場合は、カメラを回転させる前にオブジェクトの原点に変換することができます。

サンプルはここだけの3Dアプリのためにこれを行う方法を示しています。カメラは、キューブの周りに回転しているサンプルで

image image

の中心は、シーンにあるコ縦軸は0,0,0です。アニメーション回転はy軸の周りにあります。サンプル画像には、さまざまな回転角度でスナップショットが表示されます。シーン内のオブジェクトをクリックすると、カメラをオブジェクトの中心に合わせ、その周りを回転することができます。

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

コードのおかげで、非常に役に立ちました! – tokyo

関連する問題