title: How do you calculate the rate of rotation using the accelerometer values in Android for a particular axis
body: <p>I am developing a simple game which involves a character moving up and down only along the Y axis. </p>
  
  <p>Currently I am using the accelerometer readings to change the Y velocity of the character. The game works fine but the biggest problem is that you have to keep the device horizontal in order to play the game properly. </p>
  
  <p>What I really want is to change the characters Y velocity only when there is a change in the rate of rotation along the Y axis. I need to be able to translate this rate of change into the Y velocity of the character. In this way it will not matter how much the device is tilted and the user can play the game while holding the device normally.</p>
  
  <p>Since the accelerometer is mandatory in every device, therefore older devices can run my game I want to be able to calculate this rate of change using the data retrieved from the accelerometer.</p>
  
  <p>I found this <a href="http://developer.nokia.com/community/wiki/How_to_get_pitch_and_roll_from_accelerometer_data_on_Windows_Phone" rel="nofollow" title="link">link</a> which explains how to get pitch and roll from accelerometer data.
  I used the exact code and came up with this,</p>
  
  <pre><code>final double alpha = 0.5;
      double fXg = 0;
      double fYg = 0;
      double fZg = 0;
      fXg = game.getInput().getAccelX() * alpha + (fXg * (1.0 - alpha));
      fYg = game.getInput().getAccelY() * alpha + (fYg * (1.0 - alpha));
      fZg = game.getInput().getAccelZ() * alpha + (fZg * (1.0 - alpha));
      double pitch = (Math.atan2(-fYg, fZg) * 180.0) / Math.PI;
      double roll = (Math.atan2(fXg, Math.sqrt(fYg * fYg + fZg * fZg)) * 180.0) / Math.PI;
      pitch = (pitch &gt;= 0) ? (180 - pitch) : (-pitch - 180);
  </code></pre>
  
  <p>With this piece of code I am unable grasp how to calculate the RATE of change.</p>
  
  <p>Am I going in the right direction with this or is this completely different from what I want?</p>
  
  <p>Also is it better if I just use the gyroscope instead of relying on the accelerometer?</p>
  
  <p>Thanx in advance.  </p>