Results 1 to 2 of 2

Thread: camera problem

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2014

    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),
    cos(verticalAngle) * cos(horizontalAngle)

    glm::vec3 right = glm::vec3(
    sin(horizontalAngle - 3.14f/2.0f),
    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
    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.

Similar Threads

  1. Camera problem!
    By hannahbanana in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-25-2013, 06:26 PM
  2. camera problem
    By zoodoo in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-24-2012, 12:25 AM
  3. The FPS Camera problem
    By kamimail in forum OpenGL: General
    Replies: 0
    Last Post: 05-10-2010, 07:12 AM
  4. Problem with Camera ?
    By Ngoc Sang in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 05-11-2008, 10:53 AM
  5. Camera's problem
    By hendra.o in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-27-2000, 10:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean