1. ## Android accelerometer ball

Hello all,
I want to make a small android program to move a ball on the screen as the user rotates his/her phone right,left and up,down.
I have been searching through the internet for weeks and I am not able to find any help!

I have used the following code to get the values for yaw, pitch, roll from SensorManager.getRotationMatrix.
However I don't know how to calculate the position and/or distance that the ball has moved sideways as well as how fast it has moved.

private float [] m_lastAccelFields = new float[3];
private float [] m_lastMagFields = new float[3];
private float[] m_rotationMatrix = new float[9];
private float[] m_inclinationMatrix = new float[9];
private float[] m_orientation = new float[3];

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
m_lastAccelFields = event.values.clone();
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
m_lastMagFields = event.values.clone();
}
boolean proceed = SensorManager.getRotationMatrix(m_rotationMatrix, m_inclinationMatrix, m_lastAccelFields, m_lastMagFields);
if (proceed) {
sensorManager.getOrientation(m_rotationMatrix, m_orientation);
float yaw = m_orientation[0] * 57.2957795f;
float pitch = m_orientation[1] * 57.2957795f;
float roll = m_orientation[2] * 57.2957795f;
}
2. ## Re: Android accelerometer ball

You'll want to store two variables for the ball: the first is its position on the screen (in pixels), and the second is a velocity. In this context, the velocity would be a floating-point value pair such as (15.50, -2.61). As the device's rotation changes, use the new rotation to change the velocity. Tilting it to the right would change the X-velocity in a positive direction, tilting to the left would change it in the negative direction. Tilting it up would change the Y-velocity in the negative direction, tilting down would change it in the positive direction. The greater the tilt, the more you change the value. For example, a 10-degree tilt would add 2.0 to the velocity value, while a 45-degree tilt would add 6.0 or 8.0 to the velocity value (example numbers used; you'll have to tweak these)

You'll want a "game timer" of sorts which will check the rotation, update the velocity accordingly, and most importantly, move the ball by the number of pixels in the velocity. If the velocity is the (15.50, -2.61) that I used, you take the position of the ball and add 15.50 to the X coordinate and subtract -2.61 from the Y coordinate. You'll want to implement some sort of collision detection if you want the ball to collide with the edges of the screen and stay within visible bounds.

I don't have time today to put any code snippets together, but that's how I would approach the problem.
3. ## Re: Android accelerometer ball

Thank you Patrick,
rotating the phone gives you the angle of the tilt. Is it correct to add this value, which is the angle, to the ball's position?
Also that angle is in radians, do I need to convert it to degrees first?
Thank you.