Probing device sensors

Handling input devices is essential to any application, but probing sensors is important for the smartest one! The most spread sensor among Android game applications is the accelerometer.

An accelerometer, as its name suggests, measures the linear acceleration applied to a device. When moving a device up, down, left, or right, the accelerometer gets excited and indicates an acceleration vector in 3D space. Vector is expressed in relation to the screen's default orientation. The coordinate system is relative to the device's natural orientation:

  • X axis points to the right
  • Y points up
  • Z points from back to front

Axes become inverted if the device is rotated (for example, Y points left if the device is rotated 90 degrees clockwise).

A very interesting feature of accelerometers is that they undergo a constant acceleration: gravity, around 9.8m/s2 on earth. For example, when lying flat on a table, acceleration vector indicates -9.8 on the Z-axis. When straight, it indicates the same value on Y axis. So assuming the device position is fixed, device orientation on two axes in space can be deduced from the gravity acceleration vector. A magnetometer is still required to get full device orientation in 3D space.

Tip

Remember that accelerometers work with linear acceleration. They allow detecting the translation when the device is not rotating and partial orientation when the device is fixed. However, both movements cannot be combined without a magnetometer and/or gyroscope.

So we can use the device orientation deduced from the accelerometer to compute a direction. Let's now see how to apply this process in DroidBlaster.

Note

The resulting project is provided with this book under the name DroidBlaster_Part15.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.228.40