0

I've been working on a small project with FreeGlut and Glew. Now I'm coding a camera system, but there are some things that are simply weird:

  • In fullscreen mode if the mouse moves in lower area of the screen, camera movements are faster than if camera moves in upper areas.

  • The camera makes weird movement, always in same direction, a small 8 figure move move.

code:

void MouseOps(int x, int y)
{
    // Changes in mousepositions.  Always same direction and 
    // in lower right corner of monitor faster, for some reason.
    deltaX = x - MousePreviousX;
    deltaY = y - MousePreviousY;

    // Also I didn't bother to put * 360 in next equations, 
    // because it would make the camera  jump for crazy. 
    // resx and resy are screen resolutions. 
    // Endresult should be that camera can 
    // rotate once when mouse moves over screen
    yaw = yaw + (((deltaX / resx)) * deginrad);
    pitch = pitch + (((deltaY / resy)) * deginrad);

    //Check clippings (eg. camera wont end upside down etc.)
    if(yaw >= (2 * pi) || yaw <= (-2 * pi)  )
        yaw = 0;
    if(pitch >= (pi / 2))
        pitch = pi / 2;
    if(pitch <= (pi / -2))
        pitch = pi / -2;

    //Calculate x, y, and z coordinates of unit sphere to look at (r = 1)
    cam_normX = cos(yaw) * sin(pitch); 
    cam_normY = sin(yaw) * sin(pitch);
    cam_normZ = cos(yaw);

    // Current x and y to previous
    int MousePreviousX = x;
    int MousePreviousY = y; 
}

I tried to use this http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates system to calculate the point to look at. Then I passed all "cam_norm" variables to

gluLookAt(cam_posX, cam_posY, cam_posZ,
        cam_posX+cam_normX, cam_posY+cam_normY, cam_posZ + cam_normZ,
        cam_upX, cam_upY, cam_upZ); 
Nyxeria
  • 363
  • 1
  • 4
  • 12
  • Why do you declare `MousePreviousX/Y` at the end of the function? They need to be static or global variables to retain their value between function calls. – Nobody moving away from SE Mar 23 '13 at 20:14
  • My bad, i actually have them declared twice apparently. I removed those, but weird things didn't go away. Actually, it appears that the camera doesn't move at all. – Nyxeria Mar 23 '13 at 20:26
  • OK, it moves, but weirdly again. – Nyxeria Mar 23 '13 at 20:46
  • You need to be more specific. What do you mean by `it moves, but weirdly`? What is weird about it? Does it move to fast/slow, only in rough steps, in random directions, to infinity and beyond? – Nobody moving away from SE Mar 24 '13 at 18:34

1 Answers1

0

I don't know why this works but it fixed all problems:

bool isCursorWarping = false;

void MouseOps(int x, int y)
{

    if(isCursorWarping == false){
        // Changes in mousepositions. Always same direction and in lower right corner of monitor faster, for some reason.
        deltaX = x - MousePreviousX;
        deltaY = y - MousePreviousY;

        yaw = yaw + ((((deltaX / resx)) * deginrad) * 360);
        pitch = pitch + ((((deltaY / resy)) * deginrad) * 360);


        //Check clippings (eg. camera wont end upside down etc.)

        if(x >= resx - 1 || y >= resy - 1 || x == 0 || y == 0)
        {
            warpCursor();
            MousePreviousX = resx / 2;
            MousePreviousY = resy / 2; 
        }else{
            MousePreviousX = x;
            MousePreviousY = y; 
        }

        if(yaw >= (2 * pi) || yaw <= (-2 * pi)  )
            yaw = 0;
        if(pitch >= (pi / 2))
            pitch = pi / 2;
        if(pitch <= (pi / -2))
            pitch = pi / -2;


        //Calculate x, y, and z coordinates of unit sphere to look at (r = 1)

        cam_normX = cos(pitch) * cos(yaw); 
        cam_normY = sin(pitch) * sin(yaw);
        cam_normZ = cos(pitch) * sin(yaw);



    }
        // Current x and y to previous and cleanup



        isCursorWarping = false;


}

void warpCursor()
{
    isCursorWarping = true;
    glutWarpPointer(resx / 2, resy / 2);


}

Then I pass the "cam_norm" values to:

gluLookAt(0.0f, 1.0f, 2.0f, 0.0f + cam_normX, 1.0f + cam_normY, 2.0f+ cam_normZ, 0.0f, 0.1f, 0.0f);
Nyxeria
  • 363
  • 1
  • 4
  • 12