I'm trying to use this library at https://learn.microsoft.com/en-us/xamarin/essentials/orientation-sensor to work out the devices pitch, yaw, and roll. The library works, and returns in a Quaternion.
I want to be able to convert this quaternion to a pitch, yaw, and roll in 360 degree space, and also a percentage from 0 to 100 of how far the device has been rotated "towards" 360.
I've been researching this and have found various mathematical equations, expressed in mathamatical notation, of how to achieve something like this. These documents also talk about things like euler angles, rads, and various other concepts that I don't have any concept of.
Is there a way in C# to convert from a Quaternion to both a) an angle in 360 degrees space, and b) a 0-100 percentage of how rotated the device is on a certain axis?
Thanks!
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.