Charles Brown
Charles Brown

Reputation: 927

How to get the angle from quartnenion value?

Using android phone, I am able to get quarternion value x,y,z from TYPE_ROTATION_VECTOR

However, I do not know how to get the angle.

Anyone have any idea on this? Thank you.

Upvotes: 0

Views: 204

Answers (1)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

Taken from here

    /**
     * Returns the heading, attitude and bank of this quaternion as euler angles in the double array respectively
     * 
     * @return An array of size 3 containing the euler angles for this quaternion
     */
    public double[] toEulerAngles() {
        double[] ret = new double[3];

        ret[0] = Math.atan2(2 * points[1] * getW() - 2 * points[0] * points[2], 1 - 2 * (points[1] * points[1]) - 2
                * (points[2] * points[2])); // atan2(2*qy*qw-2*qx*qz , 1 - 2*qy2 - 2*qz2)
        ret[1] = Math.asin(2 * points[0] * points[1] + 2 * points[2] * getW()); // asin(2*qx*qy + 2*qz*qw) 
        ret[2] = Math.atan2(2 * points[0] * getW() - 2 * points[1] * points[2], 1 - 2 * (points[0] * points[0]) - 2
                * (points[2] * points[2])); // atan2(2*qx*qw-2*qy*qz , 1 - 2*qx2 - 2*qz2)

        return ret;
    }

Upvotes: 1

Related Questions