1. stratosb's Avatar
    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;
    03-24-2013 02:24 PM
  2. Patrick Schroedl's Avatar
    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.
    03-24-2013 03:25 PM
  3. stratosb's Avatar
    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.
    03-26-2013 08:04 AM

Similar Threads

  1. FVM Plus for Android
    By phonefusion in forum Android Apps
    Replies: 28
    Last Post: 08-22-2013, 02:26 PM
  2. Introducing Word Ball for Android
    By cidevelop in forum Android Games
    Replies: 0
    Last Post: 08-13-2011, 09:32 PM
  3. Replies: 1
    Last Post: 07-09-2011, 06:08 AM
  4. Best uses of accelerometers in Android?
    By braj in forum Android Games
    Replies: 5
    Last Post: 09-08-2010, 06:20 AM
  5. Does Android Central need more writers?
    By CNevius in forum Site Feedback & Help
    Replies: 2
    Last Post: 01-03-2010, 12:13 AM