Results 1 to 4 of 4

Thread: OpenGL first person camera orientation issues

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    3

    OpenGL first person camera orientation issues

    I have a "camera" in my opengl program that I recently finished. However, I've noticed that whenever I rotate and then move again, the x, y, and z angles change. For example, when I press the "w" key, I move forward along the "z" axis. If I then rotate the camera 90 degrees, when I push the "W" key, I will actually be moving right, seemingly along the "x" axis. It makes sense why this happens, I'm just wondering why its happening. Here's the rotation function:

    Code :
    private void camera() {
    		glRotatef(xrot, 1.0f, 0.0f, 0.0f);
    		glRotatef(yrot, 0.0f, 1.0f, 0.0f);
    	}
    The keyboard function:

    Code :
     if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
    			xpos -= 0.035 * delta;
    		}
     
    		if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
    			xpos += 0.035 * delta;
    		}
     
    		if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
    			zpos += 0.03f * delta;
    		}
     
    		if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
    			zpos -= 0.035 * delta;
    		}
        if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
    			xrot += 0.035;
    			if (xrot > 360) {
    				xrot -= 360;
    			}
    		}
     
    		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
    			xrot -= 0.035;
    			if (xrot > 360) {
    				xrot += 360;
    			}
    		}
     
    		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
    			yrot += 0.035;
    			if (xrot > 360) {
    				xrot -= 360;
    			}
    		}
     
    		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
    			yrot -= 0.035;
    			if (xrot > 360) {
    				xrot += 360;
    			}
     
    		}
    And my translate function:

    Code :
    glTranslated(xpos, ypos, zpos - 30);
    any ideas on how to solve this? I would be very grateful.

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2012
    Posts
    129
    You are moving it along the axes, not along the view direction. Increment the position by the view direction vector. You can get it using the x and y angles, or you can extract it from the view matrix.The 3rd row is the negated view direction. So you can do:

    Code :
    float matrix[16];glGetFloatv(GL_MODELVIEW_MATRIX, matrix)
     
     
    Vector3 viewVec = Vector3(-matrix[2], -matrix[6], -matrix[10]);

    or:

    Code :
    Vector3 RotationToVector(float xRotRads, float yRotRads)
    {
        Vector3 dir;
        float cosY = cosf(yRotRads);
     
     
        dir.x = sinf(xRotRads) * cosY;
        dir.y = -sinf(yRotRads);
        dir.z = cosf(xRotRads) * cosY;
     
     
        return dir;
    }

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    3
    Well thats makes sense. But what is sinf? Is that the inverse of sin? Also, is the above method just for the y direction?

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,649
    Quote Originally Posted by opiop65 View Post
    Well thats makes sense. But what is sinf? Is that the inverse of sin?
    It's just the 32-bit float version of sin(). Check your man pages or standard C library docs:

    Code cpp:
           double sin(double x);
           float sinf(float x);
           long double sinl(long double x);

Similar Threads

  1. Third Person Camera
    By ScorpZ in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-29-2012, 07:35 AM
  2. OpenGL First Person Camera, non-gluLookAt()
    By SRive in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-18-2012, 12:02 AM
  3. First Person Camera problem
    By humanitarian in forum OpenGL ES
    Replies: 1
    Last Post: 05-05-2011, 04:00 PM
  4. Camera orientation issues
    By dogdemir in forum OpenGL: General
    Replies: 0
    Last Post: 10-07-2009, 11:42 PM
  5. 3rd Person Camera
    By Binqay in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-06-2001, 11:40 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