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加速度計とジャイロスコープの2つの違いにリンクしています

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リモコンに少し似ており、振り回したり、シェイクジェスチャを拾ったりします。

于 2012-08-27T11:01:26.810 に答える
3

重力法を使用した加速度計の私の経験から、x、y、zの回転に使用できます。グーグル「ベクトル法加速度計」と入力するだけです。傾きによる座標を補正するために、この方法をコンパスで使用しました。

于 2012-11-06T10:21:04.317 に答える