Search code examples
androidaccelerometerandroid-sensors

Detect horizontal and vertical movement of the Smartphone on a table


I'm trying to write a small android game where the phone is placed on the table.
On the screen there is a ball, which the user control its movement by moving the phone.
Along all the game the user won't lift the phone from the table.
At the beginning the ball will placed in the middle of the screen:
initial state
Pushing the phone from the user:
Pushing the phone from the user
enter image description here

should move the ball toward the top of the smartphone screen:
the ball move toward the top of the smartphone screen

And from the current position of the ball, moving the phone back to the user, and to the right:
enter image description here
enter image description here
will move the ball accordingly:
enter image description here

I read the Android Motion Sensors Guide carefully but I didn't even realize what Sensor \ Sensors should I use.
I would love to get any directions.


Solution

  • First of all TYPE_LINEAR_ACCELERATION, TYPE_ROTATION_VECTOR, TYPE_GRAVITY are not physical sensors but made from sensor fusion.

    Secondly from Android 4+ these fused sensors make use of device Gyroscope, so they WON'T work if the mobile device doesn't has a gyroscope.

    So if you want to make a generic app for all phones prefer using only Accelerometer (TYPE_ACCELEROMETER).

    Now for your case since user won't lift the mobile from table, and if you want you can easily subtract the Gravity component from accelerometer. See http://developer.android.com/reference/android/hardware/SensorEvent.html under section Sensor.TYPE_ACCELEROMETER. (Code is given too).

    Now you can see How can I find distance traveled with a gyroscope and accelerometer? to find the linear displacement & the 1st answer states their is NO use of Gyroscope. (Or you can just google for finding the displacement/Linear velocity from acceleromter readings)

    Hope this all would give you quite lot an idea.