Vincent
Vincent

Reputation: 141

How can I get horizontal rotation angle whatever device orientation?

I can get horizontal rotation angle by calculating the roll value (according to the definition of iOS Device Motion) when the device is portrait.

The x, y, z-axis of the mobile device: ios rotation frame image

But when the device is in landscape, y-axis is horizontal and x-axis is vertical. How can I get the angle? The pitch value is not correct. I have tried to exchange x and y in quaternion but not worked.

And more, how can I get the angle when the device is in the middle of portrait and landscape, for example, you tilt the device 30 degrees about z-axis?

Is there a unified quaternion or rotational matrix to calculate the angle whatever device orientation?

Upvotes: 0

Views: 1545

Answers (1)

minorlogic
minorlogic

Reputation: 1908

Probably you want to get angle of some device "direction". For example if you want to get angle of Z axis projected on ground, do it exactly.

  1. Transform the Z(0,0,1) vector into current device rotation.
  2. You have direction of "ZinWorld" vector in some world frame.
  3. According to world frame orientation , project "ZinWorld" into ground plane. For example , if in default world frame Z is pointing to UP , then just take X Y components of "ZinWorld".
  4. Take atan2(X, Y) to get proper angle.

Upvotes: 0

Related Questions