如何通过边缘而不是中心旋转3D对象

时间:2017-05-01 05:23:26

标签: javafx 3d pivot javafx-3d

我正在试图弄清楚枢轴点在3D中是如何工作的。我正试图让3D盒子在盒子的边缘而不是在中心旋转。任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以将Rotate变换应用于Box,因为它允许设置角度,轴心点和旋转轴。请参阅javadoc

这将在框的中心应用转换:

Rotate rotate = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);

Box box = new Box(100, 100, 100);
box.getTransforms().add(rotate);

虽然这将适用于其中一个边缘:

Rotate rotate = new Rotate(0, -50, -50, 0, Rotate.Z_AXIS);

Box box = new Box(100, 100, 100);
box.getTransforms().add(rotate);

这是一个快速测试,包含两种情况的动画:

private long time;
private final IntegerProperty counter = new SimpleIntegerProperty();

@Override
public void start(Stage primaryStage) {
    Rotate rotate1 = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
    rotate1.angleProperty().bind(counter);

    Box box1 = new Box(100, 100, 100);
    box1.setMaterial(new PhongMaterial(Color.LIMEGREEN));
    box1.getTransforms().add(rotate1);

    Cylinder axis1 =  new Cylinder(2, 200);
    axis1.setRotationAxis(Rotate.X_AXIS);
    axis1.setRotate(90);
    axis1.setMaterial(new PhongMaterial(Color.RED));

    Group group1 = new Group(box1, axis1);
    group1.setTranslateX(-100);

    Rotate rotate2 = new Rotate(0, -50, -50, 0, Rotate.Z_AXIS);
    rotate2.angleProperty().bind(counter);

    Box box2 = new Box(100, 100, 100);
    box2.setMaterial(new PhongMaterial(Color.LAVENDER));
    box2.getTransforms().add(rotate2);

    Cylinder axis2 =  new Cylinder(2, 200);
    axis2.setRotationAxis(Rotate.X_AXIS);
    axis2.setRotate(90);
    axis2.setTranslateX(-50);
    axis2.setTranslateY(-50);
    axis2.setMaterial(new PhongMaterial(Color.RED));

    Group group2 = new Group(box2, axis2);
    group2.setTranslateX(200);

    Group root = new Group(group1, group2);

    SubScene subScene = new SubScene(root, 600, 400, true, SceneAntialiasing.BALANCED);
    PerspectiveCamera camera = new PerspectiveCamera();
    camera.setTranslateX(-200);
    camera.setTranslateY(-200);
    camera.setTranslateZ(-100);
    camera.setRotationAxis(new Point3D(0, 0.5, 0.5));
    camera.setRotate(10);
    subScene.setCamera(camera);
    Scene scene = new Scene(new StackPane(subScene), 600, 400, true, SceneAntialiasing.BALANCED);

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

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            if (now - time > 30_000_000) {
                counter.set((counter.get() + 1) % 360);
                time = now;
            }
        }
    };
    timer.start();
}

rotating boxes 1

rotating boxes 2