• ### Announcements

#### Archived

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

# extracting direction vectors from quaternion

## Recommended Posts

say i have a quaternion which defines the direction my camera is pointing at, how can i extract the right/up/front-vectors from that quat?

##### Share on other sites
It''s fairly easy, just rotate your initial forward direction around the current rotation axis.

Mike

##### Share on other sites
V[0] = 2 * (x * z - w * y)
V[1] = 2 * (y * z + w * x)
V[2] = 1 - 2 * (x * x + y * y)

where V is the direction-vector. Probably faster than performing a rotation.

Hope that helped .

##### Share on other sites
forward vector:
x = 2 * (x*z + w*y)
y = 2 * (y*z - w*x)
z = 1 - 2 * (x*x + y*y)

up vector
x = 2 * (x*y - w*z)
y = 1 - 2 * (x*x + z*z)
z = 2 * (y*z + w*x)

left vector
x = 1 - 2 * (y*y + z*z)
y = 2 * (x*y + w*z)
z = 2 * (x*z - w*y)

If you''re wondering how I got this, I derived it from quaternion to matrix conversion code. I took the matrix form and multiplied by the vectors (0,0,1) (0,1,0) and (1,0,0) to get these simplified forms.

##### Share on other sites
That''s cool, updating my code right now. Thanks.

Mike

##### Share on other sites
I double checked that transform, and it''s correct according to the Quaternion math, but to make it produce the same results D3DXMatrixRotationQuaternion does I had to make two changes:

  v3Left.z = -2 * (x*z - w*y);v3Dir.x = -2 * (x*z + w*y); //forward vector

I just realized that I don''t have any way to rotate the view up or down yet, so there''s probably a flip in v3Up vector as well. If this due to a LHC vs a RHC system problem?

Magmai Kai Holmlor
- Not For Rent

##### Share on other sites
> I double checked that transform, and it''s correct according to
> the Quaternion math, but to make it produce the same results
> D3DXMatrixRotationQuaternion does I had to make two changes...

From the D3DXQuaternionMultiply documentation:

> The result represents the rotation Q2 followed by the rotation
> Q1 (Out = Q2 * Q1). The output is actually Q2*Q1 (not Q1*Q2).
> This is done so that D3DXQuaternionMultiply maintain the same
> semantics as D3DXMatrixMultiply because unit quaternions can
> be considered as another way to represent rotation matrices.

And from my own experience D3D uses quaternions oriented differently from the ''usual'', which has to be taken into account whenever they are multiplied. As there''s no universal standard for quaternion orientation it''s generally best to do such checks when adopting a new quaternion API.

##### Share on other sites
If the order was reversed, the results wouldn''t be anywhere close to the same. I was using D3DXMatrixRotationQuaternion to generate a matrix from the Q and extracting the orthonormal vectors from that, instead of going directly from the q to the orhto''s.

D3D''s matricies don''t behave as I expected them too, I suppose the q''s might not either.

Magmai Kai Holmlor
- Not For Rent

##### Share on other sites
Im liking this forum very much indeed, thanks, updating my code right now

• ### Forum Statistics

• Total Topics
627738
• Total Posts
2978881

• 10
• 10
• 21
• 14
• 13