Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Wrong sf::Event::JoystickMoved events sent when pressing any axis or button  (Read 5614 times)

0 Members and 1 Guest are viewing this topic.

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
When I press any button or stick on the gamepad, I get several sf::Event::JoystickMoved events.
I think it's axis calibration issue...

Check out what you get with this program (don't press any axes. Just press any button).
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "");
    window.setVerticalSyncEnabled(true);
   
    sf::Joystick::update();

    std::vector<std::string> names = {
        "X",    ///< The X axis
        "Y",    ///< The Y axis
        "Z",    ///< The Z axis
        "R",    ///< The R axis
        "U",    ///< The U axis
        "V",    ///< The V axis
        "PovX", ///< The X axis of the point-of-view hat
        "PovY"  ///< The Y axis of the point-of-view hat
    };

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            switch (event.type) {
                case sf::Event::Closed:
                    window.close();
                    break;
                case sf::Event::JoystickMoved:
                    std::cout << names[event.joystickMove.axis] << " " << event.joystickMove.position << std::endl;
                    break;
                default:
                    break;
            }
        }

        window.clear(); // fill background with color
        window.display();
    }
}

I consistently get this:
X 0.389105
Y -1.17952
R -395209
U 4.31067

What can I do about it?
« Last Edit: November 04, 2016, 03:04:46 pm by Elias Daler »
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Have you checked with something else than SFML, to make sure that this is not a driver or hardware issue?

You should also test other gamepads, and/or on other OSes.
Laurent Gomila - SFML developer

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Have you checked with something else than SFML, to make sure that this is not a driver or hardware issue?

You should also test other gamepads, and/or on other OSes.
Yes, I checked with Windows 7 and Windows 8. Gamepads: Xbox 360 gamepad and Logitech F310. The output I included in the original post was from Logitech F310.
Xbox 360 gamepad gives me this:
X -5.18044
Y 6.1387
R 4.53651
U -4.19776

Even if it's hardware issue (calibration being not perfect), maybe SFML should do something with it? Like checking initial state of buttons and then checking new position of axes from it? It looks like this causes the problem. SFML assumes that all axes are at 0 by default, but they may be a little off. It sees that axis has different value than 0, thinking that it has moved a bit.

I think that's what happens. Other button pressed don't produce wrong events.
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Quote
Yes, I checked with Windows 7 and Windows 8
Well, it's more or less the same OS. Testing on Linux and/or Mac OS X would be more relevant ;)

And you should really test with another application, for example the joystick utility of the OS.

Quote
Even if it's hardware issue (calibration being not perfect), maybe SFML should do something with it?
It doesn't look like a calibration issue. If axes move when you press buttons, it's a real problem. And I don't know what SFML could do about it (if it's not the cause of the problem, of course).

Quote
SFML assumes that all axes are at 0 by default, but they may be a little off. It sees that axis has different value than 0, thinking that it has moved a bit.
You would get a single Moved event for the initial offset, not one everytime you press a button.

Quote
Other button pressed don't produce wrong events.
You never said that it was only for specific buttons, nor which ones it is, did you?
Laurent Gomila - SFML developer

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Quote
Yes, I checked with Windows 7 and Windows 8
Well, it's more or less the same OS. Testing on Linux and/or Mac OS X would be more relevant ;)

And you should really test with another application, for example the joystick utility of the OS.
Unfortunately, I don't have access to Linux/Mac OS X at the moment. Windows joystick utility doesn't give precise numbers of axes. I'll try to find precise utilities soon.

Quote
Other button pressed don't produce wrong events.
You never said that it was only for specific buttons, nor which ones it is, did you?
Oh, I've meant that pressing any button produces several "axis moved" events. Doing another key press doesn't produce any "axis moved" events.
The same happens if I press any axis. I get the same results on the first axis press. Other presses do nothing, they send correct events.

So, it looks like any gamepad state change causes axis check in SFML. It sees that some axes are not precisely at 0, thinks that they moved, and sends a bunch of events.
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Tried the snippet above with an Xbox One gamepad and I do indeed get movement, but only with the very first button press, which is most likely just due to the way polling under Windows works (retrieving states and the very first one is different from "nothing") and how SFML detects movement.

Further button presses no longer cause any axis movement.

Although keep in mind that SFML by default doesn't include any threshold, so a movement of "5" or even "10" is essentially nothing (since axes scale from -100 to 100).

That R movement on the Logitech is a bit weird though, but that might be some hardware quirks.

Edit: Regarding sensitivity, that's essentially intentional. You should do your own thresholding. I can get joystick events just by loosely rubbing my thumb on any of the thumbsticks even without applying significant force.

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Yeah, adding "dead zone" for sticks helps, but still, it just doesn't seem right to receive event when literally nothing about axes positions changes. I think that if SFML checked positions of sticks on startup and then started sending events in comparison with the beginning position, there wouldn't be any problem.

For example: suppose that L axis is at 0.005 when the program starts. SFML should store it and then send move events only if axis position changes from 0.005.

Or suppose that L axis is at 100.f at the beginning (player tilted left stick before the startup). Should SFML create JoystickMove event in that case if the player continues to tilt left stick? I think it shouldn't, because that's not the event that happened during event polling.
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Quote
I think that if SFML checked positions of sticks on startup and then started sending events in comparison with the beginning position, there wouldn't be any problem.
Guess what, this is exactly what it does ;)

Don't start trying to solve something based on assumptions (or what Mario says without checking the code first ;D). This problem definitely requires some serious debugging to figure out what happens.
Laurent Gomila - SFML developer

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Quote
I think that if SFML checked positions of sticks on startup and then started sending events in comparison with the beginning position, there wouldn't be any problem.
Guess what, this is exactly what it does ;)

Don't start trying to solve something based on assumptions (or what Mario says without checking the code first ;D). This problem definitely requires some serious debugging to figure out what happens.
Yeah, I was just being lazy. Okay, I'll take a look what goes wrong. Would appreciate if someone tries to do the same.
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler