I'm currently messing about with libGDX and will be using the Accelerometer for input. I borrowed some code from the web and B4A'd it. It includes device rotation, orientation, resolution, X, Y, Z Accelerometer axis and compass (Azmuth, Pitch & Roll).
Add the fullsensor orientation into the Project Attributes, more details here: http://developer.android.com/guide/topics/manifest/activity-element.html
Add some configuration options
Enable the Accelerometer and compass and pass it in the initilisation
Initialize the batch and font properties, density is used for the font scaling.
Resize reinitializes the batch....
The main render thread is pretty self explanatory and doesn't have anything too confusing in there.
To get the orientation is slightly different than when using Java as you need to use the native orientation and the deviceangle to determine if it is portrait or landscape depending on phone or tablet.
I've attached the zipped project
Hopefully it's of some use to someone
Cheers!
Neil
Add the fullsensor orientation into the Project Attributes, more details here: http://developer.android.com/guide/topics/manifest/activity-element.html
B4X:
#Region Project Attributes
#SupportedOrientations: fullSensor
Add some configuration options
B4X:
Globals
Dim Config As lgConfiguration
Enable the Accelerometer and compass and pass it in the initilisation
B4X:
Activity_Create
'Enable Accelerometer and Compass
Config.useAccelerometer = True
Config.useCompass = True
lGdx.Initialize2(Config, "LG")
Initialize the batch and font properties, density is used for the font scaling.
B4X:
LG_Create
batch.Initialize
font.Initialize
font.Color = font.Color.RED
font.SetTextureFilter(font.FILTER_Linear, font.FILTER_Linear)
font.Scale(Density)
End Sub
Resize reinitializes the batch....
B4X:
LG_Resize(Width As Int, Height As Int)
batch.dispose
batch.Initialize
Dim resolution As String = Width & "," & Height
Log("Resolution changed " & resolution)
The main render thread is pretty self explanatory and doesn't have anything too confusing in there.
To get the orientation is slightly different than when using Java as you need to use the native orientation and the deviceangle to determine if it is portrait or landscape depending on phone or tablet.
B4X:
LG_Render
' Screen width & height
Dim w As Int = lGdx.Graphics.Width
Dim h As Int = lGdx.Graphics.Height
GL.glClearColor(1, 1, 1, 1)
GL.glClear(GL.GL10_COLOR_BUFFER_BIT)
batch.Begin
' Use rotation and nativeorientation to establish orientation
Dim deviceAngle As Int = lGdx.Input.Rotation
Dim orientation As String = lGdx.Input.NativeOrientation
Dim accelX As Float = lGdx.Input.AccelerometerX ' points To the right (when In portrait orientation)
Dim accelY As Float = lGdx.Input.AccelerometerY ' points upwards (when In portrait orientation)
Dim accelZ As Float = lGdx.Input.AccelerometerZ ' points To the front of the display (coming out of the screen)
' Handy :)
' Y = tilt (lean back OR forward)
' X = twist(like turning a doorknob)
' Z = lift (picking up OR dropping the device)
' Gravity = 9.8ish
' Highest results for axis
If accelX > highestX Then
highestX = accelX
End If
If accelY > highestY Then
highestY = accelY
End If
If accelZ > highestZ Then
highestZ = accelZ
End If
message = message & "Native orientation is " & orientation & CRLF
message = "Device rotated to: " & deviceAngle & " degrees" & CRLF
message = message & "Device orientation Is "
' Get correct orientation for phone or tablet
Select orientation
Case "Portrait"
If deviceAngle = 0 OR deviceAngle = 180 Then
message = message & "Portrait" & CRLF
Else
message = message & "Landscape" & CRLF
End If
Case "Landscape"
If deviceAngle = 90 OR deviceAngle = 270 Then
message = message & "Portrait" & CRLF
Else
message = message & "Landscape" & CRLF
End If
End Select
message = message & "Device Resolution: " & w & " x " & h & CRLF
message = message & "X axis accel: " & accelX & CRLF
message = message & "Highest X value: " & highestX & CRLF
message = message & "Y axis accel: " & accelY & CRLF
message = message & "Highest Y value: " & highestY & CRLF
message = message & "Z axis accel: " & accelZ & CRLF
message = message & "Highest Z value: " & highestZ & CRLF
'Azimuth, rotation around the Z axis (0<=azimuth<360). 0 = North, 90 = East, 180 = South, 270 = West
'Pitch, rotation around X axis (-180<=pitch<=180), with positive values when the z-axis moves toward the y-axis.
'Roll, rotation around Y axis (-90<=roll<=90), with positive values when the z-axis moves toward the x-axis.
If lGdx.Input.isPeripheralAvailable(lGdx.Input.PERIPHERAL_Compass) Then
message = message & "Azmuth: " & lGdx.Input.Azimuth & CRLF
message = message & "Pitch: " & lGdx.Input.Pitch & CRLF
message = message & "Roll: " & lGdx.Input.Roll
Else
message = message & "No compass available"
End If
font.DrawMultiLine(batch, message, 0 , h)
batch.End
I've attached the zipped project
Hopefully it's of some use to someone
Cheers!
Neil
Attachments
Last edited: