To observe the problem in action with an Android device you may visit HERE and scroll down to "Show orientation angles". Once you turn it on and start getting live numbers, try holding your device as « upright » as you can, then slightly tilt it left or right. To make it more tangible scroll further down and find "Turn the Gyro-Cube ON". Finally view the behavior of the cube while doing the same upright positioning and the same leaning.
NOTE: You must view the page with https and NOT http
DISCLAIMER: We are not affiliated with any of the parties found on the Gyro-Cube demo page.
The [now-solved] problem starts with...
window.addEventListener("deviceorientation", handleDeviceTilt);
function handleDeviceTilt(event){
// Here we can use event.beta, event.gamma
// Note that event.alpha is just the compass
// alpha values are w.r.t. Earth's magnetic poles in the real world
// In theory, we don't need to know which way is north to build a simple bubble-level app
// or a simple driving simulator.
}
...where beta
and gamma
values are nice and usable as long as the phone or tablet is held parallel to the ground (like resting on a table). But when the phone or tablet is held UPRIGHT (like parallel to a wall) we can't get realistic values. gamma
gets more and more crazy as the device approaches the perfect vertical position (aligned with Earth's gravity) which is the most common way of holding a mobile device (i.e. portrait mode).
The question is,
What would be the simplest way to calculate the correct angles like
realBeta
andrealGamma
(perhaps by usingMath.cos()
andMath.sin()
or by some other method) in order to unlock the so called gimbal lock? Given that the "distortedness" or the "inaccuracy" in gamma is proportional in some way to beta, there must exist a solution but how exactly do we getrealGamma
oractualGamma
orfixedCorrectGamma
?
Note 1: There are a few spirit level apps on PlayStore which suggest that it is doable. However as of 2021 there seems to be no open source code available for such an app written in javascript.
Note 2: This issue has been mentioned here.
Note 3: Precision is necessary but acceptable approximations are also welcome. However, the closer we get to exact angles the better. If necessary alpha
may be omitted for simplicity.
WHAT IS THIS USEFUL FOR?
By solving this problem we would be able to use deviceorientation
to make something similar to this,
...or for example we could use gamma (or some kind of similar processed rotation data) to steer a car in a vertical racing game.
If the link HERE does not work please search and find any page that features some kind of live demo where it will let you see what numbers are reported by your mobile device. Watch γ (gamma
) values carefully as beta
approaches 90deg.
Final thought,
We may need some arcane math-magics.