Search code examples
androidlibgdx

How to detect when device is oriented upside-down in libGDX


I'm starting to develop a game for Android using libGDX.

I noticed that some sample games for this library all have fixed screen orientation. I want my game to be landscape all the time, but I want to rotate the camera 180 degrees when the device is turned upside down (most well behaved Android apps do this automatically).

I'm looking at the docs and Googling, but can't find anything about it.

Basically, I'd like to know what to add to base libGDX app. that shows libGDX logo, to just react when phone is turned upside down?

The Android docs, list this functionality in getRotation function here: http://developer.android.com/reference/android/view/Display.html#getRotation%28%29

This allows you to get 0/180 or 90/270 for rotation and thus detect how the device is turned.

But I don't see this exposed in libGDX?


Solution

  • Nevermind, I just found it. You need to change:

    android:screenOrientation="landscape"
    

    to:

    android:screenOrientation="sensorLandscape"
    

    and it works automagically. Maybe libGDX devs should make this the default.