I am trying to rotate the application screen based on the device orientation.
I used the SETORIENTATION example from the GLB Help file and I added the GETORIENTATION.
I complied and run the code on my iPhone but the orientation does not change when I turn the device UP, LEFT, RIGHT or DOWN.
Does GETORIENTATION reads the actual iPhone/iPad hardware orientation sensor?
Should I saw some examples in the FORUM that use C++ code, but I am not sure If I need to use it!
Here is the code:
// --------------------------------- //
// Project: Orientation
// Start: Tuesday, February 28, 2012
// IDE Version: 10.237
// SETCURRENTDIR("Media") // go to media files
// Test mode: 320x480
X_OBJSTART 0
X_OBJADDVERTEX 0, 20, 0, 0,0,0xffffff
X_OBJADDVERTEX 0,-20, 10, 0,0,0xff0000
X_OBJADDVERTEX 0,-20,-10, 0,0,0x0000ff
X_OBJNEWGROUP
X_OBJADDVERTEX 0, 20,0, 0,0,0xffffff
X_OBJADDVERTEX 30,-10,0, 0,0,0xff0000
X_OBJADDVERTEX -30,-10,0, 0,0,0x0000ff
X_OBJNEWGROUP
X_OBJADDVERTEX 10.5,-15, 10.5, 0,0,0x00ffff
X_OBJADDVERTEX 10.5, 15, 10.5, 0,0,0x00ffff
X_OBJADDVERTEX -10.5,-15, -10.5, 0,0,0x00ffff
X_OBJADDVERTEX -10.5, 15, -10.5, 0,0,0x00ffff
X_OBJEND
LOADFONT "Media/fonts/APDfont_Blue.bmp",0
SETFONT 0
LOCAL mx%, my%, b1%, b2%
LOCAL irot% = 0
LOCAL accelx%,accely%,accelz%
LOCAL rot%
LOCAL info$ = PLATFORMINFO$("")
WHILE TRUE
accelx = GETJOYX(0)*100
accely = GETJOYY(0)*100
accelz = GETJOYZ(0)*100
rot = GETORIENTATION()
PRINT "X:"+accelx+" Y:"+accely+" Z:"+accelz, 0, 300
SELECT rot
CASE 0
PRINT "TOP ROT:"+rot, 0, 340
irot=0
CASE 1
PRINT "RIGHT ROT:"+rot, 0, 340
irot=1
CASE 2
PRINT "BOTTOM ROT:"+rot, 0, 340
irot=2
CASE 3
PRINT "LEFT ROT:"+rot, 0, 340
irot=3
ENDSELECT
IF info$="WIN32"
IF KEY(2) THEN irot=0
IF KEY(3) THEN irot=1
IF KEY(4) THEN irot=2
IF KEY(5) THEN irot=3
ENDIF
// rotate the screen. Stays locked until you change that!
SETORIENTATION irot
// mouse position on screen (in screen pixels - not device)
MOUSESTATE mx, my, b1, b2
// print where mouse is and rotation index
PRINT "Rot: "+irot+" Mouse: "+FORMAT$(4,0,mx)+ ", "+FORMAT$(4,0,my), 0,0
No. Getorientation returns what is set with Setorientation.
Usually you only rotate to
SETORIENTATION( MOD(GETORIENTATION()+1, 4) // when tilting left
SETORIENTATION( MOD(GETORIENTATION()+2, 4) // when tilting to top
SETORIENTATION( MOD(GETORIENTATION()+3, 4) // when tilting right
Gernot,
To make sure I understand.
In the case of the iPhone/iPad, I cant read the orientation sensor using GLB'. Correct?
I have to have an option to set the orientation manually?
Sure. GETJOYX(0) and GETJOYZ(0) is what you want.
Gernot,
Thanks for your help!
I finally got the device orientation working when I flip the device to left, right, bottom or top.
I used the JOYSTATE to get the orientation from the device and modified the "SETORIENTATION"sample file from the GLB help.
I tested on the iPhone 4S, iPad2, HP Touchpad and Palm Pre 2 and it works fine!
On the TP and iPad the device "Rotation Lock" switch doesn't lock the screen. Can this be an issue with the Apple or HP app submission?
Here is the code:
// --------------------------------- //
// Project: Orientation
// Start: Tuesday, February 28, 2012
// IDE Version: 10.237
// SETCURRENTDIR("Media") // go to media files
// Test mode: 320x480
X_OBJSTART 0
X_OBJADDVERTEX 0, 20, 0, 0,0,0xffffff
X_OBJADDVERTEX 0,-20, 10, 0,0,0xff0000
X_OBJADDVERTEX 0,-20,-10, 0,0,0x0000ff
X_OBJNEWGROUP
X_OBJADDVERTEX 0, 20,0, 0,0,0xffffff
X_OBJADDVERTEX 30,-10,0, 0,0,0xff0000
X_OBJADDVERTEX -30,-10,0, 0,0,0x0000ff
X_OBJNEWGROUP
X_OBJADDVERTEX 10.5,-15, 10.5, 0,0,0x00ffff
X_OBJADDVERTEX 10.5, 15, 10.5, 0,0,0x00ffff
X_OBJADDVERTEX -10.5,-15, -10.5, 0,0,0x00ffff
X_OBJADDVERTEX -10.5, 15, -10.5, 0,0,0x00ffff
X_OBJEND
LOADFONT "Media/fonts/APDfont_Blue.bmp",0
SETFONT 0
LOCAL mx%, my%, b1%, b2%
LOCAL irot% = 0
LOCAL accelx%,accely%,accelz%
LOCAL accelRx%,accelRy%,accelRz%
LOCAL rot%
LOCAL joy_x, joy_y, joy_a, joy_b
GLOBAL text$=""
LOCAL info$ = PLATFORMINFO$("")
WHILE TRUE
accelx = GETJOYX(0)*100
accely = GETJOYY(0)*100
accelz = GETJOYZ(0)*100
accelRx = GETJOYRX(0)*100
accelRy = GETJOYRY(0)*100
accelRz = GETJOYRZ(0)*100
rot = GETORIENTATION()
PRINT "X:"+accelx+" Y:"+accely+" Z:"+accelz, 0, 300
PRINT "RX:"+accelRx+" RY:"+accelRy+" RZ:"+accelRz, 0, 320
JOYSTATE joy_x, joy_y, joy_a, joy_b;
IF joy_x=1; LET text$="LEFT";SETORIENTATION (3); ENDIF;
IF joy_x= -1; LET text$="RIGHT";SETORIENTATION (1); ENDIF;
IF joy_y=-1; LET text$="TOP"; SETORIENTATION (0); ENDIF;
IF joy_y= 1; LET text$="BOTTOM"; SETORIENTATION (2); ENDIF;
PRINT text$, 0, 340
IF info$="WIN32"
IF KEY(2) ; irot=0; text$="TOP"; ENDIF;// top
IF KEY(3) ; irot=1; text$="RIGHT"; ENDIF;// right
IF KEY(4) ; irot=2; text$="BOTTOM"; ENDIF;// bottom
IF KEY(5) ; irot=3; text$="LEFT"; ENDIF;// left
// rotate the screen. Stays locked until you change that!
SETORIENTATION irot
ENDIF
// mouse position on screen (in screen pixels - not device)
MOUSESTATE mx, my, b1, b2
// print where mouse is and rotation index
PRINT "Rot: "+irot+" Mouse: "+FORMAT$(4,0,mx)+ ", "+FORMAT$(4,0,my), 0,0
// get the full viewport
LOCAL vpx%, vpy%, x%, y%
GETVIEWPORT x, y, vpx, vpy
DRAWLINE x, y, vpx, y, RGB(255,128,128)
DRAWLINE x, y, x, vpy, RGB(255,128,128)
DRAWLINE x+vpx-1,y, x+vpx-1,vpy, RGB(255,128,128)
DRAWLINE x, y+vpy-1,vpx, y+vpy-1, RGB(255,128,128)
PRINT "VP: "+x+","+y+" "+vpx+" x"+vpy, 0,20
// and screen size
GETSCREENSIZE vpx, vpy
PRINT "SC: "+vpx+"x"+vpy, 0,40
// draw line to viewport rect
DRAWLINE vpx, vpy, 50+200,100+100, RGB(0,255,255)
// render to a viewport area
VIEWPORT 50,100, 200,100
// make sure we don't paint outside the viewport
DRAWRECT -999,-999,2000,2000,RGB(64,64,128)
// render 3D into that viewport
do3D()
// start rendering in 2D over the same viewport again
X_MAKE2D
VIEWPORT 50,100, 200,100
PRINT "2D Viewport 200x100",0,0
// is the size correct?
GETVIEWPORT x, y, vpx, vpy
PRINT "VP: "+x+","+y+" "+vpx+" x"+vpy, 0,20
// now grab that viewport and paste it again
VIEWPORT 0,0,0,0
GRABSPRITE 1, 50,100,200,100
DRAWSPRITE 1, 50,201
// mouse position
DRAWRECT mx-4, my-4, 8,8,RGB(255,255,0)
SHOWSCREEN
WEND
FUNCTION do3D:
ALPHAMODE 0
X_CULLMODE 0
X_MAKE3D 1, 100, 45
X_CAMERA 3,3,40, 0,0,0
X_ROTATION GETTIMERALL()*0.1, 0,1,0
X_DRAWOBJ 0,0
ENDFUNCTION
oh -beware the getjoy values also rotate after an setorientation.
So, basically you rotate +1 for right turn, +2 for upside down and +3 for left turn.
Then you MOD the new orientation with 4 to be in range 0...3
Test to turn to the top at first and then right. Then test left around to be sure.
I used this function (for landscape only):
// change orientation on tilting of the device
FUNCTION Touchpad_Orientation%:
// orientation for iPad
IF GETJOYY(0) > 0.8
LOCAL ori% = MOD(GETORIENTATION()+2, 4)
SETORIENTATION ori%
ENDIF
ENDFUNCTION
I use something like code for Android and iOS. i have not a TouchPad (but is one coming soon):
FUNCTION ControlTilt: direct=0
STATIC COUNTER=0
STATIC ORIN=0
LOCAL x, y
LOCAL NEWRIENTATION
STATIC yres, xres
COUNTER=COUNTER+1
IF COUNTER>100 THEN COUNTER=85
IF (COUNTER=17 OR direct=1)
IF ORIN>-1 AND GETORIENTATION()<>ORIN
SETORIENTATION ORIN
GOTO rescheck
ENDIF
ENDIF
IF MOD(COUNTER, 15)<>5 THEN RETURN
GETSCREENSIZE xres,yres
IF GETORIENTATION()=0
y=GETJOYY(0)*100
x=GETJOYX(0)*100
ELSEIF GETORIENTATION()=1
y=-GETJOYX(0)*100
x=GETJOYY(0)*100
ELSEIF GETORIENTATION()=2
y=-GETJOYY(0)*100
x=-GETJOYX(0)*100
ELSE
y=GETJOYX(0)*100
x=-GETJOYY(0)*100
ENDIF
// SWAP x, y
IF (y>60 OR KEY(17)=1) AND ORIN<>2
ORIN=2; COUNTER=0;
ELSEIF (y<-60 OR KEY(31)=1) AND ORIN<>0
ORIN=0; COUNTER=0;
ELSEIF (x>60 OR KEY(32)=1) AND ORIN<>3
ORIN=3; COUNTER=0;
ELSEIF (x<-60 OR KEY(30)=1) AND ORIN<>1
ORIN=1; COUNTER=0;
ENDIF
RETURN
// this game support port portrait and lanscape mode, and hence its need to checkout resoulution and checking if it was set on PORTRAIT or not. might not been needed in your game.
rescheck:
LOCAL oldScreenWidth=ScreenWidth
LOCAL oldScreenHeight=ScreenHeight
GETSCREENSIZE ScreenWidth, ScreenHeight
LOCAL OLDPORTRAIT=PORTRAIT
IF ScreenWidth>ScreenHeight
PORTRAIT=0
ELSE
PORTRAIT=1
ENDIF
CreateInterface(KEYBOARD)
ENDFUNCTION
I dont like if it rotate it directly, so its dosent flip totally when you tilt to a corner. Hence the counter.
spacefractal,
Thank you, I will give it a try.