Skip to content

About

Mouse to analog stick without running out of mousepad: a sliding virtual center (dependency-free C++17, MIT)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

sliding-deadzone

CI License: MIT

Mouse to analog stick, without running out of mousepad.

Demo: the hand went past the radius, so the virtual center slides along and the stick stays at full tilt

A small, dependency-free C++17 library for anything that has to turn mouse movement into a gamepad stick: console adapters, emulators, compatibility layers, remote-play clients.

The problem

Games turn the camera with the right stick as a speed, and that speed is capped at full tilt. A plain linear mapping (mouse distance = stick tilt) works in menus but hits a wall in 3D games: once the stick is at 100%, the hand keeps moving, the mousepad runs out, and to stop or turn back you first have to travel all the way back. That return trip is lag you can feel.

The sliding center

The tilt is the distance of the hand from a virtual center, and that center slides along with the hand:

[At rest]  the virtual center (C) is where the mouse (X) is

  Center [ C ]
  Mouse  [ X ] -------------------> moving right, e.g. 6 cm

[Past the radius]  3 cm away = full tilt, but the hand keeps going

  Center [ C ]
  Radius [ 3 cm ]
  Mouse  [     X ] ---------> still moving, to 6 cm

  -> the center slides along with the mouse, the distance stays at the radius

[Turning back]  moving left by the same 3 cm

  New center         [ C_new ]
  Mouse back by 3 cm [ X ]   <- inside the zero zone of the new center: the camera stops
  • Full tilt is never more than one radius away, however far the hand went.
  • Moving back by the radius always brings the stick to zero.
  • The hand never has to cover the "dead" distance beyond full tilt again, so muscle memory stays intact.

Center reset (optional)

With center_reset on, the center snaps to the hand the moment it turns back (about 1 mm toward the center) or stops (stop_ms, 50 ms by default). The stick drops to zero at once - good for third-person and RPG cameras, and there is no drift over time.

Also included

  • Velocity mode - tilt follows the speed of the hand; the stick returns to zero when the hand stops.
  • Response curve - more precision for small movements.
  • Game deadzone compensation - every movement gives at least this much tilt, so it gets past the game's own deadzone and the camera reacts at once.
  • Smoothing, full tilt (any movement tilts all the way), invert Y.

Usage

Copy src/sliding_deadzone.hpp and src/sliding_deadzone.cpp into your project, or use CMake (add_subdirectory, target sliding_deadzone).

#include "sliding_deadzone.hpp"

sliding_deadzone::Stick stick;
sliding_deadzone::Settings settings;   // Position mode (sliding center), 800 DPI, 3 cm radius
settings.dpi = 1600;                // set it to your mouse - then centimetres are real centimetres
settings.center_reset = true;

// For every raw mouse event (counts, e.g. SDL's event.motion.xrel / yrel):
stick.addMotion(dx, dy);

// Regularly, e.g. every 4 ms:
stick.update(4.0f, settings);
set_right_stick(stick.x(), stick.y());   // -1..1, x right, y down

stick.visual(settings) returns the hand position relative to the center, the zero zone and whether the center is sliding - enough to draw an on-screen indicator like the demo does.

Setting Default Meaning
mode Position Position (sliding center) or Velocity
dpi 800 mouse counts per inch
radius_cm 3.0 hand distance from the center for full tilt
zero_zone_cm 0.3 no tilt within this distance from the center
center_reset off center snaps to the hand when it turns back or stops
stop_ms 50 no movement for this long counts as a stop
sensitivity 1.0 Velocity mode: 1.0 = full tilt at 10 counts per millisecond
full_tilt off any movement tilts the stick all the way
curve 1.0 response curve exponent (1 = linear)
deadzone 0.15 minimum tilt for any movement
smoothing 0.5 0 = none, 0.95 = strong
invert_y off mouse up = stick down

Build, test, demo

cmake -S . -B build
cmake --build build
ctest --test-dir build
./build/sliding_deadzone_demo        # needs SDL2: move the mouse, 1 / 2 = mode, R = center reset, F = full tilt

Background

This is a small piece of a bigger project I'm working on, around playing consoles from a PC. More about that soon.

License

MIT - see LICENSE.

About

Mouse to analog stick without running out of mousepad: a sliding virtual center (dependency-free C++17, MIT)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages