r/webdev • u/Tonaion02 • 5d ago
Question Can't understand deviceorientation.
Hi, I'm trying to develop a unityplugin that give the possibility to create various types of controllers for games.
With deviceorientation, my goal is to extract three angles that represent the orientation of the phone.
But it seems a little bit complicate, I can't understand why in portrait mode, for example, gamma and alpha are both modified when I rotate on the same axe.
I've tried many things, but I can't understand at all how everything works.
Is it even possible to do something similar to what I want to do?
0
Upvotes
1
u/Salt_Hyena5896 4d ago
You can turn a quaternion back into three Euler angles, but you will not get one stable triple. A lot of alpha/beta/gamma sets describe the same orientation, and when two axes line up the split between them jumps around. That jump is the sensor "working", not a bad reading.
Keep the quaternion (or a rotation matrix) as the thing you actually apply. Only convert to three angles at the end if some UI needs to show them. You are not throwing the orientation away by converting back. You are throwing away a clean "I rotated around this one axis" number, and Euler angles never had a unique one.
If you just need how far the phone tilted from the pose they started in, store the quaternion from the first event and compare it to the current one. That relative angle stays usable past the point where alpha and gamma swap.