简体   繁体   English

LibGDX-加速度计问题

[英]LibGDX - Accelerometer Problems

I got some issues. 我遇到了一些问题。 I'm using LibGDX. 我正在使用LibGDX。

  1. When I hit an object, my Fighter is supposed to fall to the Ground. 当我碰到一个物体时,我的战斗机应该掉到地上。 BUT, because of the Accelerometer, i can still move it up and down eventhough it died. 但是,由于有了加速度计,即使它死了,我仍然可以上下移动它。 The Scroller on the x-Axis though stops. 尽管x轴上的滚动条停止了。 Question: (FIXED) How can i make the Accelerometer stop, after Ive been hit, and make the Fighter accelerate to the ground (in absence of the accelerometer, all by itself) ? 问题:(已修复)在被Ive击打后,我如何才能使加速度计停止运动,并使战斗机加速至地面(在没有加速度计的情况下,仅靠加速度计)?

  2. When I move the Fighter up and down, the transition from "moving up" to "moving down" isnt smooth. 当我上下移动战斗机时,从“向上移动”到“向下移动”的过渡并不顺利。 It seems like it laggs. 好像迟钝了。 Question: How can I Increase the Sensitivity of my Accelerometer? 问题:如何增加加速度计的灵敏度?

  3. Also, When I tilt the Phone further, the velocity stay the same. 另外,当我进一步倾斜手机时,速度保持不变。 Question: How can i increase the velocity, in relation to the tilt degree ? 问题:如何相对于倾斜度提高速度?

Thank You ! 谢谢 !

The Codes are Below: 代码如下:

Accelerometer Code: 加速度计代码:

       float accelY = Gdx.input.getAccelerometerY();



      if (Gdx.input.getAccelerometerY() < 5){
         velocity.y = -40;
      }


      if (Gdx.input.getAccelerometerY() > 5){
         velocity.y = 40;
      }

When Fighter hits an Object and dies: 当战斗机击中一个物体并死亡时:

              public void die() {
      isAlive = false;
      velocity.y = 0;
      acceleration.y = 460;
   }

You Need to wrap the accel. 您需要包装加速器。 function with the Alive variable 与Alive变量一起使用

if(isAlive)
{
    if (Gdx.input.getAccelerometerY() < 5)
    {
        velocity.y = -40;
    }

    if (Gdx.input.getAccelerometerY() > 5)
    {
        velocity.y = 40;
    }
}

For Q2, 对于第二季度

if (Gdx.input.getAccelerometerY() < 5){
    velocity.y = -40;
}


if (Gdx.input.getAccelerometerY() > 5){
    velocity.y = 40;
}

The reason it's not smooth is because you did not interpolate the movement. 它不平滑的原因是您没有对运动进行插值。 One workaround is: 一种解决方法是:

if (Math.abs(Gdx.input.getAccelerometerY()) > 5){
    YFactor = Gdx.input.getAccelerometerY();
}

//somewhere in your game loop //游戏循环中的某个位置

if(YFactor >0)
{
    if(velocity.y) < YFactor*8{ //tweak this value
        velocity.y += YFactor; //tweak this value
    }
}
else
{
    //reverse above logic
}

What this does is that it will keep - the velocity or +, up until YFactor * 8 (which is what your Q3 needs) 这是要一直保持-速度或+,直到YFactor * 8(这就是Q3所需要的)

This is just an introduction of concept, you'll need to handle the scope of the variables. 这只是概念的介绍,您需要处理变量的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM