Fix for accelerometer reading in libgdx
I just got around fixing the longstanding issue of those funky new tablet devices with HC (and one Motorola Phone) that have a landscape native orientation. Here’s an image illustrating this super awesome situation (taken from NVIDIA’s accelerometer whitepaper):
As you can see, on phones, the native orientation is portrait. That means
- The accelerometer x-axis coincides with the smaller side of the screen
- The accelerometer y-axis coincides with the bigger side of the screen
- The accelerometer z-axis comes out of the screen
On shiny new tablets like the Transformer the native orientation is landscape. That means:
- The accelerometer x-axis coincides with the bigger side of the screen
- The accelerometer y-axis coincides with the smaller side of the screen
- The accelerometer z-axis comes out of the screen
Which is totally fucking awesome. Thankfully the Android API does not provide a direct method to detect the native orientation. With a bit of creativity (Display metrics + Display.getOrientation()/getRotation()) one can however reliably determine the native orientation.
I did just that, and we are now “correcting” the accelerometer readings for your convenience. No matter the device, the y-axis will always coincide with the bigger side of the screen, the x-axis will always coincide with the smaller side of the screen. Just as it was on phones for the last 3 years.
In addition to that i added two new methods to the Input interface:
/** * @return the rotation of the device with respect to its native orientation. */ public int getRotation(); /** * @return the native orientation of the device. */ public Orientation getNativeOrientation(); public enum Orientation { Landscape, Portrait } |
The getRotation() method returns an angle in degrees (0, 90, 180, 270), indicating the rotation relative to the default orientation of the device. The getNativeOrientation() method returns, well, the native orientation.
If you “fixed” the above “issue” yourself so far, make sure you revert that when updating to the latest nightlies! All other methods to calculate the orientation on the three axes in angles and so on work as expected, with the same reference system on all devices.
edit: on the desktop these methods return 0 and Orientation.Landscape. Always. GdxRemote has to be adapted to report the correct values. I put that on my todo list.


