Compass & Orientation Support in Libgdx

I added compass and orientation support to libgdx’s Input class today. You actually only get the orientation of the device around three axis in degrees (yes, we are fancy and like to multiply…). Here’s the code for a simple demo that will show you the current orientation angles as well as a triangle pointing towards the magnetic north pole.

This example can of course be found in the test project under the name CompassTest. Here’s an image illustrating the orientation of the axis relative to the world taken from the Android docs.

The azimuth gives you the rotation around the z-axis, that one can be used to align something with the magnetic north pole. You can find out more in the documentation of the SensorManager.getOrientation() method in the Android docs which is what we use directly. The only difference to this is that we return the angle in degrees instead of radians.

4 thoughts on “Compass & Orientation Support in Libgdx

  1. A really nice addition ! Most (if not all) devices contain at least an accelerometer and a magnetic sensor, so you now covers every basic developer need ! Thank you !

    You should create a seedFakeOrientation(float azimuth, float pitch, float roll) method for desktop testing, and make your desktop backend getAzimuth/Pitch/Roll() methods returns the values given by this method. This way, we can change orientation values every 3 seconds for instance, and see if everything is working fine 😉

  2. @link That’s what we do

    @obli yeah, that makes sense, i’m not yet sure if it should be a part of the Input interface though.

Leave a Reply

Your email address will not be published.