#### Archived

This topic is now archived and is closed to further replies.

# Getting the UP vector from heading and pitch quaternions...

This topic is 5617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

RT. How would I go about doing this? This is to allow strafing, btw. I'm planning on crossing the UP with the LOOK vector...unless there's a more efficient/direct way of doing this? [edited by - Silex on May 29, 2004 4:25:54 PM]

##### Share on other sites
"unless there''s a more efficient/direct way of doing this?"

Not really. The side vector is simply the cross product of the forward and up vectors.

##### Share on other sites
I was wondering if perhaps the right vector had already been calculated in the process of the creating the view matrix.

In case this''ll help anyone figure out what I''m trying to do, here''s the function where I create the view vector and do the quaternion math:

        //create quaternions to represent rotations        pitch.CreateFromAxisAngle(1.0f,0.0f,0.0f, pitchdeg);	heading.CreateFromAxisAngle(0.0f,1.0f,0.0f, headingdeg);	//combine the pitch and heading rotations	q = pitch * heading;	q.CreateMatrix(matrix);	//send matrix to opengl	glMultMatrixf(matrix);	//calc j component of direction vector	pitch.CreateMatrix(matrix);	dir.j = matrix[9];	//combine heading and pitch to get i and j components of        dir vector	q = heading * pitch; //quaternion multiplication not commutative	q.CreateMatrix(matrix);	dir.i = matrix[8];	dir.k = matrix[10];	//scale direction according to speed	dir.Unit();	        [...........CODE OMITTED..........]	dir *= vel;	//increment position by dir vector	pos.x += dir.i;	pos.y += dir.j;	pos.z += dir.k;		//update scene center based on camera location	glTranslatef(-pos.x,-pos.y,pos.z);

I need a way to extract the up vector so I can cross it with the dir one to have an axis to strafe along, or a way to directly grab this strafe vector.

##### Share on other sites
For some reason the code block won''t show up in my browser, but yes, you should be able to extract the forward, up, and side vectors from the view matrix. Depending on how the matrix is set up, it should be either:

side [0, 1, 2]
up [4, 5, 6]
forward [8, 9, 10]

or:

side [0, 4, 8]
up [1, 5, 9]
forward [2, 6, 10]

##### Share on other sites
So could I just use the side vector as the strafe axis? Would this take into account the pitch and heading rotations? Or should I use the up vector and cross it with my working dir vector?

##### Share on other sites
"So could I just use the side vector as the strafe axis? Would this take into account the pitch and heading rotations? Or should I use the up vector and cross it with my working dir vector?"

If you take the vectors straight from the matrix, the side (strafe) vector should already be the cross product of the forward and up vectors. So you should just be able to use it as is.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 18
• 35
• 9
• 16
• 22