Adding alternative input controls
The last and rather short topic in this chapter will show you how to use the peripheral devices using the example of accelerometers, which are very common types of sensors in today's smartphones and tablets. Accelerometers are subsystems that reliably and accurately detect and measure acceleration, tilt, shock, and vibration. Basically, we just need to read the sensor data that is being measured by the accelerometer hardware and later translate the data into values and/or ranges suitable for our game.
Accelerometers in Android and iOS devices are exposed by LibGDX using the three axes x, y, and z, which can be queried via the Gdx.input
module. For instance, the current acceleration value for the x axis can be easily retrieved as follows:
float ax = Gdx.input.getAccelerometerX();
This is great, but what does the value that we just stored in ax
actually mean? Moreover, what will influence it? So, to better understand how sensors work, we need to know what...