Skip to content

Instantly share code, notes, and snippets.

@kkabdol
Last active March 10, 2016 15:57
Show Gist options
  • Save kkabdol/79e958c27fd6b51a6f9e to your computer and use it in GitHub Desktop.
Save kkabdol/79e958c27fd6b51a6f9e to your computer and use it in GitHub Desktop.
Mapping y deadzone for gamepad thumbstick
float Interpolate( float normalizedValue, float begin, float end )
{
// first check input values
assert( normalizedValue >= 0.0f );
assert( normalizedValue <= 1.0f );
assert( end > begin );
return ( normalizedValue * ( end - begin ) ) + begin;
}
void MapYDeadZone( Vec3 &input, float deadZone )
{
if ( deadZone >= 1.0f )
return;
// The dead zone is assumed to be zero close to the origin
// so we have to interpolate to find the right dead zone for
// our current value of X.
float actualDeadZone = Interpolate( fabs( input.x ), 0.0f, deadZone );
if ( fabs( input.y ) < actualDeadZone )
{
input.y = 0.0f;
return;
}
// Y is outside of the dead zone, but we still need to
// interpolate it so we don't see any popping.
// Map Y values [ actualDeadZone, 1.0f ] to [ 0.0f, 1.0f ]
float sign = ( input.y > 0 ) ? 1 : -1 ;
float normalizedY = ( fabs( input.y ) - actualDeadZone ) / ( 1.0f - actualDeadZone );
input.y = normalizedY * sign;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment