Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding error when computing the emulated control stick values #356

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions Source/Core/Core/HW/GCPadEmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,20 @@ GCPadStatus GCPad::GetInput() const

// sticks
m_main_stick->GetState(&x, &y);
pad.stickX =
static_cast<u8>(GCPadStatus::MAIN_STICK_CENTER_X + (x * GCPadStatus::MAIN_STICK_RADIUS));
pad.stickY =
static_cast<u8>(GCPadStatus::MAIN_STICK_CENTER_Y + (y * GCPadStatus::MAIN_STICK_RADIUS));
// immediately compute and cast the absolute offsets to u8 to avoid rounding inconsistencies
// for example, when x or y is 0.5/-0.5, the absolute offset of that axis should always be 63, never 64
u8 stickXAbsoluteOffset = static_cast<u8>(abs(x) * GCPadStatus::MAIN_STICK_RADIUS);
u8 stickYAbsoluteOffset = static_cast<u8>(abs(y) * GCPadStatus::MAIN_STICK_RADIUS);
pad.stickX = static_cast<u8>(GCPadStatus::MAIN_STICK_CENTER_X + sign(x) * stickXAbsoluteOffset);
pad.stickY = static_cast<u8>(GCPadStatus::MAIN_STICK_CENTER_Y + sign(y) * stickYAbsoluteOffset);

m_c_stick->GetState(&x, &y);
pad.substickX =
static_cast<u8>(GCPadStatus::C_STICK_CENTER_X + (x * GCPadStatus::C_STICK_RADIUS));
pad.substickY =
static_cast<u8>(GCPadStatus::C_STICK_CENTER_Y + (y * GCPadStatus::C_STICK_RADIUS));
// immediately compute and cast the absolute offsets to u8 to avoid rounding inconsistencies
// for example, when x or y is 0.5/-0.5, the absolute offset of that axis should always be 63, never 64
u8 substickXAbsoluteOffset = static_cast<u8>(abs(x) * GCPadStatus::C_STICK_RADIUS);
u8 substickYAbsoluteOffset = static_cast<u8>(abs(y) * GCPadStatus::C_STICK_RADIUS);
pad.substickX = static_cast<u8>(GCPadStatus::C_STICK_CENTER_X + sign(x) * substickXAbsoluteOffset);
pad.substickY = static_cast<u8>(GCPadStatus::C_STICK_CENTER_Y + sign(y) * substickYAbsoluteOffset);

// triggers
m_triggers->GetState(&pad.button, trigger_bitmasks, triggers);
Expand Down