Hello everybody.
So I was tinkering around with accelerometer and gyroscope.
I made a snippet that will move the camera using accelerometer, and rotate the camera depending on the gyroscope input.
When I rotate the smartphone, the input from gyroscope will rotate the camera. This works flawlessly. But the accelerometer got me scratching my head.
To my understanding, the accelerometer will return values of the smartphone movement, not rotation. So If I move to the right, GetRawAccelX() will return positive value.
But, I noticed that the accelerometer did not respond to the direction of my movement, but to the direction of which my smarphone is facing.
For example, when I tilt the smarphone down, GetRawAccelX() gives some values, and the camera moves. When I point it upward, it moves in opposite direction.
To make it clear, I want the camera to move to the right when I move my smartphone to the right. The same goes for the other direction (left, front, down, up, back). I suppose this will require the command to return linear velocities, but I can't find the command that do this.
Am I doing something wrong? Was I getting the concept of acccelerometer input wrong?
If I'm wrong, is there any way to get the values of the smartphone movement? For example, when I move my smartphone to the right, I want some values that signifies its movement to the right.
Here's the snippet:
// set window properties
SetWindowTitle( "Test VR" )
// set window properties
deviceHeight# = GetDeviceHeight()
deviceWidth# = GetDeviceWidth()
SetWindowTitle( "Test World" )
IF deviceWidth# > deviceHeight#
practicalWidth# = deviceHeight#
practicalHeight# = deviceWidth#
ENDIF
IF deviceWidth# < deviceHeight#
practicalWidth# = deviceWidth#
practicalHeight# = deviceHeight#
ENDIF
SetWindowSize( practicalWidth#, practicalHeight#, 0 )
SetDisplayAspect( -1 )
// set display properties
deviceHeight# = GetDeviceHeight()
deviceWidth# = GetDeviceWidth()
SetVirtualResolution( practicalWidth#, practicalHeight# )
SetOrientationAllowed( 1, 0, 0, 0 )
SetClearColor(255,255,255)
SetPrintColor(1,1,1, 100)
//Setup camera
SetCameraPosition(1, 0, 2, -8)
//Load dummy object
loadImage(1,"Wooden Figures/bayotex003.png")
LoadObject(1,"Wooden Figures/bayo.obj")
SetObjectImage(1,1,0)
DO
//Rotate camera
gyroX# = GetRawGyroVelocityX()
gyroY# = GetRawGyroVelocityY()
gyroZ# = GetRawGyroVelocityZ()
CameraRotX# = GetCameraAngleX(1) - gyroX#
CameraRotY# = GetCameraAngleY(1) - gyroY#
CameraRotZ# = GetCameraAngleZ(1) + gyroZ#
//Reset view when screen pressed
IF (GetPointerPressed ( ) = 1)
gyroX# = 0
gyroY# = 0
gyroZ# = 0
CameraRotX# = 0
CameraRotY# = 0
CameraRotZ# = 0
ENDIF
setCameraRotation(1, CameraRotX#, CameraRotY#*1, 0)
//THE PROBLEMatic PART !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Move camera
accelX# = GetRawAccelX()*0.1
accelY# = GetRawAccelY()*0.1
accelZ# = GetRawAccelZ()*0.1
CameraPosX# = GetCameraX(1) + accelX#
CameraPosY# = GetCameraY(1) + accelY#
CameraPosZ# = GetCameraZ(1) + accelZ#
SetCameraPosition(1, CameraPosX#, CameraPosY#, CameraPosZ#)
Print( ScreenFPS() )
print("Gyro: "+str(GetGyroSensorExists()) + " X: "+formatnumber(GetRawGyroVelocityX()) + ", Y: " + formatnumber(GetRawGyroVelocityY()) + ", Z: " + formatnumber(GetRawGyroVelocityZ()))
print("Accel: "+str(GetAccelerometerExists()) + " X: "+formatnumber(GetRawAccelX()) + ", Y: " + formatnumber(GetRawAccelY()) + ", Z: " + formatnumber(GetRawAccelZ()) )
Sync()
LOOP
function formatnumber( f as float )
if ( f > 0 )
s$ = " " + str(f)
else
s$ = str(f)
endif
endfunction s$