0
votes

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.

1
I just noticed you've got a weird quote after android:screenOrientation, and the syntax colouring has screwed up. Assuming this is copied directly from your manifest, this could be your problem. If it is, I'm surprised it ever built ok.funferret-com
@funferret-com I think that was just when I pasted it here. I retyped the line and ran and same issue.iwritecode
Sorry, I have no idea what's wrong. I assume you've done a complete clean and rebuild of project, a few times I've wasted an hour or two hunting down some non-existent bug that went away when I cleaned my project.funferret-com
Just one more point, you could maybe post more of your code, since we can't really see what it's doing. eg. I assume that code you've posted is called on a resize and not just in the create function or something silly like that.funferret-com
Hmm. I'm not sure what else I can post that would be relevant since it's meant to be fixed in landscape, I didn't use resize. The only other place I reference camera is when setting the projection matrix for the SpriteBatch... game.batch.setProjectionMatrix(game.cam.combined); It's only an issue with Android 11 and iOS it seems so it might be LibGDX. I'll probably just need a workaround. Thank you for your suggestions.iwritecode

1 Answers

0
votes

Since no ones helped you so far I'll post my android manifest, you should maybe post yours since this is almost certainly where the problem is. I set this up a long time ago and, to be honest, can't really remember exactly why everything is set as it is, but it works for me. Note: I use device rotation to control my game in some modes.

As for your manual screen rotation issues, you could access the cameras projection matrix and scale it accordingly to get it looking ok. I do not recommend this though, since it is difficult to track how screen resize calls should alter your layout when you also have to track screen rotation. You would have to consider if the width change should actually be considered a height change when in portrait mode and vice versa (depending on your game complexity). I would recommend just sorting out your manifest. Good luck.

        android:screenOrientation="fullSensor"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <activity
            android:name="com.funferret.tr.AndroidLauncher"
            android:label="@string/app_name" 
            android:screenOrientation="sensorLandscape"
            android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>