Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Faster pooling of mouse position than using an eventFilter?
Forum Updated to NodeBB v4.3 + New Features

Faster pooling of mouse position than using an eventFilter?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tmason101
    wrote on last edited by
    #1

    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;

    }
    @

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tmason101
        wrote on last edited by
        #3

        Interesting idea; will try this out!

        Thank you.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved