问题
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.
回答1:
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);
}
}
来源:https://stackoverflow.com/questions/43763551/rotating-perspective-camera-around-an-object-in-javafx