JavaFx中3D旋转后的节点漂移

时间:2018-08-06 14:06:09

标签: java javafx 3d rotation javafx-3d

我从RFID标签获得x方向和y方向,并希望对JavaFX应用程序中的运动进行动画处理。这是我的第一个Java项目,如果出现愚蠢的错误,我感到抱歉。

我以thisthis螺纹的方式旋转箱形节点。 两张有关绿色节点如何旋转的图片,位于背景enter image description here的RFID阅读器图像的前面 xAxis =红色,yAxis =绿色,zAxis =蓝色 enter image description here

我这样称呼rotateNode方法:

// calculate necessary variables:
delta_x = -x_angle + x_angle_old;
delta_y = -y_angle + y_angle_old;

delta_x_radians = Math.toRadians(delta_x);
delta_y_radians = Math.toRadians(delta_y);

pitch_rad = delta_y_radians;
yaw_rad = 0d; // not used at the moment
roll_rad = delta_x_radians;

if (!((roll_rad == 0d) && (pitch_rad == 0d) && (yaw_rad == 0d))) {
    rotateNode(model3D, pitch_rad, yaw_rad, roll_rad);
}

我的Box节点在开始处的位置为(0,0,-200),对象的中心应该在空洞时间停留在该位置。只是两个方向上的方向应该改变。我的rotateNode方法看起来像这样:

public static void rotateNode(Group n, double pitch_rad, double yaw_rad, double roll_rad) {// , TranslateTransition
                                                                                            // tt_z) {
    double A11 = Math.cos(roll_rad) * Math.cos(yaw_rad);
    double A12 = Math.cos(pitch_rad) * Math.sin(roll_rad)
            + Math.cos(roll_rad) * Math.sin(pitch_rad) * Math.sin(yaw_rad);
    double A13 = Math.sin(roll_rad) * Math.sin(pitch_rad)
            - Math.cos(roll_rad) * Math.cos(pitch_rad) * Math.sin(yaw_rad);
    double A21 = -Math.cos(yaw_rad) * Math.sin(roll_rad);
    double A22 = Math.cos(roll_rad) * Math.cos(pitch_rad)
            - Math.sin(roll_rad) * Math.sin(pitch_rad) * Math.sin(yaw_rad);
    double A23 = Math.cos(roll_rad) * Math.sin(pitch_rad)
            + Math.cos(pitch_rad) * Math.sin(roll_rad) * Math.sin(yaw_rad);
    double A31 = Math.sin(yaw_rad);
    double A32 = -Math.cos(yaw_rad) * Math.sin(pitch_rad);
    double A33 = Math.cos(pitch_rad) * Math.cos(yaw_rad);

    double d = Math.acos((A11 + A22 + A33 - 1d) / 2d);
    if (d != 0d) {
        double den = 2d * Math.sin(d);
        if (den != 0d) {
            Point3D p = new Point3D((A32 - A23) / den, (A13 - A31) / den, (A21 - A12) / den);
            x_pos_node = (n.localToScene(n.getBoundsInLocal()).getMaxX()
                    + n.localToScene(n.getBoundsInLocal()).getMinX()) / 2d;
            y_pos_node = (n.localToScene(n.getBoundsInLocal()).getMaxY()
                    + n.localToScene(n.getBoundsInLocal()).getMinY()) / 2d;
            z_pos_node = (n.localToScene(n.getBoundsInLocal()).getMaxZ()
                    + n.localToScene(n.getBoundsInLocal()).getMinZ()) / 2d;
            r.setPivotX(x_pos_node);
            r.setPivotY(y_pos_node);
            r.setPivotZ(z_pos_node);
            r.setAxis(p);
            r.setAngle(Math.toDegrees(d));
            n.getTransforms().add(r);
            Transform all = n.getLocalToSceneTransform();
            n.getTransforms().clear();
            n.getTransforms().add(all);
        }
    }
}

打印以下变量显示该节点向y移动,尽管我不希望这种情况发生。我还看到,随着时间的流逝,旋转的枢轴点逐渐不再位于节点的中心,当我旋转RFID标签时,它不会围绕节点的中心旋转,而是旋转成一个圆圈,越来越大..

来自:

x_pos_node:     0,00
y_pos_node:     0,39
z_pos_node:  -200,00
MaxX:   199,00
MinX:  -199,00
MaxY:     2,78
MinY:    -2,00
MaxZ:  -176,12
MinZ:  -223,88
Depth:   47,76
Height:   4,78
Width:  398,00

收件人:

x_pos_node:     0,00
y_pos_node:    15,52
z_pos_node:  -200,00
MaxX:   198,51
MinX:  -198,51
MaxY:    38,35
MinY:    -7,31
MaxZ:  -130,85
MinZ:  -269,15
Depth:  138,30
Height:  45,67
Width:  397,02

从侧面显示绿色节点如何在蓝色z轴/零线下移动的图片: enter image description here
我的错误在哪里?为什么对象缓慢移动而不是旋转?

添加翻译后,可能会定位错误:

n.getTransforms().add(new Translate(0, -y_pos_node, 0)); 

但这只是一个修复程序,您可以看到对象如何上下移动。.我认为计算或枢轴点的位置存在错误。尽管将“ yaw_rad”设置为0,它也会绕绿色的y轴旋转一点;

0 个答案:

没有答案