2011-06-25 76 views
5

我已經完成了開發一個示例應用程序的幫助,在網上發佈的教程。我的目標是訪問加速度計並根據手機方向移動一個球。我成功地證明了程度。但我有兩個issesAndroid加速度計移動球

  1. 球是走出去​​的束縛屏幕
  2. 球運動的不順利(看起來消失,重新出現在屏幕上)

這裏是我的碼。我們需要做些什麼改變才能讓球順利,精確地運動,就像我們在很多比賽中看到的那樣。

public class Accelerometer extends Activity implements SensorEventListener{ 
    /** Called when the activity is first created. */ 
    CustomDrawableView mCustomDrawableView = null; 
    ShapeDrawable mDrawable = new ShapeDrawable(); 
    public static int x; 
    public static int y; 
     private Bitmap mBitmap; 
     private Bitmap mWood; 
    private SensorManager sensorManager = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 
     // Get a reference to a SensorManager 
     sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
     mCustomDrawableView = new CustomDrawableView(this); 
     setContentView(mCustomDrawableView); 
     // setContentView(R.layout.main); 

    } 

    // This method will update the UI on new sensor events 
    public void onSensorChanged(SensorEvent sensorEvent) 
    { 
     { 
      /* if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
       // the values you were calculating originally here were over 10000! 
       x = (int) Math.pow(sensorEvent.values[0], 2); 
       y = (int) Math.pow(sensorEvent.values[1], 2); 

      }*/ 

      if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) { 
        Display display = getWindowManager().getDefaultDisplay(); 
        int xmax = display.getWidth(); 
        int ymax = display.getHeight(); 
        x = (int) Math.pow(sensorEvent.values[1], 2); 
        y = (int) Math.pow(sensorEvent.values[2], 2); 
        if (x > xmax) { 
         x = xmax; 
        } else if (x < -xmax) { 
         x = -xmax; 
        } 
        if (y > ymax) { 
         y = ymax; 
        } else if (y < -ymax) { 
         y = -ymax; 
        } 

      } 
     } 
    } 

    // I've chosen to not implement this method 
    public void onAccuracyChanged(Sensor arg0, int arg1) 
    { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     // Register this class as a listener for the accelerometer sensor 
     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
       SensorManager.SENSOR_DELAY_GAME); 
     // ...and the orientation sensor 
     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
       SensorManager.SENSOR_DELAY_NORMAL); 
    } 

    @Override 
    protected void onStop() 
    { 
     // Unregister the listener 
     sensorManager.unregisterListener(this); 
     super.onStop(); 
    } 

    public class CustomDrawableView extends View 
    { 


     public CustomDrawableView(Context context) 
     { 
      super(context); 

      Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball); 
      final int dstWidth = 50; 
      final int dstHeight = 50; 
      mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true); 
      mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood); 

     } 

     protected void onDraw(Canvas canvas) 
     { 

      final Bitmap bitmap = mBitmap; 

      canvas.drawBitmap(mWood, 0, 0, null); 
      canvas.drawBitmap(bitmap, x, y, null); 

      invalidate(); 
     } 
    } 
} 
+0

..完美匹配您正在尋找的是[這裏](http://www.appsrox.com/android/tutorials/accelerometer-golf/) – user2230793

回答

10

這是不是你最後一個問題的問題here?!你應該編輯/擴展你原來的問題,而不是開始一個新的問題!

但基本上,爲了使其逼真,您希望通過使用x/y速度而不是僅僅改變位置來移動球。所以你想要一個能夠畫出球的循環,並且你想知道當前循環和前一個循環之間的時間差,那麼你可以使用簡單的運動學方程來計算出位置變化。

例如:

newspeed = oldSpeed + (acceleration * time) 
distance = (original speed*time) + (0.5 * acceleration * time^2). 

你在哪裏使用傳感器輸入來設置加速度,然後只需添加所計算的距離以球位置。


編輯 - 按要求編碼。

你幸運的是你發現了一個無聊的遊戲程序員!這不是完美的任何手段,但它爲你工作。你應該給自己買一本遊戲開發書,看看使用開放式GL,因爲它會好很多!

public class test2 extends Activity implements SensorEventListener{ 

CustomDrawableView mCustomDrawableView = null; 
ShapeDrawable mDrawable = new ShapeDrawable(); 
public float xPosition, xAcceleration,xVelocity = 0.0f; 
public float yPosition, yAcceleration,yVelocity = 0.0f; 
public float xmax,ymax; 
private Bitmap mBitmap; 
private Bitmap mWood; 
private SensorManager sensorManager = null; 
public float frameTime = 0.666f; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 

    //Set FullScreen & portrait 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    // Get a reference to a SensorManager 
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
      SensorManager.SENSOR_DELAY_GAME); 

    mCustomDrawableView = new CustomDrawableView(this); 
    setContentView(mCustomDrawableView); 
    // setContentView(R.layout.main); 

    //Calculate Boundry 
    Display display = getWindowManager().getDefaultDisplay(); 
    xmax = (float)display.getWidth() - 50; 
    ymax = (float)display.getHeight() - 50; 
} 

// This method will update the UI on new sensor events 
public void onSensorChanged(SensorEvent sensorEvent) 
{ 
    { 
     if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) { 
      //Set sensor values as acceleration 
      yAcceleration = sensorEvent.values[1]; 
      xAcceleration = sensorEvent.values[2]; 
      updateBall(); 
     } 
    } 
} 

private void updateBall() { 


    //Calculate new speed 
    xVelocity += (xAcceleration * frameTime); 
    yVelocity += (yAcceleration * frameTime); 

    //Calc distance travelled in that time 
    float xS = (xVelocity/2)*frameTime; 
    float yS = (yVelocity/2)*frameTime; 

    //Add to position negative due to sensor 
    //readings being opposite to what we want! 
    xPosition -= xS; 
    yPosition -= yS; 

    if (xPosition > xmax) { 
     xPosition = xmax; 
    } else if (xPosition < 0) { 
     xPosition = 0; 
    } 
    if (yPosition > ymax) { 
     yPosition = ymax; 
    } else if (yPosition < 0) { 
     yPosition = 0; 
    } 
} 

// I've chosen to not implement this method 
public void onAccuracyChanged(Sensor arg0, int arg1) 
{ 
    // TODO Auto-generated method stub 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
      SensorManager.SENSOR_DELAY_GAME); 
} 

@Override 
protected void onStop() 
{ 
    // Unregister the listener 
    sensorManager.unregisterListener(this); 
    super.onStop(); 
} 

public class CustomDrawableView extends View 
{ 
    public CustomDrawableView(Context context) 
    { 
     super(context); 
     Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball); 
     final int dstWidth = 50; 
     final int dstHeight = 50; 
     mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true); 
     mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood); 

    } 

    protected void onDraw(Canvas canvas) 
    { 
     final Bitmap bitmap = mBitmap; 
     canvas.drawBitmap(mWood, 0, 0, null); 
     canvas.drawBitmap(bitmap, xPosition, yPosition, null); 
     invalidate(); 
    } 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    // TODO Auto-generated method stub 
    super.onConfigurationChanged(newConfig); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
} 
} 
+0

嘿,我對這個網站非常陌生。我想關閉先前的問題,但無法在任何地方看到選項。可能是我沒有看到正確的地方。我如何接受答案? –

+0

嗨,我有如何接受答案。我已經接受你以前的答案。感謝 –

+0

你好,聽起來不錯。但我也是java新手。你能改變我的代碼,並把你解釋的邏輯 –

2

我會成立SENSOR_DELAY常數

SENSOR_DELAY_FASTEST 

看看是否能解決它。 這應該有助於口吃問題。 另外,我認爲你的邊界檢查是錯誤的,因爲xmax和ymax值是給定的display.getWidth/getHeight 也許你應該把它的BitMap寬度和高度邊界餵給xmax和ymax值。

+0

喜鮑勃,將其更改爲SENSOR_DELAY_FASTEST沒有什麼解決問題,事實上它使得運動速度太快,我的邊界問題也沒有解決 –