Archived

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

paniq

extracting direction vectors from quaternion

Recommended Posts

paniq    122
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 this post


Link to post
Share on other sites
NewDeal    134
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 this post


Link to post
Share on other sites
Dobbs    164
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 this post


Link to post
Share on other sites
Shannon Barber    1681
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 this post


Link to post
Share on other sites
johnb    351
> 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 this post


Link to post
Share on other sites
Shannon Barber    1681
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 this post


Link to post
Share on other sites