Android的加速度计的困难 [英] Android accelerometer difficulties

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

问题描述

我在的Andr​​oid游戏开发的初学者,我开发一个小游戏。 我面临着运动传感器的一些困难:加速度

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,

这是我的code:

 @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;
          }

}

所以,我的code ++工程(有点),但是我的角色的动作是不平滑的。有时候,我的手机倾斜,但我的角色不动......

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];
 }

您可能需要使用该值重力[] 找出手机中的倾斜。此外,玩弄最终浮动字母的值:接近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天全站免登陆