Game Development Reference
In-Depth Information
if (accelerometerAvailable) {
// normalize accelerometer values from [-10, 10] to [-1, 1]
// which translate to rotations of [-90, 90] degrees
float amount = Gdx.input.getAccelerometerY() / 10.0f;
amount *= 90.0f;
// is angle of rotation inside dead zone?
if (Math.abs(amount) <Constants.ACCEL_ANGLE_DEAD_ZONE) {
amount = 0;
} else {
// use the defined max angle of rotation instead of
// the full 90 degrees for maximum velocity
amount /= Constants.ACCEL_MAX_ANGLE_MAX_MOVEMENT;
}
level.bunnyHead.velocity.x =
level.bunnyHead.terminalVelocity.x * amount;
}
// Execute auto-forward movement on non-desktop platform
else if (Gdx.app.getType() != ApplicationType.Desktop) {
level.bunnyHead.velocity.x =
level.bunnyHead.terminalVelocity.x;
}
}
}
}
The new variable accelerometerAvailable is set once in the init() method, which
in turn is used to select the input mode in handleInputGame() . If an accelerometer
hardware is detected, the alternative input controls for player movement will be
used. In this case, however, we first normalize the accelerometer values to make
them stay in a range between -1.0f and 1.0f , which can now also be interpreted by
us as the percentage of desired movement in relation to the amount of tilt. The sign
of the percentage also nicely describes the direction of the horizontal movement. A
negative sign means movement to the left, whereas a positive sign means movement
to the opposite direction. The percentage currently maps a movement of 100 percent
in one direction to 90 degrees of tilting the screen in the same direction. It is very
inconvenient to have to turn the device over the full range of 180 degrees, which also
prevents fast player reactions.
 
Search WWH ::




Custom Search