How can I reliably convert device orientation into phone rotation angles?
Asked by
!keep talking
I'm building a Unity plugin that supports different game-controller styles using a phone's orientation. I want to extract three angles describing how the phone is rotated, but I'm confused by deviceorientation: in portrait mode, rotating around what seems like one physical axis changes both gamma and alpha. Is there a reliable way to interpret these readings across different screen orientations? I'd also like to convert the result to quaternions for processing and then, if needed, convert it back into three angles without losing information.
Answers
Convert the orientation to a quaternion as early as possible and perform the rotation math with quaternions instead of manipulating alpha, beta, and gamma directly. Also account for screen.orientation.angle or the equivalent screen rotation value, because deviceorientation is relative to the device and screen coordinate system. When sending the result to Unity, remember that browser coordinates and Unity use different axis conventions, so youโll need an axis conversion.
Reply to this answer
If browser support allows it, the Generic Sensor APIโs AbsoluteOrientationSensor is a cleaner option because it provides a quaternion directly. Otherwise, deviceorientation can still work, but youโll need to apply the screen-orientation correction and carefully map the browserโs right-handed coordinate system to Unityโs left-handed one.
Reply to this answer
A quaternion can be converted back to Euler angles, so you donโt permanently lose information by using one internally. However, the resulting three angles arenโt unique and may jump or become ambiguous near gimbal-lock positions. If the angles are only for display, conversion is fine; for gameplay and controller movement, keep the quaternion or use a rotation matrix internally.
Reply to this answer
The reported alpha, beta, and gamma values are Euler angles, not independent readings for three fixed world axes. Theyโre applied in a particular order, so rotating the device around one physical axis can change multiple values. This is expected behavior, and Euler angles can also run into gimbal lock.
!keep talking: So the values arenโt directly tied to fixed axes in the world? That explains why the readings change together when I rotate the phone.
Reply to this answer
Add your answer
Answers are reviewed before publication.
!keep talking: That makes sense. I can use the quaternion for the actual controller logic and only convert to three angles when I need to show or export them.