Thread: How to combine rot/translation with first person OpenGL camera?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    101

    How to combine rot/translation with first person OpenGL camera?

    I have a camera in my Open GL ES 2.0 app. Using translation/rotation matrices, I can move and rotate the scene using the WSAD keys for movement/strafe and the arrow keys for rotation. I do this by pulling the "look at" vector from the modelview matrix on each loop iteration. I then normalize and add this to the translation matrix.

    The problem I'm having is getting the rotation to work. I can either translate or rotate the scene just fine, but not both. If I try to rotate the scene, it always rotates around the origin (0,0,0). If I try to pre-multiply the translation matrix with the current rotation angle, my translation always goes in the same position prior to the translation (e.g. W goes right, instead of forward when the scene is rotated 90 degrees).

    I can pull the look at, or current camer orientation vector from the modelview matrix. I can then use this value to add to the translation matrix to make the camera move forward. I use the dot product of Z and Y (which is always 1.0, since I never move up) to derive the strafe vector. This all works fine, I just need to combine the rotation!

    Here's an example from my code:

    Code:
    //calculate strafe vector using cross product of Z and Y
    vec4 direction_strafe;
    vec4_cross(direction_strafe,direction_move,(vec4){0.0,-1.0,0.0,0.0});
    
    camera_position[z] += (direction_move[z] * speed); //WS keys (fwd/back)
    camera_position[x] += (direction_strafe[x] * speed); //AD keys (strafe)
    
    //construct translation matrix
    mat4x4 translate;
    mat4x4_translate(translate, camera_position[x], 0.0, camera_position[z]); //Y is 0.0 since we never go up/down
    I apologize if I left out a bunch of info. I'm happy to provide more if necessary.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    101
    I figured out the problem. I was adding the direction_move vector to the camera position incorrectly. I was adding to separate X,Y,Z vector elements based on key input, where I should have been adding the entire vector to the direction_move vector:

    This is the incorrect code:

    Code:
    camera_position[z] += (direction_move[z] * speed); //WS keys (fwd/back)
    camera_position[x] += (direction_strafe[x] * speed); //AD keys (strafe)
    It should be:

    Code:
    camera_position += direction_move * speed; //WS keys (fwd/back)
    camera_position += direction_strafe * speed; //AD keys (strafe)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help openGL program -camera
    By evildotaing in forum C++ Programming
    Replies: 0
    Last Post: 05-30-2012, 11:55 PM
  2. Get the camera location: OpenGL
    By Queatrix in forum Windows Programming
    Replies: 2
    Last Post: 11-17-2005, 08:58 AM
  3. OpenGL : Swinging camera
    By Necrofear in forum Game Programming
    Replies: 13
    Last Post: 04-16-2005, 03:30 AM
  4. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM
  5. OpenGL Camera
    By frenchfry164 in forum Game Programming
    Replies: 15
    Last Post: 12-10-2003, 02:50 PM