开发者

Detect specific shaking movement (illustrated :D)

I am developing an Android platformer. I wanted to do a jumping control where basically, you shake the phone. However, as you can probably tell, I'm a little stumped.

I have X,Y,Z values from the accelerometer, how do I tell if the player is shaking the phone? As far as I know, this isn't trivial... at least for me.

Shaking might be the wrong word, it's more like just moving the phone up to jump. I do开发者_如何学Cn't know how to explain it in words, so here's a beautiful illustration:

Detect specific shaking movement (illustrated :D)


I'm going to describe how to do it manually.

An accelerometer returns values that correspond to the acceleration felt by the object.

For example, if the X value is high, that means that the phone is being accelerated in its X-direction. However, you don't know from a single snapshot if this acceleration is because of shaking or because of suddenly moving in a steady direction.

Therefore, what you are looking for is when the acceleration is high, and then suddenly very low or negative, and so on. What you want is the rate of change of acceleration, or jerk.

In term of code, you want to keep a history of the acceleration values, and keep a running jerk value (say, change in acceleration in the last 10 frames). If this value reaches a threshold (positive or negative), then this is your shake.

Of course, do this for X, Y, and Z accelerations individually and then add the jerk values to get the final one that is tested for the threshold.


You want to use Linear Acceleration Sensorsto accomplish this task.

Simply put the sensor type in your trigger code and put any logic you need in onSensorChanged().

Hope this helps!


Think of rour accelerometer coordinates as a ball with inertia. If the phone jumps the ball jumps (Z goes up, and then back down).


I think you should time average the accelerometer readings for like 250 ms and then see if the avg is higher than a value(at your discretion and the requirement of the application) in the direction u expected. if it does your action is complete. when you shake, the accelerometer's time avg will be mostly neutral. but what i have described above will need to be repeatedly tested to groove it to the right numbers for the better user experience.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜