Results 1 to 5 of 5

Thread: Flying - Rotating/Translating?

  1. #1
    Member Newbie
    Join Date
    Jan 2010
    Location
    Mars
    Posts
    30

    Flying - Rotating/Translating?

    Hello I am relatively new to the forums (but not OpenGL) and have made a few 2D games.

    I was working on a 3D 'flight' simulator, but I had a problem. The goal is to have the user be able to fly (heading,pitch,roll) through a world, like any spaceship simulator. There will be no 'gravity' or drag.

    I had code for heading (y rotation) and pitch (x rotation) but which order do I roll (z rotation)? Do I rotate, then translate, or rotate?

    Here is my code:

    Code :
    xp += cos(DEG_TO_RAD * (heading - 90.0f ) ) * sin(DEG_TO_RAD * roll + 90.0f)    *   airspeed * dt;
    	zp += sin(DEG_TO_RAD * (heading - 90.0f) ) *  airspeed * dt;
     
     
     
     
     
    	yp +=  sin(DEG_TO_RAD   * -pitch ) * cos(DEG_TO_RAD * -roll)  * airspeed  *  dt;
    Code :
    		glRotatef(pitch, 1.0f, 0.0f, 0.0f);
     
    glRotatef(roll, 0.0f,0.0f,1.0f);
     
     
    		glRotatef(heading, 0.0f, 1.0f, 0.0f);
     
     
     
    	glTranslatef(-xp,0.0f,0.0f);
     
    	glTranslatef(0.0f,-yp,0.0f);
     
    	glTranslatef(0.0f,0.0f,-zp);


    I kept the three translates seperate so I can move them easily.

    Please help

    mikeynovemberoscar

  2. #2
    Member Newbie
    Join Date
    Jan 2010
    Location
    Mars
    Posts
    30

    Re: Flying - Rotating/Translating?

    OK, I have been researching more. I think my problem is gimbal lock. How do I fix this? I have read about quaternions but I don't really understand.

  3. #3
    Member Contributor
    Join Date
    Oct 2009
    Posts
    56

    Re: Flying - Rotating/Translating?

    Here's a good article on quaternions. There you can basically find functions to convert from a quaternion to matrix and viceversa, and to convert euler angles to a quaternion. And of course quaternion multiplication, that works as transformation combination.

    http://www.gamasutra.com/view/featur...uaternions.php

    A quaternion is a set of 4 numbers that define a rotation.

    You can use quaternions this way:

    - You have a quaternion describing the current rotation of the airplane. When you have to draw it in opengl, obtain the matrix from the quaternion and use it in opengl.

    - When you have to rotate the airplane, you normally have to do it in X axis, then in Y, then in Z (or any other order). So let's say you have an axis A and an angle a, and want to combine that rotation with the current airplane quaternion. Note that the axis must be in world coordinates, so you probably have to obtain the matrix from the current quaternion and obtaining your axis from the matrix.
    Well, there is another operation (described in the article) that converts from an axis and angle to a quaternion. Simply use it to obtain another quaternion (let's say q1) from the axis A and angle a. This quaternion describes the little rotation of the airplane for the current frame.
    Now, obtain a third quaternion that is the multiplication of the current quaternion by q1. You have obtained the new airplane quaternion. (If you have to rotate over three axis, obtain q1 and apply the quaternion multiplication twice more)

    Regards
    ----
    Antares game blog: http://antaresgame.blogspot.com/

  4. #4
    Member Newbie
    Join Date
    Jan 2010
    Location
    Mars
    Posts
    30

    Re: Flying - Rotating/Translating?

    Yeah thanks but I don't really understand. Can I just check:

    Here are my two functions:
    Code :
    eulerToQuat(float qroll, float qpitch, float qyaw, QUAT * quat)
    {
    	float cr, cp, cy, sr, sp, sy, cpcy, spsy;
    	// calculate trig identities
    	cr = cos(qroll/2);
    	cp = cos(qpitch/2);
    	cy = cos(qyaw/2);
    	sr = sin(qroll/2);
    	sp = sin(qpitch/2);
    	sy = sin(qyaw/2);
    	cpcy = cp * cy;
    	spsy = sp * sy;
    	quat->w = cr * cpcy + sr * spsy;
    	quat->x = sr * cpcy - cr * spsy;
    	quat->y = cr * sp * cy + sr * cp * sy;
    	quat->z = cr * cp * sy - sr * sp * cy;
    }
     
     
    quatToMatrix(QUAT * quat)
    {
     
     
    	float x2 = quat->x * quat->x;
    	float y2 = quat->y * quat->y;
    	float z2 = quat->z * quat->z;
    	float xy = quat->x * quat->y;
    	float xz = quat->x * quat->z;
    	float yz = quat->y * quat->z;
    	float wx = quat->w * quat->x;
    	float wy = quat->w * quat->y;
    	float wz = quat->w * quat->z;
     
    	// This calculation would be a lot more complicated for non-unit length quaternions
    	// Note: The constructor of Matrix4 expects the Matrix in column-major format like expected by
    	//   OpenGL
    	return Matrix4( 1.0f - 2.0f * (y2 + z2), 2.0f * (xy - wz), 2.0f * (xz + wy), 0.0f,
    				   2.0f * (xy + wz), 1.0f - 2.0f * (x2 + z2), 2.0f * (yz - wx), 0.0f,
    				   2.0f * (xz - wy), 2.0f * (yz + wx), 1.0f - 2.0f * (x2 + y2), 0.0f,
    				   0.0f, 0.0f, 0.0f, 1.0f)
     
     
     
     
    }

    And what I should do:

    1. Make quaternion from PITCH, ROLL, and YAW
    2. Multiply quaternion to the current one
    3. glMultMatrixf(newquaternion);

    ???

    Should I translate before I rotate? Or after?

  5. #5
    Member Contributor
    Join Date
    Oct 2009
    Posts
    56

    Re: Flying - Rotating/Translating?

    Er..sorry, I've answered you in the other post, in "Math and Algorithms" because i read it before this one. Anyway, that forum is better for this thread, i think.

    Heres the link to that post
    http://www.opengl.org/discussion_boa...189#Post270189
    ----
    Antares game blog: http://antaresgame.blogspot.com/

Similar Threads

  1. rotating and translating
    By gleps in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 08-22-2010, 10:52 PM
  2. I need a rotating and translating program
    By michael2423 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-24-2010, 03:27 PM
  3. Translating and rotating an object
    By moonwalkercs in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-18-2005, 11:49 PM
  4. Translating, Rotating Bitmaps...
    By Heywood in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-04-2003, 05:41 AM
  5. rotating,translating, and scaling text
    By drumminj in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 11-07-2000, 06:46 AM

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