Allowing Both Landscape Options And Accelerometer With Libgdx
I've completed the SimpleApp tutorial for libgdx. I have changed the controls for the game to use the accelerometer instead of touch input, in the following way: In MainActivity.j
Solution 1:
From the Libgdx accelerometer wiki (specifically this section), the accelerometer data is always relative to portrait orientation.
You are expected to use the result of Gdx.input.getNativeOrientation()
or Gdx.input.getRotation() and flip the results as necessary.
The wiki and the javadoc are a bit inconsistent with respect to "native" orientations, but it does look like the code does what the wiki says ("portait" is always the default orientation, even on tablets where Android defaults to landscape).
Solution 2:
Here's my solution, based on P.T.s answer:
float rot = Gdx.input.getRotation() - 180;
float acc = Gdx.input.getAccelerometerY();
bucket.x += acc * BUCKET_SPEED * Gdx.graphics.getDeltaTime() * (rot / Math.abs( rot ) * -1);
Now I can turn my device 180 degrees and the game will follow, and the accelerometer controls works the same way both ways.
Post a Comment for "Allowing Both Landscape Options And Accelerometer With Libgdx"