Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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?

like image 409
Milan Babuškov Avatar asked Aug 03 '13 16:08

Milan Babuškov


1 Answers

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.

like image 91
Milan Babuškov Avatar answered Sep 20 '22 06:09

Milan Babuškov