Detect horizontal and vertical movement of the Sma

2019-05-30 19:52发布

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.

2条回答
叼着烟拽天下
2楼-- · 2019-05-30 20:34

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.

查看更多
迷人小祖宗
3楼-- · 2019-05-30 20:39

It's really difficult to do this type of linear position sensing using the types of sensors that smartphones have. Acceleration is the second derivative of position with respect to time. So in theory, you could use the accelerometer data, integrating twice in time to achieve the absolute position. In practice, noise makes that calculation inaccurate.

On the other hand, if you're really talking about putting the camera facedown on the table, maybe you can come up with some clever way of using OpenCV's optical flow features to detect motion using the camera, almost like an optical mouse. I don't know how you would light the surface (the flash would probably burn through the battery) but it might be possible to work something out.

查看更多
登录 后发表回答