简体   繁体   中英

LibGDX - Accelerometer Problems

I got some issues. I'm using 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. 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) ?

  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

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)

This is just an introduction of concept, you'll need to handle the scope of the variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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