Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: camera problem

  1. #1
    Newbie Newbie
    Join Date
    Jul 2014
    Posts
    1

    camera problem

    I try to make a camera that can walk on X/Z plane and rotate 360 degrees. I've looked at tutorials but I can't understand them. Most of them use trigonometric functions, like the one below:

    // horizontal angle : toward -Z
    float horizontalAngle = 3.14f;
    // vertical angle : 0, look at the horizon
    float verticalAngle = 0.0f;

    int xpos, ypos;
    glfwGetMousePos(&xpos, &ypos);
    glfwSetMousePos(1024/2, 768/2);

    horizontalAngle += mouseSpeed * deltaTime * float(1024/2 - xpos );
    verticalAngle += mouseSpeed * deltaTime * float( 768/2 - ypos );

    glm::vec3 direction(
    cos(verticalAngle) * sin(horizontalAngle),
    sin(verticalAngle),
    cos(verticalAngle) * cos(horizontalAngle)
    );

    glm::vec3 right = glm::vec3(
    sin(horizontalAngle - 3.14f/2.0f),
    0,
    cos(horizontalAngle - 3.14f/2.0f)
    );

    glm::vec3 up = glm::cross( right, direction );

    ViewMatrix = glm::lookAt(
    position, // Camera is here
    position+direction, // and looks here : at the same position, plus "direction"
    up // Head is up (set to 0,-1,0 to look upside-down)
    );


    Why is trigonometry used in the code,how do I create a camera that can freely walk and look around?

  2. #2
    Junior Member Newbie
    Join Date
    Jul 2011
    Location
    USA
    Posts
    23
    You may want to learn spherical coordinates and the mathematics behind rotation matrices. Graphics programming will seem nearly impossible without a solid understanding of trigonometry, linear algebra, and vector analysis. In your case, the camera model is easier to describe using spherical coordinates but it needs to be transformed into rectangular coordinates before you can use it in your OpenGL scene.

    For the view matrix, you need a direction vector, a right vector, and the up vector. The direction vector is found by converting the horizontal and vertical angles from spherical coordinates to rectangular. The other "trigonometric" code computes the right vector. It takes the horizontal angle and rotates it by 90 degrees, which is the same thing as rotating pi / 2 radians. Taking the sin of the horizontal for x, y equal to 0, and the cos of the horizontal for z, gives the required coordinates. Finally, the up vector is just the cross product of the right and direction vectors. This gives the three vectors needed for constructing the view matrix which will update the camera.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •