Game Development Reference
In-Depth Information
It is good practice on mobile devices to ensure that we only enable parts
of the hardware when we are actually using them, since this helps to
conserve battery power. In the case of accelerometers, the power use
is probably so small that it is insignificant, but this is an area of mobile
game programming that is always worth keeping in mind.
Reading accelerometer input
Finding the current accelerometer input values is really very simple. Marmalade
provides three functions which return the current accelerometer value for each
axis. These functions are called s3eAccelerometerGetX , s3eAccelerometerGetY ,
and s3eAccelerometerGetZ . Unsurprisingly, they return the current value of the
accelerometer for the specified axis.
The values returned by these functions use a value of 1000 (though we should use
the handy definition S3E_ACCELEROMETER_1G to avoid magic numbers in our code!)
to represent an acceleration equivalent to normal Earth gravity.
When a sharp, quick movement is made to the device, the forces being applied to
it will be greater than the normal gravitational force. In this case, the magnitude
of the vector formed from the accelerometer values will be greater than S3E_
ACCELEROMETER_1G . This can be a useful way of detecting whether the user has
been shaking the device.
If the device were to be horizontally on a table, we should get a value of 0 returned
for both the X and Y axes, and -1000 for the z axis, since gravity acts downwards!
As we rotate the device, the values returned will form a vector showing the direction
in which gravity is acting, and we can then use this to determine the orientation of
the device.
Using some trigonometry, we can work out the angle of tip around the x axis
(forward/backward) and Y-axis (left/right). The angle around the X-axis can be
found by taking the arc tan of the Y-accelerometer value divided by the Z value.
The angle around the y axis is a little trickier. First we have to find the length of
the accelerometer vector projected onto the YZ plane, then we can find the arc tan
of the X-accelerometer value divided by the projected length.
 
Search WWH ::




Custom Search