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

Scale mouse/joystick sensitivity depending on FOV #11007

Merged
merged 1 commit into from Mar 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/client/game.cpp
Expand Up @@ -823,6 +823,8 @@ class Game {
const NodeMetadata *meta);
static const ClientEventHandler clientEventHandler[CLIENTEVENT_MAX];

f32 getSensitivityScaleFactor() const;

InputHandler *input = nullptr;

Client *client = nullptr;
Expand Down Expand Up @@ -2334,7 +2336,6 @@ void Game::checkZoomEnabled()
m_game_ui->showTranslatedStatusText("Zoom currently disabled by game or mod");
}


void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
{
if ((device->isWindowActive() && device->isWindowFocused()
Expand Down Expand Up @@ -2370,6 +2371,18 @@ void Game::updateCameraDirection(CameraOrientation *cam, float dtime)
}
}

// Get the factor to multiply with sensitivity to get the same mouse/joystick
// responsiveness independently of FOV.
f32 Game::getSensitivityScaleFactor() const
{
f32 fov_y = client->getCamera()->getFovY();

// Multiply by a constant such that it becomes 1.0 at 72 degree FOV and
// 16:9 aspect ratio to minimize disruption of existing sensitivity
// settings.
return tan(fov_y / 2.0f) * 1.3763818698f;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this constant should be a DEFINE in order to understand what is this directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it hard to give such a constant a meaningful name so I thought it was more clear just to multiply with it and explain its purpose in the comments. If std::tan was constexpr in C++11 I could do the something like following which would be more clear about its purpose and where it comes from:

static constexpr float default_fov_y = 72.0f * M_PI / 180.0f;
static constexpr float constant = 1.0f / tan(default_fov_y / 2.0f);

This works for gcc but as far as I know std::tan isn't is not guaranteed to be constexpr in C++11.

Maybe you have a better idea what to call such a constant?

Also where should I put it? I think it is better to have a constexpr variable in the function instead of a global macro because it is only used in that function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I think the comment right above it good enough to explain it.

}

void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
{
#ifdef HAVE_TOUCHSCREENGUI
Expand All @@ -2385,8 +2398,9 @@ void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
dist.Y = -dist.Y;
}

cam->camera_yaw -= dist.X * m_cache_mouse_sensitivity;
cam->camera_pitch += dist.Y * m_cache_mouse_sensitivity;
f32 sens_scale = getSensitivityScaleFactor();
cam->camera_yaw -= dist.X * m_cache_mouse_sensitivity * sens_scale;
cam->camera_pitch += dist.Y * m_cache_mouse_sensitivity * sens_scale;

if (dist.X != 0 || dist.Y != 0)
input->setMousePos(center.X, center.Y);
Expand All @@ -2395,7 +2409,8 @@ void Game::updateCameraOrientation(CameraOrientation *cam, float dtime)
#endif

if (m_cache_enable_joysticks) {
f32 c = m_cache_joystick_frustum_sensitivity * (1.f / 32767.f) * dtime;
f32 sens_scale = getSensitivityScaleFactor();
f32 c = m_cache_joystick_frustum_sensitivity * (1.f / 32767.f) * dtime * sens_scale;
cam->camera_yaw -= input->joystick.getAxisWithoutDead(JA_FRUSTUM_HORIZONTAL) * c;
cam->camera_pitch += input->joystick.getAxisWithoutDead(JA_FRUSTUM_VERTICAL) * c;
}
Expand Down