Question

How do people create games which make the object move when we turn or rotate our phone in different directions (accelerating a car, for example)? Do they use readings from the accelerometer and gyroscope or do they use OpenGL in Android?

Was it helpful?

Solution

They are likely using the accelerometer, because it is widely available on a large variety of devices.

You might want to take a look at this tutorial: http://blog.androgames.net/85/android-accelerometer-tutorial/

And here's a demo of the accelerometer feature from Google: http://developer.android.com/resources/samples/AccelerometerPlay/src/com/example/android/accelerometerplay/AccelerometerPlayActivity.html

OTHER TIPS

This example shows a simple calculation for the 3 directions value
pitch, orientation and azimuth.

if(SensorManager.getRotationMatrix(R, null, AccelerometerValues_last, MagneticFieldValues_last))
{
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, remapR);
SensorManager.getOrientation(remapR, orientationValues);

Matrix.multiplyMV(orientationVector, 0, remapR, 0, sZVector, 0);
pitch = (float) (-Math.atan2(orientationVector[1], orientationVector[2]) * RADIANS_TO_DEGREES);

Matrix.multiplyMV(orientationVector, 0, remapR, 0, sZVector, 0);
orientation = (float) (-Math.atan2(orientationVector[0], orientationVector[1]) * RADIANS_TO_DEGREES);

Matrix.invertM(remapR_inv, 0, remapR, 0);
Matrix.multiplyMV(azimuthVector, 0, remapR_inv, 0, sZVector, 0);
azimuth = (float) (180 + Math.atan2(azimuthVector[0], azimuthVector[1]) * RADIANS_TO_DEGREES);
}     

The complete code in the article How to use Android sensors?. FYI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top