2012-08-27 103 views
3

我正在構建一款Android遊戲,我想弄清楚用戶是否將設備向左或向右傾斜(類似於Temple Run如何將人從左右移動到另一側)。Android陀螺儀詩歌加速度計

我已經閱讀了很多教程和示例,並且我做了示例應用程序,但是從陀螺儀和加速度計返回的數據量都很大。我是否需要兩套硬件來確定用戶是否傾斜設備並朝哪個方向移動?

我目前的應用程序正在檢測每一個輕微的移動,這顯然是不正確的。

public class Main extends Activity { 

    private SensorManager mSensorManager; 
    private float mAccel; // acceleration apart from gravity 
    private float mAccelCurrent; // current acceleration including gravity 
    private float mAccelLast; // last acceleration including gravity 
    private RelativeLayout background; 
    private Boolean isleft = true; 
/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    this.background = (RelativeLayout) findViewById(R.id.RelativeLayout1); 
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); 
    mAccel = 0.00f; 
    mAccelCurrent = SensorManager.GRAVITY_EARTH; 
    mAccelLast = SensorManager.GRAVITY_EARTH; 

    /* float x1, x2, y1, y2; 
    String direction; 
    switch(event.getAction()) { 
      case(MotionEvent.ACTION_DOWN): 
       x1 = event.getX(); 
       y1 = event.getY(); 
       break; 
      case(MotionEvent.ACTION_UP) { 
       x2 = event.getX(); 
       y2 = event.getY(); 
       float dx = x2-x1; 
       float dy = y2-y1; 

        // Use dx and dy to determine the direction 
       if(Math.abs(dx) > Math.abs(dy)) { 
        if(dx>0) directiion = "right"; 
        else direction = "left"; 
       } else { 
        if(dy>0) direction = "down"; 
        else direction = "up"; 
       } 
      } 
      }*/ 
} 

private final SensorEventListener mSensorListener = new SensorEventListener() { 

    public void onSensorChanged(SensorEvent se) { 

     float x = se.values[0]; 
     float y = se.values[1]; 
     float z = se.values[2]; 

     if((mAccelLast<mAccelCurrent)&&(isleft == true)){ 
      background.setBackgroundResource(R.drawable.bg_right); 
      isleft = false; 
     } 
     if((mAccelLast>mAccelCurrent)&&(isleft == false)){ 
      background.setBackgroundResource(R.drawable.bg_left); 
      isleft = true; 
     } 
     mAccelLast = mAccelCurrent; 
     mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z)); 
     float delta = mAccelCurrent - mAccelLast; 
     Log.d("FB", "delta : "+delta); 
     mAccel = mAccel * 0.9f + delta; // perform low-cut filter 
    // Log.d("FB", "mAccel : "+mAccel); 

    } 

我只會使用加速度計,只是陀螺儀或我需要兩者嗎?

回答

4

這個帖子鏈接到兩者之間的區別:Android accelerometer and gyroscope

http://diydrones.com/profiles/blogs/faq-whats-the-difference

http://answers.oreilly.com/topic/1751-mobile-accelerometers-and-gyroscopes-explained/

的文件也將有所幫助:http://developer.android.com/guide/topics/sensors/sensors_motion.html

從我的經驗非常有限,陀螺不停措施x,y,z旋轉並不斷更新。用於在遊戲中轉向汽車/飛機/角色。加速度計更像是一個wii-mote,用於擺動或拾取抖動手勢。

+0

感謝RossC,我確信這些將有助於 –

3

從我的加速度計使用重力法的經驗中,您可以將它用於x,y和z旋轉。只需鍵入谷歌「矢量方法加速度計」。我已經使用這種方法與指南針來糾正由於傾斜引起的座標。

+0

如果我需要獲得關於沿設備的三個軸的旋轉度的度量,那麼如何? –

相關問題