Faster pooling of mouse position than using an eventFilter?



  • Hello,

    I am hoping this question makes sense.

    I would like to track and hold the mouse in the center of the screen for an OpenGL application. I currently use the below working code which is based on an event filter but the problem is that the event filter is kind of slow in comparison to the rendering speed of the application.

    Therefore, what happens is that the rendered scene looks jittery when I move the mouse. When the mouse isn't moving; the scene is liquid smooth.

    So, is there a way to get the position of the mouse "faster" such that the OpenGL scene can look smooth when I move the mouse as well?

    Thank you for your time.

    @
    bool QTOpenGLWindow::eventFilter(QObject *target, QEvent *event) {

    if (target == this) {
    if (event->type() == QEvent::MouseMove) {
    QMouseEvent mouseEvent = static_cast<QMouseEvent>(event);
    if (MainScene->IsInitialized()) {
    if (MouseLocked) {
    MainScene->GetCurrentCamera()->SetCurrentMousePosition((GLdouble)mouseEvent->pos().x(), (GLdouble)mouseEvent->pos().y());
    QPoint glob = mapToGlobal(QPoint(width() / 2, height() / 2));
    QCursor::setPos(glob);
    }
    }
    }
    if (event->type() == QEvent::Resize) {
    if (MainScene->IsInitialized()) {

    CurrentWindowWidth = this->size().width();
    CurrentWindowHeight = this->size().height();
    MainScene->SetWindowWidth(CurrentWindowWidth);
    MainScene->SetWindowHeight(CurrentWindowHeight);
    glViewport(0, 0, CurrentWindowWidth, CurrentWindowHeight);
    

    }
    }

    }

    return false;

    }
    @


  • Moderators

    Instead of event filter you can start a timer and poll the current position of a cursor with QCursor::pos() at whatever interval you need (down to what OS is able to provide).

    I had some experience with similar issues. If the mouse controls camera movement then what I found is if you want smooth movement you can't rely on the discrete mouse moves. It will always be more jittery than, say, a gamepad stick, no matter the poll interval (unless you have really high frequency mouse and proper drivers).

    To fight this I usually implement some sort of "mouse smoothing". This can be done by having a camera velocity vector and having the mouse control that vector and not the position itself. In every frame the current mouse vector (from the window center to mouse position) would be added to that velocity vector and the velocity would update position. the velocity vector should have some damping applied to make it 0 after the mouse stops moving.
    One side-effect is that the camera seems to "chase" the cursor. It can be mitigated by adjusting the damping factor and velocity magnitude. Usually some amount of tweaking leads to very smooth, gamepad-like movement.



  • Interesting idea; will try this out!

    Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.