java - How to adjust accelerometer data for phones held in different positions. -
i'm making libgdx game using android studio alien ufo has avoid incoming asteroids, , ufo moves using accelerometer data.
//gets , smoothens accelerometer data. values x , y switched because game played in landscape mode. currentaccelerometerx = gdx.input.getaccelerometery(); currentaccelerometerx = currentaccelerometerx * alpha + lastaccelerometerx * (1 - alpha); currentaccelerometery = gdx.input.getaccelerometerx(); currentaccelerometery = currentaccelerometery * alpha + lastaccelerometery * (1 - alpha); //moves alien alien.alienx += (currentaccelerometerx * 5); alien.alieny -= (currentaccelerometery * 5); lastaccelerometery = currentaccelerometery; lastaccelerometerx = currentaccelerometerx;
this code works when phone being held of phone parallel floor, such position can uncomfortable play in after while. wondering how take account current rotation of device , adjust movement of ufo accordingly.
the problem seems y position of alien, meaning x accelerometer data of phone(the values switched since game played in landscape mode). when phone held @ angle floor, ufo stays near bottom edge of screen.
jollygoodsir, when new game starts, take snapshot of current state of accelerometer , set base point. when check state of accelerometer changes see how tilt being changed, compare delta base value. game calibrate whatever position held device in when game started. happy days :-)