Vector2 GetStickDirection ( short sX, short sY, short threshold )
{
float tX = sX;
float tY = sY;
//determine how far the controller is pushed
float magnitude = (float) Math.Sqrt(tX * tX + tY * tY);
//determine the direction the controller is pushed
var ret = Vector2.Zero;
if (magnitude > 0.0f) {
ret.X = tX/magnitude;
ret.Y = tY/magnitude;
}
float normalizedMagnitude = 0;
//check if the controller is outside a circular dead zone
if (magnitude > threshold) {
//clip the magnitude at its expected maximum value
if (magnitude > 32767) magnitude = 32767;
//adjust magnitude relative to the end of the dead zone
magnitude -= threshold;
//optionally normalize the magnitude with respect to its expected range
//giving a magnitude value of 0.0 to 1.0
normalizedMagnitude = magnitude / (32767 - threshold);
} else { //if the controller is in the deadzone zero out the magnitude
magnitude = 0.0f;
normalizedMagnitude = 0.0f;
}
ret *= normalizedMagnitude;
return ret;
}