2012-08-27 1 views
3

Android 게임을 제작 중이며 사용자가 기기를 왼쪽 또는 오른쪽으로 기울여야하는지 파악하고 싶습니다 (사람을 좌우로 움직일 때 Temple Run이 작동하는 것과 유사).Android Gyroscope verse Accelerometer

많은 튜토리얼과 예제를 읽었으며 샘플 애플리케이션을 만들었지 만 자이로 스코프와 가속도계에서 얻은 데이터의 양은 압도적입니다. 사용자가 장치를 기울이거나 방향을 결정하는 데 필요한 두 가지 하드웨어 세트가 필요합니까?

현재 응용 프로그램이 모든 미세한 움직임을 감지하고 있으며 이는 분명히 올바르지 않습니다.

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

기기의 세 축을 따라 회전하는 정도를 측정해야합니까? –

관련 문제