Archive for November 10th, 2008

10
Nov

Snippet: Recognizing Shakes

Here’s a couple tips for you on using accelerometer:didAccelerate data to recognize a shake of the device, which has to be the most awesome method of user input ever.

First off, from the redoutable Erica Sadun:

I personally took the dot product as such:

float dot = (px * xx) + (py * yy) + (pz * zz);
float a = ABS(sqrt(px * px + py * py + pz * pz));
float b = ABS(sqrt(xx * xx + yy * yy + zz * zz));
dot /= (a * b);

Where px is the previous x acceleration and xx is the current, etc, 
looking for a value less than zero.

As an alternative, you can find a complete implementation here of shake detect code that was used in the DiceShaker application you can download for free to see how it works, along with comments and other pointers.

Because, as we all know, it’s always awesome when you can Shake That

h/t: iphonesdk!