So I've tried the vector rotation sensor using the Android API and it works perfectly to do what I want, but LibGDX does not support this sensor. I've been searching and it seems like I can do the same thing as vector rotation sensor using the accelerometer and compass values returned by LibGDX API:



But I couldn't find out how to do this.


1 Answer 1


You can get the rotation matrix by calling Gdx.input.getRotation(float[] matrix), it gives you the rotation matrix as per the Android SensorManager getRotationMatrix call (SensorManager.getRotationMatrix).

From that you can get a Quaternion out that is a direction and a rotation around that direction.

So if you you're looking to get the pitch, roll and yaw you can retrieve those from the Quaternion directly;

    public class MyAndroidStudioSandboxGame extends Game {

        public void create () {

        public void render () {
            Gdx.gl.glClearColor(1, 0, 0, 1);

            float[] mat = new float[4 * 4];

            Matrix4 m = new Matrix4(mat);

            Quaternion q = m.getRotation(new Quaternion());

            Gdx.app.log("SANDBOX", String.format(
                "Pitch=%.2f, Roll=%.2f, Yaw=%.2f", 
                q.getPitch(), q.getRoll(), q.getYaw()));

The above app will log the orientation (Euler angles) of the device to logcat. There are also getPitchRad, getRollRad and getYawRad that gives you the values in radians.

  • \$\begingroup\$ How can I use the returned values to get the rotation of the device in the three axes? \$\endgroup\$
    – Alex Pi
    Commented Aug 30, 2016 at 21:37
  • \$\begingroup\$ @AlexPi I added a code sample to show how to get the angles out. Hope this helps. \$\endgroup\$
    – bornander
    Commented Aug 31, 2016 at 5:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .