0

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

4

1 回答 1

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();
}

旋转盒 1

旋转盒 2

于 2017-05-01T08:48:44.180 回答