3

我正在构建一个 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);

    }

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

4

2 回答 2

4

这篇文章链接了两者之间的区别: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,用于摆动或拾取摇晃手势。

于 2012-08-27T11:01:26.810 回答
3

根据我使用重力法使用加速度计的经验,您可以将其用于 x、y 和 z 旋转。只需输入谷歌“矢量法加速度计”。我已经将此方法与指南针一起使用,以校正由于倾斜引起的坐标。

于 2012-11-06T10:21:04.317 回答