I’m using libGDX to develop an Android game. The main character can be moved freely in the 2d plane by tilting the phone. The relevant part of my current implementation looks somewhat like this:
Vector2 mainCharacter;
float speedX, speedY;
public void render(float delta) {
speedX = 0.6f * speedX + 0.4f * -Gdx.input.getAccelerometerX();
speedY = 0.6f * speedY + 0.4f * -Gdx.input.getAccelerometerY();
mainCharacter.move(speedX, speedY);
}
This simple implementation works quite well as long as the player sits or stands upright. But there are two situations where this approach fails:
- If the player holds his phone almost horizontally (e.g. if he lies in bed, see fig. 1). Neither the computation of speedY nor the computation of speedX works properly in that situation.
- Inside a moving vehicle. When the vehicle speeds up or slows down, this has a strong impact on the values returned by Gdx.input.getAccelerometerX/Y.
So my question is: Is there anything that can be done to solve these two problems? How can I get satisfying results independent of whether the player is lying, standing or sitting? And is there a reasonable way to reduce the influence that the movement of a vehicle has on the output of the accelerometer?
fig. 1
