Thread: walking through space

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    61

    walking through space

    I'm making a small and simple 3D game, but I'm habing difficulties to walk around in space.

    I made a structure which holds the position and the rotation of my camera. I also have a structure which holds the position and rotation of my object.

    To draw this, I tried this code :
    Code:
    	glLoadIdentity();
    	glTranslatef (-cam_pos[0], -cam_pos[1], -cam_pos[2]);
    	glRotatef (cam_rot[0], 1.0f, 0.0f, 0.0f);
    	glRotatef (cam_rot[1], 0.0f, 1.0f, 0.0f);
    	glRotatef (cam_rot[2], 0.0f, 0.0f, 1.0f);
    
    	glTranslatef (obj_pos[0], obj_pos[1], obj_pos[2]);
    	glRotatef(obj_rot[0], 1.0f, 0.0f, 0.0f);
    	glRotatef(obj_rot[1], 0.0f, 1.0f, 0.0f);
    	glRotatef(obj_rot[2], 0.0f, 0.0f, 1.0f);
    	
    	glBegin(...);
    	glVertex3f(vert_pos[0], vert_pos[1],vert_pos[2]);
    	...etc
    	glEnd()
    }
    This code gives wrong results though.
    Does anyone know what I'm doing wrong?

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    can you describe the "wrong results" your seeing? The problem could be a number of things.

    *Are your vert_pos and obj_pos coords in world coordinates or are they relative to the camera's position? (because the vert_pos coords are being rendered relative to the object (obj_pos) which is rendered relative to the camera)

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    The objects seem to rotate around the origin (but I could be wrong) . The vert_pos coordinates are relative to the obj_pos coordinate. And obj_pos is in world coordinates.
    The cam_pos is als in world coordinates.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Well, the way this works highly depends on the conventions you're using. My first guess would be to try performing the camera rotations before the translation.

    If that doesnt work you'll have to trace through what is happening one step at a time (in terms of positioning and rotation). Note that a rotation followed by a translation will have a different effect than a translation followed by a rotation.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Transformation order is reverse of "logical" order. If you transform in this order:
    Code:
    glLoadIdentity();
    glTranslate();
    glRotate();
    glScale();
    The scale will be done first, then the rotation and last the translation.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM
  2. disk space mysteriously gone
    By evader in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2004, 01:30 PM
  3. Replies: 12
    Last Post: 05-17-2003, 05:58 AM
  4. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 03:59 PM