Game Development Reference
In-Depth Information
If all this sounds like too much scary math, the following code snippet does it all for
us. Note than when calculating the rotation around the x axis using the IwGeomAtan2
function, we negate both the Y- and Z-accelerometer values in order to yield a more
usable result range, with 0 degrees returned when the device is level and increasing
values when tipped away from the user.
iwangle xAngle = IwGeomAtan2(-accY, -accZ);
int32 lYZProjection = (int32) sqrtf((float) ((accY * accY) +
(accZ * accZ)));
iwangle yAngle = IwGeomAtan2(accX, lYZProjection);
Smoothing accelerometer input
One problem we will encounter when using the accelerometer for input is that the
values returned from it tend to be a bit "jumpy". Even the steadiest hand will be
unable to hold the device still enough to see a steady value being returned from the
accelerometer. This can cause your game to register movements when you don't
want it to.
A common approach for solving this problem is to smooth the accelerometer values
by combining the current readings with the previous readings. The easiest way of
doing this is shown in the following code:
int32 accX = 0, accY = 0, accZ = 0;
int32 lSmoothFactor = IW_GEOM_ONE / 4;
// The following loop shows how we generate the smoothed accelerometer
// inputs. In a real application the code within the loop would be
called once
// per game frame.
while (TRUE)
{
int32 deltaX = s3eAccelerometerGetX() - accX;
int32 deltaY = s3eAccelerometerGetY() - accY;
int32 deltaZ = s3eAccelerometerGetZ() - accZ;
accX += IW_FIXED_MUL(lSmoothFactor, deltaX);
accY += IW_FIXED_MUL(lSmoothFactor, deltaY);
accZ += IW_FIXED_MUL(lSmoothFactor, deltaZ);
}
 
Search WWH ::




Custom Search