How can I convert a Quaternion to an angle?

后端 未结 1 648
星月不相逢
星月不相逢 2021-01-25 05:22

I\'m trying to use this library at https://docs.microsoft.com/en-us/xamarin/essentials/orientation-sensor to work out the devices pitch, yaw, and roll. The library works, and r

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-25 06:06

    According to the document:

    A Quaternion value is very closely related to rotation around an axis. If an axis of rotation is the normalized vector (ax, ay, az), and the rotation angle is Θ, then the (X, Y, Z, W) components of the quaternion are:

    (ax·sin(Θ/2), ay·sin(Θ/2), az·sin(Θ/2), cos(Θ/2))

    First, get Θ from W:

    var Θ = Math.Acos(W)*2;
    

    Then, get ax,ay,az by:

    var ax = X / Math.Sin(Math.Acos(Θ));
    var ay = Y / Math.Sin(Math.Acos(Θ));
    var az = Z / Math.Sin(Math.Acos(Θ));
    

    The Axis Angle of the Quaternion is [ax, ay, az].

    For example, a Quaternion of [0,0,0.7071068,0.7071068] will have an Axis-Angle of [0,0,1]. You could consider it rotated 90 degrees on the Z axis.

    0 讨论(0)
提交回复
热议问题