teeworlds / teeworlds

A retro multiplayer shooter
https://www.teeworlds.com
Other
2.37k stars 638 forks source link

Having a joystick plugged in moves the mouse cursor on its own #2085

Closed LordSk closed 5 years ago

LordSk commented 5 years ago

I started teeworlds with my xbox controller plugged in and the mouse cursor was moving on its own. SDL_JoystickGetAxis seems to always return a value even when not touching the controller (I guess controller sticks are not perfect).

So there are several issues here:

Relevant code:

void CInput::MouseRelative(float *x, float *y)
{
    if(!m_InputGrabbed)
        return;

    int nx = 0, ny = 0;
    float Sens = g_Config.m_InpMousesens/100.0f;

    SDL_GetRelativeMouseState(&nx,&ny);

    float jx = 0.0f;
    float jy = 0.0f;

    if(m_pJoystick)
    {
        jx = static_cast<float>(SDL_JoystickGetAxis(m_pJoystick, g_Config.m_JoystickX)) / 32768.0f * 50.0f;
        jy = static_cast<float>(SDL_JoystickGetAxis(m_pJoystick, g_Config.m_JoystickY)) / 32768.0f * 50.0f;
    }

    *x = (nx + jx)*Sens;
    *y = (ny + jy)*Sens;
}

engine/client/input.cpp

oy commented 5 years ago

From SDL wiki:

On most modern joysticks the X axis is usually represented by axis 0 and the Y axis by axis 1. The value returned by SDL_JoystickGetAxis() is a signed integer (-32768 to 32767) representing the current position of the axis. It may be necessary to impose certain tolerances on these values to account for jitter.

Don't have a joystick so can't test it myself. Would be good if someone who's able to test it could work on it.

Don't think that you have to deactivate a device(mouse/joystick). What's the benefit? You don't use them both at the same time right?

Dune-jr commented 5 years ago

Unfortunately can't test it myself either. :(

So apparently, the issue right now is that the joystick is buggy because of the lack of tolerance for jitter, just like in that SDL wiki quote. To me, the proper thing to do would be to fix the joystick by adding this tolerance (or, second choice, disable it until it is fixed, since simply having one plugged in breaks Teeworlds).

Would be interesting to know how @tsoding did not run into this issue. Maybe he has a perfect controller :)

Side note: it seems like there is no good setup that lets you play Teeworlds on an xbox 360 controller, according to @TerminalMan

LordSk commented 5 years ago

Don't think that you have to deactivate a device(mouse/joystick). What's the benefit? You don't use them both at the same time right?

Two different modes, for example:

Joystick

Mouse

Using one or the other switches mode. This pattern is used a lot for pc games supporting joysticks (e.g. Dark Souls). That's what I meant by one or the other should be active at any time. But the real issue is my controller moving my mouse cursor while I try to move my mouse. Which can be fixed by adding a tolerance, and/or by separating the two code paths.

EbrahimBGA commented 5 years ago

issue

Originally posted by @EbrahimBGA in https://github.com/teeworlds/teeworlds/pull/2047#issuecomment-471075948

the cursor moving up... Playstation Dualshock 4 - DS4Windows -Win10x64

rexim commented 5 years ago

I have Logitech Rumble F510. I guess it's a pretty good controller. ¯_(ツ)_/¯

The easiest fix without completely disabling the feature would be adding the tolerance. I could do that, but only on April 16th 2019.

P.S. @Dune-jr please use @rexim to summon me to a discussion. @tsoding doesn't work, because it's an organization and I probably haven't configuration notifications correctly or something.

oy commented 5 years ago

Maybe add a tolerance setting for the jitter, that you can adjust or lets you detect automatically.

oy commented 5 years ago

Fixed.