Question

So, I'm attempting to create an FPS style camera to explore my scene with, and it seems to be working fine. There is no way of changing the pitch of the camera at the moment. I am trying to create a function that allows me to set the camera to some predefined viewpoints, however when attempting to rotate the camera after setting it to a new viewpoint using the setPosition() function, I experience a jump not in position but in orientation when first pressing the rotate button.

This is my camera class implementation.

Camera::Camera(void)
{
    angleX = 0.0;
    angleY = 0.0;
    directionX = 0.0;
    directionZ = -5.0;
    directionY = 0.0;
    xPos = 0.0;
    zPos = 1.0;
    yPos = 2.0;
}

void Camera::setPosition(float xDir, float yDir, float zDir, float posX, float posZ, float posY)
{
    directionX = xDir;
    directionZ = zDir;
    directionY = yDir;
    xPos = posX;
    zPos = posZ;
    yPos = posY;
}

Camera::~Camera(void)
{
}

The following is my special keys callback function, with functionality other than camera movement omitted.

void specialKeys(int key, int xx, int yy)
{
    float fraction = 0.3f;

    switch(key)
    {
        // Camera Controls
        case GLUT_KEY_LEFT:
            camera.angleY -= 0.05f;
            camera.directionX = sin(camera.angleY);
            camera.directionZ = -cos(camera.angleY);
            break;
        case GLUT_KEY_RIGHT:
            camera.angleY += 0.05f;
            camera.directionX = sin(camera.angleY);
            camera.directionZ = -cos(camera.angleY);
            break;
        case GLUT_KEY_UP:
            camera.xPos += camera.directionX * fraction;
            camera.zPos += camera.directionZ * fraction;
            camera.yPos += camera.directionY * fraction;
            break;
        case GLUT_KEY_DOWN:
            camera.xPos -= camera.directionX * fraction;
            camera.zPos -= camera.directionZ * fraction;
            camera.yPos -= camera.directionY * fraction;
            break;
       }
}

The display callback function then has the following code using gluLookAt:

glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(camera.xPos, camera.yPos, camera.zPos, camera.xPos+camera.directionX, camera.yPos+camera.directionY, camera.zPos+camera.directionZ, 0, 1, 0);

I cannot see why I am getting this issue. I believe it's something to do with the interaction between setting the camera and the maths being used to rotate it. Can anyone see how to prevent this jump in orientation?

I am setting the camera position in a keyboard callback function, the code is as follows:

    if(key == '1') // Default View
        camera = Camera();
    if(key == '2') // Ferris Wheel View
        camera.setPosition(-0.8, 0.0, -0.6, 3.6, 3.6, 6.5);
    if(key == '3') // Fountain Water View
        camera.setPosition(0.9, 0.0, -0.4, -0.59, -6.4, 0.6);
Was it helpful?

Solution

The jump is because how you are initializing your directionZ variable, look at:

   angleX = 0.0;
   angleY = 0.0;
   directionX = 0.0;
   directionZ = -5.0;
   directionY = 0.0;

But later you define directionX and directionZ to be:

camera.directionX = sin(camera.angleY);
camera.directionZ = -cos(camera.angleY);

cos is bounded on the intervel [-1, 1] and since angleY is 0.0f, directionZ will be set to -1. If you change your initialization code to set directionZ = -1.0; you should get rid of the jump.

don't set direction explicitly. Here is an example of setting an angle and computing direction from that angle:

void Camera::setPosition(float angle, float posX, float posZ, float posY)
{
    xPos = posX;
    zPos = posZ;
    yPos = posY;

    //compute instead of set based on angle
    angleY = angle;
    camera.directionX = sin(angleY);
    camera.directionZ = -cos(angleY);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top