• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android accelerometer ball

stratosb

Newbie
Jan 29, 2013
11
1
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.
Can someone help me please?
Thank you in advance.

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;
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones