Skip to content

Commit

Permalink
Apply deadzone on analog inputs being interpreted as digital
Browse files Browse the repository at this point in the history
  • Loading branch information
Lactozilla committed Jul 30, 2024
1 parent ed740d4 commit ec1fd2a
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions source/Engine/Input/InputPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,22 @@ PRIVATE bool InputPlayer::IsControllerBindPressed(unsigned num) {
return ControllerState[num] == INPUT_STATE_PRESSED;
}

static float ApplyAxisDeadzone(float magnitude, float deadzone) {
if (deadzone >= 1.0) {
if (magnitude < 1.0)
return 0.0;
}

float scale = (magnitude - deadzone) / (1.0 - deadzone);

if (scale < 0.0)
scale = 0.0;
else if (scale > 1.0)
scale = 1.0;

return magnitude * scale;
}

static bool CheckControllerBindHeld(PlayerInputConfig& config, Controller* controller) {
if (controller == nullptr || !controller->Connected)
return false;
Expand All @@ -484,11 +500,16 @@ static bool CheckControllerBindHeld(PlayerInputConfig& config, Controller* contr
else if (bind->Type == INPUT_BIND_CONTROLLER_AXIS) {
ControllerAxisBind* axisBind = static_cast<ControllerAxisBind*>(bind);

float magnitude = controller->GetAxis(axisBind->Axis);
if (axisBind->AxisDeadzone != 0.0) {
magnitude = ApplyAxisDeadzone(magnitude, axisBind->AxisDeadzone);
}

if (axisBind->IsAxisNegative) {
if (controller->GetAxis(axisBind->Axis) < -axisBind->AxisDigitalThreshold)
if (magnitude < -axisBind->AxisDigitalThreshold)
return true;
}
else if (controller->GetAxis(axisBind->Axis) > axisBind->AxisDigitalThreshold)
else if (magnitude > axisBind->AxisDigitalThreshold)
return true;
}
else if (bind->Type == INPUT_BIND_CONTROLLER_BUTTON) {
Expand Down Expand Up @@ -531,25 +552,12 @@ PRIVATE bool InputPlayer::GetAnalogControllerBind(unsigned num, float& result) {
ControllerAxisBind* axisBind = static_cast<ControllerAxisBind*>(bind);

float magnitude = controller->GetAxis(axisBind->Axis);

if (axisBind->AxisDeadzone != 0.0) {
if (axisBind->AxisDeadzone == 1.0) {
if (magnitude < 1.0)
magnitude = 0.0;
}
else {
float scale = (magnitude - axisBind->AxisDeadzone) / (1.0 - axisBind->AxisDeadzone);

if (scale < 0.0)
scale = 0.0;
else if (scale > 1.0)
scale = 1.0;

magnitude *= scale;
}
result = ApplyAxisDeadzone(magnitude, axisBind->AxisDeadzone);
}
else {
result = magnitude;
}

result = magnitude;

return true;
}
Expand Down

0 comments on commit ec1fd2a

Please sign in to comment.