簡體   English   中英

Android陀螺儀版加速度計

[英]Android Gyroscope verse Accelerometer

我正在構建一個Android游戲,我想弄清楚用戶是否向左或向右傾斜設備(類似於當你將人從一側移到另一側時Temple Run的工作方式)。

我已經閱讀了很多教程和示例,並且我制作了示例應用程序,但是從陀螺儀和加速度計中獲取的數據量都是壓倒性的。 我是否需要兩套硬件來確定用戶是否傾斜設備以及在哪個方向?

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

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

    }

我最好只使用加速度計,只使用陀螺儀還是我需要兩者?

這篇文章鏈接到兩者之間的差異: Android加速度計和陀螺儀

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,用於擺動或拾取搖動手勢。

根據我使用重力法測量加速度計的經驗,您可以將其用於x,y和z旋轉。 只需輸入谷歌“矢量方法加速度計”。 我使用這種方法用指南針來校正由於傾斜引起的坐標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM