Skip to main content
GameDev.net gamedev.net
🔒 Locked

extracting direction vectors from quaternion

Started by paniq Aug 3, 2001 at 8:20 PM 7 replies 34.6k views
Original Post
paniq
paniq
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?
Vetinari
Vetinari
It''s fairly easy, just rotate your initial forward direction around the current rotation axis.


Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
NewDeal
NewDeal
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 .
Dobbs
Dobbs
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.
Vetinari
Vetinari
That''s cool, updating my code right now. Thanks.


Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
Shannon Barber
Shannon Barber
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
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
johnb
johnb
> 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.
John BlackburneProgrammer, The Pitbull Syndicate
Shannon Barber
Shannon Barber
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
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
iccle
iccle
Im liking this forum very much indeed, thanks, updating my code right now

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.