1

I wonder if there is a possibility to perform a perspective rotation on a square in JavaFX. When I normally rotate it on the x-axis I just get a compressed square, however I wanted to simulate a real-life rotation with perspective. The result should look something like this

What it should look like

Of course depending on the degree. Is there any way to achieve this, best case where it is possible to specify a certain degree.

Here is what the code for the compression looks like:

public class HelloWorld extends Application {

    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();

        Rectangle rectangle = new Rectangle();
        rectangle.setWidth(500);
        rectangle.setHeight(500);
        rectangle.setRotationAxis(Rotate.X_AXIS);
        rectangle.setRotate(50);

        Scene scene = new Scene(root, 1000, 1000);
        root.getChildren().addAll(rectangle);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
  • paste the code that you currently have used to perform the x-axis rotation that gives you a compressed square (meaning orthogonal projection) – Edd Mar 23 '17 at 13:11
  • 1
    Googling for "javafx perspective" gives me all sorts of hits that look relevant, including many here on SO. We expect you to expend at least a modicum of effort researching the question for yourself before you turn to us. – John Bollinger Mar 23 '17 at 13:12
  • Have you tried the `PerspectiveTransform` ? https://docs.oracle.com/javafx/2/api/javafx/scene/effect/PerspectiveTransform.html – john16384 Mar 23 '17 at 13:14
  • Don't worry, I did my research before and found the `PerspectiveTransform`, however as far as I know it only simulates a rotation, but I can't specify the degree value. So how am I supposed to know the coordinates of the corners for a 45 degree rotation? This is not what I need. – Caroline Hernandez Mar 23 '17 at 13:38
  • Related: [Flip a card animation](http://stackoverflow.com/questions/19896562/flip-a-card-animation). – jewelsea Mar 23 '17 at 22:17

2 Answers2

2

By default, a Scene uses a ParallelCamera, which does not define any perspective. Hence the coordinate mapping for x and y coordinates are effectively independent of the z coordinate.

In order to see a three-dimensional effect in the scene, specify a PerspectiveCamera:

public class HelloWorld extends Application {

    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();

        Rectangle rectangle = new Rectangle();
        rectangle.setWidth(500);
        rectangle.setHeight(500);
        rectangle.setRotationAxis(Rotate.X_AXIS);
        rectangle.setRotate(50);

        Scene scene = new Scene(root, 1000, 1000);
        scene.setCamera(new PerspectiveCamera());
        root.getChildren().addAll(rectangle);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

enter image description here

(As you can see, your angle is computed in the opposite direction to the angle you want.)

You can further configure properties of the PerspectiveCamera, such as the field of view, if needed. Refer to the documentation.

James_D
  • 201,275
  • 16
  • 291
  • 322
1

JavaFX supports real 3D so there is no need to mess around with this PerspectiveTransform which is just an Effect. You just have to set up a proper 3D Scene with a PerspectiveCamera and you can get exactly what you want.

mipa
  • 10,369
  • 2
  • 16
  • 35