Skip to main content

Problem rotating platform with quaterions sometimes the scene mirrors

No replies
sunflower1
Offline
Joined: 2008-09-18
Points: 0

Hello,

I am using following code to rotate the viewing platform:
90% the time it works fine but sometimes it flips and the scene is mirrored but after continuing rotating it can get back to normal. Does anyone know how to prevent the mirroring?

...
    /**
     * @see java.awt.event.MouseAdapter...
     */
    @Override
    public void mouseDragged(MouseEvent e)
    {
            int dx, dy;
            int x = e.getX();
            int y = e.getY();

            dx = e.getX() - x_last;
            dy = e.getY() - y_last;

            x_angle += dy * y_factor;
            y_angle -= -dx * x_factor;

            TransformGroup transoformGroup               
             =simpleUniverse.getViewingPlatform().getViewPlatformTransform();
           
            transoformGroup.getTransform(currXform);

            Matrix3d matrix3d = new Matrix3d();
            currXform.get(matrix3d);

            currXform.set(createQuaternionFromEuler(x_angle, y_angle, 0));
            Vector3d vector3dTranslationCamera = new Vector3d(0, 0, -3);
           
            currXform.setTranslation(vector3dTranslationCamera);
            currXform.invert();
       
            transoformGroup.setTransform(currXform);

            x_last = x;
            y_last = y;

    }
...

public static Quat4f createQuaternionFromEuler(double angleX, double angleY, double angleZ)
    {
        // simply call createQuaternionFromAxisAndAngle for each axis
        // and multiply the results
        Quat4f qx = createQuaternionFromAxisAndAngle(new Vector3d(1, 0, 0), angleX);
        Quat4f qy = createQuaternionFromAxisAndAngle(new Vector3d(0, 1, 0), angleY);
        Quat4f qz = createQuaternionFromAxisAndAngle(new Vector3d(0, 0, 1), angleZ);

        // qx = qx * qy
        qx.mul(qy);

        // qx = qx * qz
        qx.mul(qz);

        return qx;

    }

...

public static Quat4f createQuaternionFromAxisAndAngle(Vector3d axis, double angle)
    {
        double sin_a = Math.sin(angle / 2);
        double cos_a = Math.cos(angle / 2);

        // use a vector so we can call normalize
        Vector4f q = new Vector4f();

        q.x = (float) (axis.x * sin_a);
        q.y = (float) (axis.y * sin_a);
        q.z = (float) (axis.z * sin_a);
        q.w = (float) cos_a;

        // It is best to normalize the quaternion
        // so that only rotation information is used
        q.normalize();

        // convert to a Quat4f and return
        return new Quat4f(q);

    }

...