There is no exposed API that allows you to do this, so you cant really turn the orientation to match that of the phone
HOWEVER!
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!
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!
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 ()
orientation.screen()
end
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)
end
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 orientation.name(angle) 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)