Kyle Emmerich
Kyle Emmerich

Reputation: 631

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 don't know how to explain it in words, so here's a beautiful illustration:

Demo of shaking phone?

Upvotes: 0

Views: 921

Answers (4)

MozenRath
MozenRath

Reputation: 10030

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.

Upvotes: 0

Hayk Martiros
Hayk Martiros

Reputation: 2186

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.

Upvotes: 2

André Puel
André Puel

Reputation: 9179

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

Upvotes: 0

Codeman
Codeman

Reputation: 12375

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!

Upvotes: 0

Related Questions