Android 加速度计难点 [英] Android accelerometer difficulties

查看:39
本文介绍了Android 加速度计难点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 游戏开发的初学者,我正在开发一个小游戏.我在使用运动传感器:加速度计时遇到了一些困难.

I'm a beginner in Android game Development, and I developing a small Game. I am facing some difficulties with the Motion Sensor: Accelerometer.

此游戏处于横向模式.我希望如果我的手机向右倾斜,我的角色也会正确.(左边同样的事情)当我停止倾斜时,游戏中的角色应该停止移动.

this game is in Landscape mode. I want if my phone tilts in right, my character also goes right. ( same thing for left ) And when I stop tilting Character in the game should stop moving.

但是我不太了解加速度计的操作,

But i don't understand really good the operation of the accelerometer,

这是我的代码:

 @Override
public void onSensorChanged(SensorEvent event) {
     synchronized (this) {
        long penchement = (long) (event.values[1]- 0.5);
          if(penchement>0){
              if(penchement>lastUpdate)
                  lastUpdate=penchement;
              if(penchement>0.2){
                  booldroite=true; // boolean for going right
                  boolgauche=false; // boolean for going left
              }
              if(lastUpdate-penchement<0.2)
                  booldroite=false;
          }
          else{
              if(penchement<lastUpdate)
                  lastUpdate=penchement;
              if(penchement<-0.2){
                  boolgauche=true;
                  booldroite=false;
              }
              if(lastUpdate+penchement>-0.2)
                  boolgauche=false;
          }

}

所以我的代码有效(有点),但我角色的动作一点也不流畅.有时我的手机倾斜,但我的角色不动...

So my code works (a bit), but my character's moves aren't smooth at all. Sometimes my phone tilts but my Characters doesn't move...

如果你能帮助我,非常感谢你.

Thank you very much in advance if you can help me.

推荐答案

传感器读数本质上是嘈杂的,这就是角色移动闪烁的原因.

Sensor readings are noisy by nature, that's the reason for the flickering movement of your character.

您需要在读数后实施某种低通滤波器.SensorEvent 中有一个基本的低通滤波器示例.>

You will need to implement some kind of low pass filter after the readings. There's a basic low pass filter example in SensorEvent.

 public void onSensorChanged(SensorEvent event) {
      
      // alpha is calculated as t / (t + dT)
      // with t, the low-pass filter's time-constant
      // and dT, the event delivery rate

      final float alpha = 0.8;

      gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
      gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
      gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

      linear_acceleration[0] = event.values[0] - gravity[0];
      linear_acceleration[1] = event.values[1] - gravity[1];
      linear_acceleration[2] = event.values[2] - gravity[2];
 }

您可能想使用 gravity[] 中的值来找出手机的倾斜度".此外,调整 final float alpha 的值:接近 1.0 的值会提高平滑度,而接近 0.0 的较小值会获得更嘈杂的读数,但响应速度更快.

You might want to use the values in gravity[] to find out the 'tilt' of the phone. Also, play around with the value of final float alpha: Values near 1.0 will improve the smoothness, while smaller values near 0.0 will obtain noisier readings, but have a faster response.

好机会!

这篇关于Android 加速度计难点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆