Page 1 of 1

Question/suggestion about rotating screen

Posted: Wed Aug 19, 2015 5:29 am
by HugoBDesigner
So, I've taken a quick look into the Android port, so I wanted to know if there's any way to rotate the display of a game so it matches the orientation of the screen? If it's possible, how? If not, has anyone tried to make it? I just want to know because, in case it isn't currently possible, I'll have to improvise something with screen orientation, window-size and translating touch coordinates. Thanks :awesome:

Re: Question/suggestion about rotating screen

Posted: Thu Aug 20, 2015 10:31 pm
by Positive07
There is no exposed API that allows you to do this, so you cant really turn the orientation to match that of the phone


There is a trick! We have access to the accelerometer, and you may say, the accelerometer is no good to work with angles and orientations and I will grant you that, but there is a constant acceleration that we can use to measure changes in angles... GRAVITY!

So well in order to turn the Gravity vector into an angles with just the values of X, Y and Z provided by the accelerometer you would need to do some complex MATH (not METH but MATH) that is pretty HARD and I cant even grasp, so instead what I did was make a plot app that shows me the traces of each individual axis of the accelerometer, then I noticed some patterns and made a library that detected this patterns and gave me the orientation of the screen!

The first prototype sucked so I wont show that, the second one worked REALLY WELL (Actually the newest attempts are not quite there as this one just yet, but soon they will be there dont worry) The only problem about this, CODE SUCKS!
(3.65 KiB) Downloaded 663 times
So I reworked the API and made a nice library that did just what was needed and here you have it, with an added demo!
Orientation library
(7.47 KiB) Downloaded 788 times
And a good demo!
(3.88 KiB) Downloaded 776 times
Library Usage:

Require the library, wherever you like doesnt need to be love.load

Code: Select all

local orientation = require "orientation"
Then you need to give the screen size or default orientation to the library by calling orientation.screen(), if you are using the whole screen I recommend you call it without arguments, otherwise you can pass width and height, or simply "landscape" or "portrait", this needs to be done ONCE! and in love.load

Code: Select all

love.load = function ()
Call orientation.update(dt) on love.update, this will keep it updated (even if you dont need to update the angle every frame you should do this!)

Code: Select all

love.update = function (dt)
   local angle = orientation.update(dt)
Notice that this function returns the angle! you can already use this to rotate your camera or whatever, but if you dont need it in love.update but in love.draw you can use orientation.get() to get the angle in the last update!

Note that this functions can return false! if for example the accelerometer joystick was not found, or this is not running on mobile. You should use orientation.possible() to check if the functions will work

If you need the name of the current screen mode you can use where angle is the angle returned by orientation.get() or one of the following angles: 0, math.pi/2, math.pi and 3*math.pi/2, if it is none of this you will get some weird errors (same if you pass false so be sure to check that orientation.possible() is not false)

Re: Question/suggestion about rotating screen

Posted: Fri Aug 21, 2015 4:36 am
by HugoBDesigner
Thank you! I just tested it, and it's amazing! :awesome:

Really, can't thank you enough. I messed around with orientation myself before, in order to get my Andralog library working, and decided to try to use that to make the screen rotate and translate coordinates, but that worked terribly. Now thanks to you, I can have proper screen rotation :rofl:

Re: Question/suggestion about rotating screen

Posted: Sat Aug 22, 2015 5:12 pm
by Positive07
I should note some issues with this method:

Notification bar will still be on the position that the AndroidManifest requires so for example if the manifest says landscape but you use this lib to display on portrait the notification bar will be on the right/left, which is weird because it should be on the top

This doesnt change the keyboard orientation so you should probably force the AndroidManifest orientation when you need keyboard input, alternatively you could draw your own keyboard but I dont think the user will like that, and it is pretty hard to make keyboards that match all cellphones

You need to mess with the tresholds to find the ones that suit you!

Anyway this is a WIP so if you find any bugs tell me and I'll fix them right away

Re: Question/suggestion about rotating screen

Posted: Sat Aug 22, 2015 5:47 pm
by HugoBDesigner
Well, I won't be using keyboard for my game (at least not anytime soon), so that shouldn't be an issue. I still wish that the Android port developers worked on making a fully integrated orientation that included touch translation and keyboard rotation, but it's understandable that it may not be that easy.

As for bugs, I really just faced one, but wasn't a big deal. I just rotated the screen clockwise on your demo, to test it out, and at the end, instead of going back to landscape, it became upside down. Rotating it back fixed it, and the problem didn't reoccur when I tried again, so it may very well have been user-sided. But I'll inform you of any issue I might have.

Again, thank you very much :3

Re: Question/suggestion about rotating screen

Posted: Wed Aug 26, 2015 9:04 pm
by Positive07
Yes that is probably an issue with your phone's accelerometer, mine sucks!! It detects top and bottom wrong and left and right wrong sometimes, as you said rotating it again fixes the issue. That is why you should mess up the tresholds! Android has a HUGE time treshold in my phone, while the demo uses a 0.3 seconds time tresholds which is pretty fast, you should mess with that if you would like in order to get differents sensibilities (you could also leave the option to the user)