How to rotate an object around a custom pivot in JavaFX?

自作多情 提交于 2020-01-03 03:39:05

问题


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 and camForm2 are the examples of XForm class.

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

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


回答1:


Just dealt with this problem myself.

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

An example:

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



回答2:


Create rotate object

Rotate x = new Rotate(1,2,7,134); 

Force custom pivot location with pivot property

x.pivotXProperty().set(2);
x.pivotYProperty().set(7);
x.pivotZProperty().set(134);

Add rotation axis

x.axisProperty().setValue(Rotate.Y_AXIS);

Use get transforms

meshViews[14].getTransforms().add(x);
meshViews[9].getTransforms().add(x);                      


来源:https://stackoverflow.com/questions/20457122/how-to-rotate-an-object-around-a-custom-pivot-in-javafx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!