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

Author Topic: Cursor for camera rotation in OpenGL  (Read 2681 times)

0 Members and 1 Guest are viewing this topic.

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Cursor for camera rotation in OpenGL
« on: September 01, 2011, 01:26:08 pm »
So I'm working on a small OpenGL project, and my goal is to get a camera to translate through a scene and rotate around itself in a way similar to the first-person camera in Minecraft.

So far, I've got almost everything working the way I want, except for one problem: when moving the mouse left or right to rotate the camera, the cursor eventually hits the edge of the window and stops rotating the camera.  I don't want this; I am aiming for a camera that can continuously rotate in one direction, so long as the mouse is being moved in that direction.

I tried continuously setting the cursor to half the window dimensions (i.e. the center), but that wasn't workable - which should have been obvious to me in hindsight, but meh.

My current code looks like this.  The "wrap around" code doesn't work, though, because when I move the mouse too quickly over the edge of the window, it stops registering a mouse position before the mouse oversteps its bounds - and if I go do slow, it gets thrown around constantly.

Code: [Select]
       //Current Mouse Coords.
        NewMouseX = App.GetInput().GetMouseX();
        NewMouseY = App.GetInput().GetMouseY();

        //If the mouse has left the bounds of the screen, wrap around.
        if (NewMouseX >= WindowWidth - 1 || NewMouseY >= WindowHeight - 1 || NewMouseX < 1 || NewMouseY < 1)
        {
            std::cout << "Wrapping!\n";
            int WrappedX = NewMouseX;
            int WrappedY = NewMouseY;
            if (WrappedX >= WindowWidth - 1)
            {
                WrappedX = 2;
            }
            else if (WrappedX < 1)
            {
                WrappedX = WindowWidth - 2;
            }
            if (WrappedY >= WindowHeight - 1)
            {
                WrappedY = 2;
            }
            else if (WrappedY < 1)
            {
                WrappedY = WindowHeight - 2;
            }
            App.SetCursorPosition(WrappedX, WrappedY);
        }

        //Modify the angle based on mouse movement.
        PlayerRot[0] += (NewMouseX - OldMouseX) * 0.3;
        PlayerRot[1] += (NewMouseY - OldMouseY) * 0.3;
        //Set it back within the bounds of the 0-359 circle.
        if (PlayerRot[0] > 359)
        {
            PlayerRot[0] -= 360;
        }
        else if (PlayerRot[0] < 0)
        {
            PlayerRot[0] += 360;
        }
        if (PlayerRot[1] > 359)
        {
            PlayerRot[1] -= 360;
        }
        else if (PlayerRot[1] < 0)
        {
            PlayerRot[1] += 360;
        }

        //Adjust mouse values, for the next frame.
        OldMouseX = NewMouseX;
        OldMouseY = NewMouseY;


Does anyone here know how to handle this kind of continuously rotating camera?  Are there any special features in SFML that might be helpful here?