Created
April 9, 2011 04:45
-
-
Save SriramBms/911140 to your computer and use it in GitHub Desktop.
Code snippet to determine the total force applied while shaking/moving an Android phone- for Motion based Games
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is accomplished by implementing android.hardware.SensorListener in an Activity | |
// You will also need to register for SENSOR_SERVICE using android.hardware.SensorManager | |
// The rules of Physics in 3-Dimensional space apply! | |
public void onSensorChanged(int sensor, float[] values) { | |
if (sensor == SensorManager.SENSOR_ACCELEROMETER) { | |
double sensitivity = 1.0f; //tweak this to ignore slight hand movements. | |
double forceMagnitude = 0.0f; | |
forceMagnitude += Math.pow(values[SensorManager.DATA_X], 2.0); | |
forceMagnitude += Math.pow(values[SensorManager.DATA_Y], 2.0); | |
forceMagnitude += Math.pow(values[SensorManager.DATA_Z], 2.0); | |
forceMagnitude = Math.sqrt(forceMagnitude); | |
forceMagnitude = forceMagnitude / SensorManager.GRAVITY_EARTH; | |
/* use the sensitivity setting in conjunction with the calculated | |
forceMagnitude to perform on-screen actions like say, moving a ball | |
a certain distance etc */ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment