Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6875893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:24:38+00:00 2026-05-27T04:24:38+00:00

I have an opengl program and I am using SDL for the window creating

  • 0

I have an opengl program and I am using SDL for the window creating / event handling etc. Now, when I move the mouse to re-position the cameras yaw/pitch, I get a jittering image, with the old position flashing on the screen for a split second.

Now i think this may be an issue with the way I re-position the mouse. I have it set back to the center of the screen at the end of the re-positioning. Here is the said code:

void handleMouseMove(int mouseX, int mouseY)
{
    GLfloat vertMouseSensitivity  = 10.0f;
    GLfloat horizMouseSensitivity = 10.0f;

    int horizMovement = mouseX - midWindowX;
    int vertMovement  = mouseY - midWindowY;

    camXRot += vertMovement / vertMouseSensitivity;
    camYRot += horizMovement / horizMouseSensitivity;

    if (camXRot < -90.0f)
    {
        camXRot = -90.0f;
    }

    if (camXRot > 90.0f)
    {
        camXRot = 90.0f;
    }

    if (camYRot < -180.0f)
    {
        camYRot += 360.0f;
    }

    if (camYRot > 180.0f)
    {
        camYRot -= 360.0f;
    }

    // Reset the mouse to center of screen
    SDL_WarpMouse(midWindowX, midWindowY);
}

Now, Inside my main while loop, here is the SDL code for calling this function:

while( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_KEYDOWN )
            {
                if( event.key.keysym.sym == SDLK_ESCAPE )
                {
                    Game.running = false;
                }
            }
            if( event.type == SDL_MOUSEMOTION )
            {
                int mouse_x, mouse_y;
                SDL_GetMouseState(&mouse_x, &mouse_y);

                handleMouseMove(mouse_x, mouse_y);
            }

I found that getting the coordinates like this verses event.motion.x and event.motion.x caused less of this effect.

Any thoughts on how I could avoid this ‘jittering’ image?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:24:39+00:00Added an answer on May 27, 2026 at 4:24 am

    When you SDL_WarpMouse, another mouse move event is generated that you are handling again. You must ignore the second one.

    To make things more complicated, it’s not like every motion event is followed by one in the opposite direction caused by SDL_WarpMouse. The events may come for example two motions and 1 generated by warp to the middle of the screen. I have written this code which works for me:

    void motion(SDL_MouseMotionEvent *mme)
    {
        static bool initializing = true;  // For the first time
        static int warpMotionX = 0, warpMotionY = 0;  // accumulated warp motion
        if (initializing)
        {
            if (mme->x == midWindowX && mme->y == midWindowY)
                initializing = false;
            else
                SDL_WarpMouse(midWindowX, midWindowY);
        }
        else if (mme->xrel != -warpMotionX || mme->yrel != -warpMotionY)
        {
            /****** Mouse moved by mme->xrel and mme->yrel ******/
            warpMotionX += mme->xrel;
            warpMotionY += mme->yrel;
            SDL_WarpMouse(midWindowX, midWindowY);
        }
        else    // if event motion was negative of accumulated previous moves,
                // then it is generated by SDL_WarpMotion
            warpMotionX = warpMotionY = 0;
    }
    
    void processEvents()
    {
        SDL_Event e;
        while (SDL_PollEvent(&e))
        {
            switch (e.type)
            {
                case SDL_MOUSEMOTION:
                    motion(&e.motion);
                    break;
                default:
                    break;
            }
        }
    }
    

    Note that, I myself am interested in -mme->yrel than mme->yrel and that’s why it’s in the original code I have it negated wherever I use it (I removed it in the code I posted above)

    Where I wrote /****** Mouse moved by mme->xrel and mme->yrel ******/ is where you want to act on the mouse motion. Other code is to ignore motion generated by SDL_WarpMouse

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an OpenGL-program using GLSL, that I can run just fine with the
I am a beginner at using openGL. I have used a program which I
I'm working on some cross-platform code using OpenGL and SDL, but have immediately run
I've been attempting to render text onto an openGL window using SDL and the
I have a program that uses OpenGL combined with Cocoa and Python using PyObjC.
I have an OpenGL program that works on all of my computers but one.
I have a program (NWShader) which hooks into a second program's OpenGL calls (NWN)
I'm using GLUT to work with OpenGL (would work with SDL if I could).
I have an OpenGL example I wrote years ago demonstrating the lense effect using
I'm have horrendous problems trying to get a JOGL program compiling using Netbeans 6.9.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.