如何围绕JavaFX中的自定义枢轴旋转对象? [英] How to rotate an object around a custom pivot in JavaFX?

查看:354
本文介绍了如何围绕JavaFX中的自定义枢轴旋转对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想围绕自定义轴旋转对象,这就是它的重点,所以我有这样的代码:

I want to rotate an object round a custom pivot, which is its point, so I have such code:

private final EventHandler<MouseEvent> mouseEventHandler = new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
                dragStartX = mouseEvent.getSceneX();
                dragStartY = mouseEvent.getSceneY();
                mousePosX = mouseEvent.getSceneX();
                mousePosY = mouseEvent.getSceneY();
                mouseOldX = mouseEvent.getSceneX();
                mouseOldY = mouseEvent.getSceneY();

                if (mouseEvent.isMiddleButtonDown()) {
                    pivot = mouseEvent.getPickResult().getIntersectedPoint();
                    camForm1.rx.setPivotX(pivot.getX());
                    camForm1.ry.setPivotY(pivot.getY());
                    camForm1.rz.setPivotZ(pivot.getZ());
                    System.out.println(pivot);
                }

            } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {

                double modifier = 1.0;
                double modifierFactor = 0.3;

                if (mouseEvent.isControlDown()) {
                    modifier = 0.1;
                }
                if (mouseEvent.isShiftDown()) {
                    modifier = 10.0;
                }

                mouseOldX = mousePosX;
                mouseOldY = mousePosY;
                mousePosX = mouseEvent.getSceneX();
                mousePosY = mouseEvent.getSceneY();
                mouseDeltaX = (mousePosX - mouseOldX); //*DELTA_MULTIPLIER;
                mouseDeltaY = (mousePosY - mouseOldY); //*DELTA_MULTIPLIER;

                double flip = -1.0;

                if (mouseEvent.isPrimaryButtonDown() && mouseEvent.isSecondaryButtonDown()) {
                    camForm2.t.setX(camForm2.t.getX() + flip * mouseDeltaX * modifierFactor * modifier * 0.3);  // -
                    camForm2.t.setY(camForm2.t.getY() + mouseDeltaY * modifierFactor * modifier * 0.3);  // -  yFlip*
                } else if (mouseEvent.isSecondaryButtonDown()) {
                    camForm1.ry.setAngle(camForm1.ry.getAngle() - mouseDeltaX * modifierFactor * modifier * 2.0);  // + yFlip*
                    camForm1.rx.setAngle(camForm1.rx.getAngle() + flip * mouseDeltaY * modifierFactor * modifier * 2.0);  // -

                }
            }
        }
    };

camForm1 camForm2 XForm 类的示例。

    public class XForm extends Group {

        public enum RotateOrder {
            XYZ, XZY, YXZ, YZX, ZXY, ZYX
        }

        public Translate t  = new Translate();
        public Translate p  = new Translate();
        public Translate ip = new Translate();
        public Rotate rx = new Rotate(0.0, 0.0, 0.0, 0.0, Rotate.X_AXIS);
        public Rotate ry = new Rotate(0.0, 0.0, 0.0, 0.0, Rotate.Y_AXIS);
        public Rotate rz = new Rotate(0.0, 0.0, 0.0, 0.0, Rotate.Z_AXIS);
        public Scale s = new Scale();

        public XForm() {
            super();
            getTransforms().addAll(t, rz, ry, rx, s);
        }
...
}

绕点O(0,0,0)。我在做什么错了?

But the rotation is around point O(0, 0, 0). What am I doing wrong?

推荐答案

我自己已经解决了这个问题。

Just dealt with this problem myself.

我不太了解您的代码,但是旋转class 允许设置自定义枢轴点。

I don't quite understand your code, but the Rotate class allows setting a custom pivot point.

示例:

Box box = new Box(1, 1, 1); // can be any Node
box.getTransforms().add(new Rotate(angle, pivotX, pivotY, pivotZ, Rotate.Z_AXIS));

这篇关于如何围绕JavaFX中的自定义枢轴旋转对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆