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

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?

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.

Related

Wrong Camera Orientation with Android & Vuforia

We are developing our own Android-based hardware and we wish to use Vuforia (developed via Unity3D) for certain applications. However, we are having problems making Vuforia work well with our current camera orientation settings.
On our hardware, when the camera is placed horizontally - everything works fine. That is, when the camera is parallel to the placement of the display. However, we need to place the camera vertically, or in other words, with a 90 degree difference to the placement of the display. These are all hardware settings. Our kernel is programmed according to such settings and every other program that utilises the camera works compatibly with everything, including our IMU sensors. However, apps developed with Vuforia behave completely odd when the camera is placed vertically.
We assume the problem to be related to Vuforia's algorithms of processing raw camera data however we are not sure. Moreover, we do not know how to fix the situation. For further details, I can list:
-When "Enable Video Background" is on, the projected image is distorted and no video feed is available. The AR projection appears on a black background with distorted dimensions.
-When "Enable Video Background" is on and the device is rotated, the black background is replaced by flickering solid colors.
-When "Enable Video Background" is off, the AR projection has normal dimensions (no distortion) however it is tracked with wrong axis settings. For example, when the target moves left in real world, the projection moves up.
-When "Enable Video Background" is off and the device is rotated, the AR projection is larger compared to its appearance when the device is in it's default state.
I will be glad to provide any more information you need.
Thank you very much, have a nice day.
PS: We have found out that applications that use the camera as a main purpose (Camera apps, Barcode Scanners, etc) work fine while apps for which camera usage is an extra quality (such as some games) have the same problem as Vuforia. This make me think that apps who access the camera directly work fine whereas those who use Android API and classes fail for some reason.
First understand that every platform deals with cameras differently and that beyond this different android phone manufacturers deal with these differently as well. In my testing WITHOUT vuforia I had to transform the plane I cast the video feed onto 0,-90,90 for android/iphone and -270,-90,90 for the windows surface tablet. Past this the iPhone rear camera was mirrored, the android front camera was mirrored as well as the surface front camera. That is easy to account for, but an annoying issue is that the Google Pixel and Samsung front cameras were mirrored across the y (as were ALL iOS on the back camera), but the Nexus 6p was mirrored across the x. What I am getting at here is that there are a LOT of devices to account for with android so try more than just that one device. Vuforia so far has dealt with my pixel and 4 of my iOS devices just fine.
As for how to fix your problem:
Go into your player settings for unity and look at the orientation. There are a few options here and my application only uses portrait so I force portrait and it seems to work fine (none of the problems I had to account for with the above mentioned scenario). Vuforia previously did NOT support auto rotation so you need to make sure you have the latest version since it sounds like that is what you need. If the auto rotate is set and it is not working right you may have to account for that specific device (don't do this for all devices until after you test those devices). To account for that device use an if (or construct a case statement if you have multiple instances of this problem with different devices) and then reflect or translate as needed. Cross platform development systems (like unity) doesn't always get everything perfect since there is basically no standard. In these cases you have to directly account for them by creating a method and a case statement within that so you can cleanly and modularly manipulate all necessary devices. It is a pain, but it beats developing for all devices separately.
One more thing is make sure you check out the vuforia configuration file as it has some settings such as camera mirror and direction settings on there. These seem to be public settings so you should also be able to script to these in your case statement in the event you need to use "Flip Horizontally" for one phone, but not another.

Change screen orientation in a Unity Google Cardboard project

I'm just starting a cardboard project as it's really joyful to experiment VR without any huge hardware, but I have a little problem which sounds really basic : the screen orientation. I just would have my game playable in Landscape Right instead of Left as I'm developing on my Xperia Z.
Usually screen orientation's problems are solved by forcing the orientation through the Player's Settings in the Build Menu. But here, the fact is that when I tick the "Landscape Right" box instead of the left one (which is the default setting of the Cardboard API) it correctly inverts my display but my camera stays at the same orientation as before.
From this results that my gyroscope works in reverse and that the ground is seen on the top, with all the texts reversed as well.
I've already done what Eldir tried here (Google cardboard device orientation) but as it wasn't the same problem I'm having, it wasn't efficient.
Do you have any ideas that if the API allows a quick change for this, or if I have to modify the whole parameters of the gyroscope ?
Thanks !

Camera is being rotated 90 degree in air for android

In my AS3 Flex Mobile application for Android, I am using camera and it is being automatically rotated 90 degrees before I even done any video rotation by myself, it seems like it's a known bug in AIR. But I was wondering if anyone found a solution since it's really pretty important feature for mobile application developer.
I've tried to do some rotation manually in my code, but it is only fixes the view on my display, but still sends the wrong video to the receiver.
If any code is required I will add the snippets
Please let me know.
As you mentioned, this is a known bug with AIR. It is not consistent, either. On some devices, it is in the correct orientation but in some (and all iOS devices, I believe, though I haven't fully tested that), it is rotated as you are seeing. For example, it was always oriented correctly on my Nexus 4 and on my Nexus 5, but a friends Moto X is rotated incorrectly.
Unfortunately, I don't believe there is anything you can do short of having the user do a calibration (i.e. overlay a straight line and tell them to place it horizontally and click a button) and rotating the camera display and any images you take with the display.
That being said, if you are using the camera to take photos, I highly recommend using CameraUI instead, which is the native implementation.
I've faced the same issue today but i'm developping in Java, not with AIR so i don't know if it the same, for me the solution was to add this line before starting the recording.
mMediaRecorder.setOrientationHint(90);

How to get device orientation

What is the best way to get the device orientation with cocos2d-x (c++)?
My Android/iOS application does support all 4 device orientations, and more important than knowing if it is landscape or portrait, I would like to know if it is upside down or not.
I will have to know from within a CCLayer.
UPDATE
I am trying to get events for right tilt and left tilt. To do so, I am calculating the angle at which the device is held. However, I need to know what orientation the device has, to be able to know if I am actually tilting left or right.
Though cocos2d-x is cross platform, few things are still very much platform dependent, for example, if you see the CCFileUtils, we have separate implementation files for iOS, Android etc, the cross platform is maintained with a single header,
Keeping that in mind,
In Android, you can get the device orientation by using the constants,
Surface.ROTATION_0 (no rotation)
Surface.ROTATION_90
Surface.ROTATION_180
Surface.ROTATION_270.
which you can get by calling Display.getRotation()
(It only rotate in all these directions if, android:screenOrientation:fullSensor is set)
And in iOS,
You can get the values with
UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
(It only rotates in all these directions if, supportedInterfaceOrientations method returns UIInterfaceOrientationMaskAll)
Note:
Its not a good practice to rotate a phone upside down in portrait mode, only on tablets its accepted.. (Apple might even reject your app for this reason)
Let me know if you want to know how to use it, but googling the keywords will give you the usage..
After a quick google search :
http://www.cocos2d-x.org/wiki/Device_Orientation
http://www.cocos2d-x.org/forums/6/topics/36414
UDPDATE:
I don't think you can determine the orientation the way you want to from cocos2d-x.
Then the only solution that I'm thinking of is to go into java through jni and use android's methods to determine the current orientation. Here's a
link
on how to determine all 4 orientations.
As for how to call java methods from c++, that's another ball game.

GLSurfaceView device orientation change, black screen

Im porting an iOS game to Android.
Im using as template the GL2JNI example from the latest NDK, and have added inside GL2JNIActivity.java/onCreate mView.setPreserveEGLContextOnPause( true ); to preserve the EGL context but it as soon the device orientation change the screen is all black.
I've research on this and some suggests to reload all textures, shaders, geometry etc... But that is not an option for me (too slow to reload).
Is there any way to handle like on iOS multiple devices orientation using on OpenGLES2 view on Android? If yes how can it be done without reloading everything. I've seen like Unity games doing it... so it must be possible right?
TIA!

Categories

Resources