Game Development Reference
In-Depth Information
the x and y values of the joystick are both slightly less than the dead zone value,
you won't get any movement even though you have moved the joystick beyond
the 10% threshold.
Another issue is that this approach does not use the full range of values available.
There is a smooth ramp from 10% speed to 100% speed, but everything below
10% is lost. We would prefer a solution where the movement can be anywhere
from 0% speed to 100% speed. Or in other words, we want valid movement input
to start at slightly higher than 0% speed, rather than slightly higher than 10%
speed. This will require modifying the range so an “original” value of 55% speed,
which is half way between 10% speed and 100% speed, is instead filtered into
50% speed.
To solve this problem, rather than treating the x and y values as individual com-
ponents, we can treat the joystick input as a 2D floating point vector. Then vector
operations can be performed in order to filter for the dead zone.
float
deadZone
= 3000
float
maxValue
= 32677
Vector2
joy
= get joystick input
float
length
=
joy
.length()
// If the length < deadZone, we want no input
if
length
<
deadzone
joy
.
x
= 0
joy
.
y
= 0
else
// Calculate the percent between the deadZone
and maxValue circles
float
pct
= (
length
-
deadZone
) / (
maxValue
-
deadZone
)
// Normalize vector and multiply to get correct
final value
joy
=
joy
/
length
joy
=
joy
*
maxValue
*
pct
end