【问题标题】:Accessing orientation roll data is shaky for android访问方向滚动数据对于 android 来说是不稳定的
【发布时间】:2012-03-04 05:57:19
【问题描述】:

所以我正在为 android 创建一个游戏,它使用设备的滚动来设置主角的位置。每次运行 .onSensorChanged(event) 方法时都会更新位置。问题是,即使我运行我的应用程序并将手机放在桌面上,这些值也会发生显着变化(大约两度)。收集到的数据的敏感性似乎也不是很精确,因为角度的差异似乎每次变化都会增加约 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)。

感谢您的任何意见。

【问题讨论】:

  • 刚刚将其编辑到问题中

标签: android orientation pitch


【解决方案1】:

moving average 可以让您的数据动态平滑吗?

它会有点滞后,但不会引起注意。

另一方面,我不会使用Euler angles(比如roll)。

【讨论】:

  • 我已经合并了这个位置移动限制算法(现在包含在问题中)但是有太多的滞后。我没有遇到您链接中的问题。我不希望任何人在极端方向上使用设备玩游戏,因此不会出现角度行为不稳定的问题。
猜你喜欢
  • 2018-01-13
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多