So I'm pretty new to Android programming and my current project involves moving a picture left and right by tilting. It works fine but I can't figure out how to make the accelerometer useful in landscape mode (forced landscape). I get good results if i hold the phone upright (it remains in landscape) but obviously that doesn't really fulfill my needs. Is there a quick fix for this I can use?
Related
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 !
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.
I am busy writing an app that lets me use my phone (Galaxy S2 2.2.3) as a steering wheel. Just a nerdy weekend project really.
I have gotten everything working regarding calculating the orientation of the device using SensorManager.GetOrientation() with a slight snag. The rotation around the axis which comes out of the screen and back of the phone rises from 0 to 90 degrees and then falls back down in the same manner to 0 degrees instead of proceeding to 180.
This had me really confused until I read something somewhere that suggested the API might be flipping (internally as the screen doesn't flip) orientation so the phones coordinates system is flipping from left to right landscape (Its worth noting that I have it locked in landscape mode in the Manifest). This explains the weird behaviour in terms of orientation.
Does anyone know how to stop this happening, or have I gone wrong completely in my understanding?
I made a particle system that was designed for a tablet.. particles follow finger movement etc. anyways.. I implemented the gyroscope so when you tilt the tablet whatever direction.. all the particles fall to that direction. In the manifest I locked it down to landscape view.
So then I loaded it up on a Samsung Intercept. When I moved that screen around nothing was going in the correct direction at all.. So what I did to fix the situation is
if (width<800) // My tablet width is obviously 800 phone is much less
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
else
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
this fixes my problem.. but I'm unsure if this is for all phones? My samsung intercept is only for development and it's a POS IMO. Is it the phone or is this just how it works..
Some devices have a natural orientation of portrait and some have a natural orientation of landscape. This will affect the default sensor coordinate system for the device. Take a look at this post from the Android Developers blog for more details and solutions: http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves-another.html
I know you can set orientation using android:screenOrientation statically and setRequestedOrientation programatically. However the problem is that if I use either of these to set the screen orientation I do not get to choose which landscape or which portrait I want. To wit, flipping the phone 180 degrees does not rotate the view but rather just appears upside down to the user. I cannot use screenOrientation="sensor", because in some cases I would only want the view to be landscape OR portrait and seldom both. This seems insignificant until you consider external hardware.
If I developed an app and set the layout to android:screenOrientation="landscape", the app would only display correctly if the left side of the droid were sitting on a desk. Unforunately the left side is where the usb port is, so the phone would not sit very well on that side if it were plugged in. Unless of course they used a docking station but that is not the kind of help I am looking for. If the right side of the phone is sitting on the desk to allow for the cord, the display appears upside down. I tried posting an image but being a new user I was not able to do so.
What I would like to know is how to programmatically choose which landscape (Surface.ROTATION_90,Surface.ROTATION_270) or portrait (Surface.ROTATION_0,Surface.ROTATION_180) orientation I wish the user to see, but more importantly allow them to do so in the app's settings activity. I have googled for quite awhile and all I can find are sensors that tell you when the orientation/rotation has changed and to what, but none show how to programmatically set either.