Android 陀螺仪与加速度计 [英] Android Gyroscope verse Accelerometer

查看:46
本文介绍了Android 陀螺仪与加速度计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Android 游戏,我想弄清楚用户是将设备向左倾斜还是向右倾斜(类似于将人从一侧移动到另一侧时 Temple Run 的工作原理).

I am building an Android game and I want to figure out whether the user tilts the device to the left or the right (Similar to how Temple Run works when you move the man from side to side).

我阅读了许多教程和示例,并制作了示例应用程序,但是我从陀螺仪和加速度计返回的数据量是巨大的.我是否需要两套硬件才能确定用户是否倾斜设备以及向哪个方向倾斜?

I have read many tutorials and examples and I made sample applications but the amount of data I get back from both the Gyroscope and the Accelerometer are overwhelming. Would I need both sets of hardware to work out whether the user tilts the device and in which direction?

我当前的应用程序正在检测每一个轻微的运动,这显然是不正确的.

My current application is detecting every slight movement and that is obviously not correct.

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

    }

我最好只使用加速度计、陀螺仪还是两者都需要?

Would I be better off using just the accelerometer, just the gyroscope or would I need both?

推荐答案

这篇文章链接了两者之间的差异:Android 加速度计和陀螺仪

This post links to the differences between the two: Android accelerometer and gyroscope

http://diydrones.com/profiles/blogs/faq-whats-差异

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

文档也将有所帮助:http://developer.android.com/guide/topics/sensors/sensors_motion.html

根据我非常有限的经验,陀螺仪不断测量 x、y、z 旋转并不断更新.用于在游戏中驾驶汽车/飞机/角色.加速度计有点像一个wii-mote,用于摆动或拾取摇晃手势.

From my VERY limited experience, the gyro constantly measures the x, y, z rotation and keeps updating. Useful for steering a car/plane/character in a game. The accelerometer is a little more like a wii-mote, for swinging around or picking up a shake gesture.

这篇关于Android 陀螺仪与加速度计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆