Reputation: 69
I was able to achieve a 2D rotation on X-Axis using x and y values but would like to compute Y-Axis and Z-Axis rotation angles.
Below is the formula that I have used for computing xAngle rotaion.
var xAngle = Math.atan2(x, y)/(Math.PI/180)
where x and y are the gravitational values on x and y-axis provided by the gyroscope sensor respectively.
Can someone help me by sharing a similar set of formulae for yAngle and zAngle computations?
Thanks in Advance!
Upvotes: 1
Views: 1838
Reputation: 1114
Having just done the maths on this myself, there are some caveats you might want to be aware of.
Lets start by taking a quick look at the axis and using the correct names. Pitch, roll, and yaw describe the rotation around each of the axis. See https://sidvind.com/wiki/Yaw,_pitch,_roll_camera for more information.
Also, everything is relative. The sensors might be relative to one way of thinking, whilst the desired way of thinking might be relative to another.
In my case, the relative aspect is the tablet in landscape (landscape-left: rotated 90° counter-clockwise) then laid flat on its back (upright has a pitch of 90°).
The accelerometer readings (that I use) are relative to portrait upright.
Relative to gravity, the rotation around the y-axis can't be calculated from accelerometer information. This means:
pitch = Math.atan2(-x, -z) * 180 / Math.PI;// In degrees
roll = Math.atan2(-y, -x) * 180 / Math.PI;// In degrees
yaw = Math.atan2(y, -z) * 180 / Math.PI;// In degrees
pitch = Math.atan2(z, -y) * 180 / Math.PI;// In degrees
roll = Math.atan2(x, -y) * 180 / Math.PI;// In degrees
yaw = Math.atan2(-x, -z) * 180 / Math.PI;// In degrees
Your x-axis rotation matches the pitch on neither of these (nor gets close, negating the symbols), which suggests you're not calculating the rotation around the x-axis, but rotation of the x-axis around another, most likely the z-axis rotation (roll).
Upvotes: 1