0

だから私は、デバイスのロールを使用して主人公の位置を設定するアンドロイド用のゲームを作成しています。.onSensorChanged(event) メソッドが実行されるたびに位置が更新されます。問題は、アプリを実行して電話を卓上に置いても、値が大幅に (約 2 度) 変化することです。角度の違いは変化ごとに約 0.4 度ずつ増加しているように見えるため、収集されたデータの感度もあまり正確ではないようです。使用する

Log.e(TAG, "roll: " + event.values[2]); 

順次次数出力は次のようになります。

roll: 2.265
roll: 2.265
roll: 2.265
roll: 1.843
roll: 2.265
roll: 2.265
roll: 2.75
roll: 2.265

画面内でのキャラクターの動きを制限するためにこのアルゴリズムも実装しましたが、それでは十分ではなく、現在のロール データに関してキャラクターの動きの速度と応答性を大幅に制限します (キャラクターの配置を可能な限りロールに関して1-1)。

public void onSensorChanged(SensorEvent event)
{
    if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
    {
    float newX = -(int)event.values[2] * CCDirector.sharedDirector().displaySize().width/10.0f + 
          CCDirector.sharedDirector().displaySize().width/2.0f;

            sam.updatePosition(newX);

    Log.e(TAG, "roll: " + event.values[2]);
     }
}

public void updatePosition(float newX)
{
    float winSizeX = CCDirector.sharedDirector().displaySize().width;
    float contentWidth = this.sprite.getContentSize().width;

    //tries to eliminate jumpy behaviour by the character by limiting his speed
    double tolerance = 0.01;
    if (Math.abs(newX - this.sprite.getPosition().x) > winSizeX * tolerance) 
        newX = this.sprite.getPosition().x - ((this.sprite.getPosition().x - newX) * 0.1f);

    this.sprite.setPosition(newX, this.sprite.getPosition().y);
}

これが正常なのか、それともテストしている電話(Sony Xperia Play)なのか疑問に思っています。

ご意見ありがとうございます。

4

1 に答える 1

0

その場でデータをスムーズにするために移動平均はどうですか?

少し遅れますが、目立たないでしょう。

一方、オイラー角(ロールなど)は使用しません。

于 2012-03-04T08:46:34.273 に答える