# Simple Quaternion question ?

## Recommended Posts

Hey, In order to implement a Steering Behavior demop in 3D, I started coding a very simple 3D object composed of : - 1 arrow - 1 sphere - 1 point The Steering Behavior implementation wants the point to be moving on the phere, and the sphere to *always* be in front of the arrow. So far, I could only render my arrow correctly, always facing its velocity, thanks to this previous post. Now, I am struggling in order to correctly display the sphere. Here is what I did so far:
D3DXMatrixIdentity(&matWorld);
D3DXMatrixIdentity(&matRotation);
D3DXMatrixTranslation(&matWorld, vArrowPosition.x, vArrowPosition.y, vArrowPosition.z);

D3DXQUATERNION qR;
D3DXQuaternionRotationMatrix(&qR, &matArrowRotation);
//D3DXQuaternionRotationYawPitchRoll(&qR, ?, ?, ?);
D3DXVECTOR3 MyVector = D3DXVECTOR3(fDistanceFromArrow, 0.0f, 0.0f);
D3DXMatrixAffineTransformation(&matRotation, 1.0f, &MyVector, &qR, NULL);
D3DXMatrixMultiply(&matWorld, &matRotation, &matWorld);

m_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);


Note that in a previous demo, I used the D3DXQuaternionRotationYawPitchRoll function instead of D3DXQuaternionRotationMatrix, and everything worked fine ; the previous demo was in 2D though. It looks that D3DXQuaternionRotationMatrix does not work too well for what I want to do. However, I have not found a way to use D3DXQuaternionRotationYawPitchRoll here, because I do not have the Yaw, Picth and Roll components of my matArrowRotation D3DXMATRIX (what is replaced by the '?' in the source code). Any suggestions ? Thank you in advance. Cheers StratBoy61

##### Share on other sites
You could avoid to do the matrix->quaternion->matrix conversion if you drop the use of D3DXMatrixAffineTransformation.

The invocation of that routine does no translation but a rotation center correction. It is (if I have not made an error; please check it) equivalent to
D3DXMatrixTranslation(&matWorld, vArrowPosition.x, vArrowPosition.y, vArrowPosition.z);D3DXMatrixTranslation(&temp, fDistanceFromArrow, 0.0f, 0.0f);D3DXMatrixMultiply(&matWorld, &temp, &matWorld);D3DXMatrixMultiply(&matWorld, &matArrowRotation, &matWorld);D3DXMatrixTranslation(&temp, -fDistanceFromArrow, 0.0f, 0.0f);D3DXMatrixMultiply(&matWorld, &temp, &matWorld);m_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);

EDIT: I assume matArrowRotation to be initialized well, of course, since the OP's code snippet does so as well.

##### Share on other sites
I did not understand your answer, but it seems to work. Enough to get a rate ++.
Thanks !
StratBoy61

##### Share on other sites
When you look at the documentation of D3DXMatrixAffineTransformation, you could see what it does with the supplied parameters. My suggestion does nothing than to simulate what D3DXMatrixAffineTransformation does, but drops the unused parts and (that is the actual reason!) is able to work with a rotation matrix instead of a quaternion.

Inside D3DXMatrixAffineTransformation the quaternion is converted into a matrix, and you have to convert the already given matrix into a quaternion to invoke the routine. That is an unnecessary matrix->quat->matrix conversion. Hence the approach I've chosen.

However, it seems to work :)

##### Share on other sites
Quote:
 Original post by haegarrInside D3DXMatrixAffineTransformation the quaternion is converted into a matrix, and you have to convert the already given matrix into a quaternion to invoke the routine. That is an unnecessary matrix->quat->matrix conversion.

Makes sense. It still does not tell me why what I did previously did not work, but it is okay. The D3DXQuaternionRotationMatrix transformation did not seem to do what I expected.
Cheers
StratBoy61

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627689
• Total Posts
2978659

• 18
• 14
• 12
• 10
• 12