Example: Detecting "Shake" With The Accelerometer

Quickly share your Android Code Snippets here...

Example: Detecting "Shake" With The Accelerometer

Postby snctln » Mon Jan 19, 2009 2:32 am

I have learned a lot from this forum so I decided it is time to give back

In my latest android game WordWrench I detect the "shaking" motion of the phone as follows (the following is pseudo code, I did not try to compile it)

MyActivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyActivity extends Activity
  2. {
  3.     private MyView m_myView;
  4.     private SensorManager m_sensorManager;
  5.  
  6.     /** Called when the activity is first created. */
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState)
  9.     {          
  10.         super.onCreate(savedInstanceState);
  11.  
  12.         m_myView = (MyView)findViewById(R.id.my_view);
  13.  
  14.         m_sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  15.         m_sensorManager.registerListener(m_myView,
  16.                 SensorManager.SENSOR_ACCELEROMETER,
  17.                 SensorManager.SENSOR_DELAY_GAME);
  18.     }
  19.  
  20.         @Override
  21.         protected void onPause()
  22.         {
  23.                 super.onPause();
  24.                
  25.                 m_sensorManager.unregisterListener(m_myView);
  26.         }
  27.  
  28.         @Override
  29.         protected void onResume()
  30.         {              
  31.                 super.onResume();
  32.                
  33.                 m_sensorManager.registerListener(m_myView,
  34.                         SensorManager.SENSOR_ACCELEROMETER,
  35.                         SensorManager.SENSOR_DELAY_GAME;
  36.         }
  37. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


MyView.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyView extends View implements SensorListener
  2. {
  3.     private float m_totalForcePrev; // stores the previous total force value
  4.  
  5.     // do your constructor and all other important stuff here
  6.     // make sure you set totalForcePrev to 0
  7.     // ...
  8.  
  9.         public void onAccuracyChanged(int arg0, int arg1)
  10.         {
  11.                 // I have no desire to deal with the accuracy events
  12.         }
  13.  
  14.         public void onSensorChanged(int sensor, float[] values)
  15.         {      
  16.                 if(sensor == SensorManager.SENSOR_ACCELEROMETER)
  17.                 {
  18.                         double forceThreshHold = 1.5f;
  19.                        
  20.                         double totalForce = 0.0f;
  21.                         totalForce += Math.pow(values[SensorManager.DATA_X]/SensorManager.GRAVITY_EARTH, 2.0);
  22.                         totalForce += Math.pow(values[SensorManager.DATA_Y]/SensorManager.GRAVITY_EARTH, 2.0);
  23.                         totalForce += Math.pow(values[SensorManager.DATA_Z]/SensorManager.GRAVITY_EARTH, 2.0);
  24.                         totalForce = Math.sqrt(totalForce);
  25.                        
  26.                         if((m_gameState == STATE_RUNNING) && (totalForce < forceThreshHold) && (m_totalForcePrev > forceThreshHold))
  27.                         {
  28.                                 doWrenchWord();
  29.                         }
  30.                        
  31.                         m_totalForcePrev = totalForce;
  32.                 }
  33.         }
  34.  
  35. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Feel free to let me know what you think of it o rif you have a different strategy when detecting the "shake" of hte device.


*EDIT*
I figured out the problem on my own... so this is just an example post now. @Mods: feel free to move it to a different sub forum if you want

[color=red][mod]I will, as this is now a Code Snippet :). Moved[/mod]

*EDIT #2*
thanks to this thread on the android developers group I was reminded of some of my old physics classes and I was able to simplify the coding of this function and also was able to get better overall result
android develoeprs
snctln
Freshman
Freshman
 
Posts: 5
Joined: Mon Jan 19, 2009 1:16 am

Top

Postby snctln » Fri Jan 23, 2009 4:37 pm

Hopefully I just made my last tweak to this code this morning. I was having numerous problems where the accelerometer would tell me that the phone was moving when it wasn't... I tracked it down to an issue with the vibrator in my ADP1. Whenever the vibrator goes off the Y and/or Z axis values become skewed for about a quarter of a second. In my case this ended up severely affecting gameplay for some users. So now I do all my calls to the phone vibrate function through a helper function that logs the time of the vibrate and the sensor changed code will only register the "Shake" motion if a certain amount of time has passed since the last vibrate.

Hopefully this helps future game devs that need vibrate and "shake" functionality in their games
snctln
Freshman
Freshman
 
Posts: 5
Joined: Mon Jan 19, 2009 1:16 am

Postby sudheer1437 » Wed Sep 16, 2009 9:38 am

thanks for sharing the code..
i dont have device to test...
i implemented it in my code..
have to see wads happens...
Once again thanks a lot for sharing the code...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby sudheer1437 » Wed Sep 16, 2009 9:48 am

once again Hai snctln,

before i implemented my own code...
but the app was crushing...
so i thought its problem with my code..

but i copy pasted ur code and made modifications according to my requirement..

but again the same old problem.. application crashes...
wen i debug..
i found the problem with below line in my code...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. myManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


the debug controller stops wen this line comes..
wads the problem....

plz help me out....
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Re: Example: Detecting "Shake" With The Accelerometer

Postby nareshkaushik36 » Thu Aug 05, 2010 2:00 pm

can anybody tell me how to test shake on emulator
response highly appreciable
nareshkaushik36
Freshman
Freshman
 
Posts: 3
Joined: Mon Jul 05, 2010 11:53 am

Re: Example: Detecting "Shake" With The Accelerometer

Postby limteckchai » Fri Aug 06, 2010 4:32 am

nareshkaushik36 wrote:can anybody tell me how to test shake on emulator
response highly appreciable


i dun noe how to shake it..but pressing keypad 7 & 9 when the keypad lock is off does rotate the screen
limteckchai
Experienced Developer
Experienced Developer
 
Posts: 92
Joined: Mon Jun 28, 2010 3:33 am

Top

Re: Example: Detecting "Shake" With The Accelerometer

Postby ashwani » Wed Mar 16, 2011 8:15 am

limteckchai wrote:
nareshkaushik36 wrote:can anybody tell me how to test shake on emulator
response highly appreciable


i dun noe how to shake it..but pressing keypad 7 & 9 when the keypad lock is off does rotate the screen

you can use openintents SensorSimulator application connect it to your emulator instance and start shaking.http://code.google.com/p/openintents/wiki/SensorSimulator....have fun
ashwani
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 10, 2011 12:43 pm

Re: Example: Detecting "Shake" With The Accelerometer

Postby Mike.Adams » Wed Apr 06, 2011 4:52 pm

thanks for the share im going to toy around with it
Hi I'm Mike. Checkout my source code, projects and work here and let me know what you think.

Feel free to also upload and submit your code/projects there for all to see and find!
Mike.Adams
Developer
Developer
 
Posts: 26
Joined: Mon Mar 07, 2011 9:19 pm
Location: New York

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 3 guests