Game Development Reference
In-Depth Information
form of input for character and camera movement. In particular we want to add in
support for the left thumbstick, which is the player movement control. This is done as
follows:
float thumbY = state.Gamepad.sThumbLY;
float magnitude = abs(thumbY);
if (magnitude >
XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
{
if (thumbY > 0)
_sidePressed = -1;
else
_sidePressed = 1;
}
else
{
_sidePressed = 0;
}
To do this, we can extract the vertical (y axis) value from the state object using the
sThumbLY field. If you want to get the X component from the right thumbstick, you
should use the sThumbRX field. You can determine which stick and axis you're re-
trieving from the name.
This field gives us a SHORT object that defines the location of the stick, with the pos-
itive values pointing up, and the negative values pointing down. This is, however,
an analog device and the value you get when the stick isn't being touched is often
non-zero. This is where the deadzone comes into play. The deadzone is a threshold
within which all input is ignored. This allows the players to rest their thumbs on the
stick and not worry about the game reacting to the tiny movements that are created.
XInput provides standard values for the deadzone on each thumbstick, so it's recom-
mended that you make use of those defined constants for a good experience.
To calculate if the thumbstick is sitting within the deadzone, we get the absolute
value of the y axis and check if this value is greater than the threshold. If we don't get
the absolute value first, we'll end up ignoring any negative values, which represents
the entire down direction. Once we've filtered out values within the deadzone we can
Search WWH ::




Custom Search