I’m making a LibGDX (1.9.9) game that is meant to be fixed in landscape mode, but when I run it on certain devices, it shows up in portrait mode.
I’ve tested it on several physical and virtual devices ranging from Android 4 to 11 as well as on an iPhone simulator through RoboVM (2.3.9). This happens on an android virtual tablet (Android 11) and the iPhone simulator. It has gotten stuck in portrait a couple times on a physical device as well, but I have not been able to reproduce it. I haven’t been able to understand why this could be.
I have landscape (android:screenOrientation="landscape”) set in the android manifest.
Gdx.input.getRotation() returns 90 when it is properly set on landscape, but 0, when in portrait so perhaps I can try checking if it is 0 and change the rotation dynamically, but I have not been able to find out how to do that either.
I’ve tried a workaround where I check if screen width < screen height and rotate the camera 90 degrees if that is true to put it into landscape manually, but that causes the camera to become distorted in the manner shown below even though I know I’ve modified the aspect ratio correctly for the rotation.
How camera distorts when trying to rotate to landscape
I am using OrthographicCamera without any Viewports.
// Screen dimensions
width = ((float) Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight()) * GameSpecs.WORLD_HEIGHT;
height = GameSpecs.WORLD_HEIGHT;
System.out.println("width: " + width);
System.out.println("height: " + height);
System.out.println("orientation: " + Gdx.input.getNativeOrientation());
System.out.println("rotation: " + Gdx.input.getRotation());
// If showing portrait and needs to be rotated
rotateForProperLayout = width < height;
if (rotateForProperLayout) {
height = ((float) Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth()) * GameSpecs.WORLD_HEIGHT;
width = GameSpecs.WORLD_HEIGHT;
}
cam = new OrthographicCamera(width, height);
// Rotate camera to be in landscape
if (rotateForProperLayout) {
cam.rotate(-90);
}
cam.translate(cam.viewportWidth / 2f, cam.viewportHeight / 2f);
cam.update();
My Android Manifest...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mydev.mygame” >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:isGame="true"
android:appCategory="game"
android:label="@string/app_name"
android:theme="@style/GdxTheme" >
<activity
android:name="com.mydev.mygame.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation=“landscape"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Any help in trying to understand the reason for this or possible workarounds would be greatly appreciated. Please let me know if I should clarify anything or provide more information.
Thanks in advance.