DirectX - Quaternion



The rotations of every graphic element are done with the help of xwing as per the rotations around the 3D axis, i.e., X, Y and Z axis. Xwing is something which is considered as a unique feature of rotating a particular object in quaternion. The only problem here is that it includes same dimensions with respect to matrices. For such a situation, a user or a developer will need a particular solution which would solve the problem.

Deriving a total rotation from the mentioned 3 separate values of each axis with reference to trigonometric formulae is something which should be discussed and it is included in further analysis.

Quaternion is matrix like structure which provides only one rotation. A quaternion is considered to be very easy to use from developer point of view. Consider for the following examples where where we declare vectors with mentioned below which describes the standard syntax −

Vector3 xwingPosition = new Vector3(18, 11, -3);
#Vector definition with three co-ordinates
Quaternion xwingRotation = Quaternion.Identity;
#Creation of identity with respect to rotation

Consider the CreateModel method where the user can change the world matrix for our xwing, with respect to correct location, and correct rotation −

Matrix worldMatrix = Matrix.CreateScale(0.07f, 0.0015f, 0.12 f) 
   * Matrix.CreateRotationY(MathHelper.Pi) 
   * Matrix.CreateTranslation(xwingPosition);

The mentioned code seems to be complex, but it is quite easy.

  • The mesh is translated to its correct position with the help of matrix.

  • The xwing is rotated among the rotation stored in the xwingRotation quaternion.

  • Once the above steps are achieved, it is rotated along 180 degrees to compensate the opposite direction stored inside the model. And finally, the model is scaled down so it fits nicely in our scene.

The code snippet for creating a dimension of camera is mentioned below −

private void UpdateCamera() { 
   Vector3 campos = new Vector3(0, 0.1f, 0.6f); 
}

The sample code which is created is mentioned below −

D3DXQUATERNION qZ;
   D3DXQUATERNION qY;
   D3DXQUATERNION qX;
   D3DXQUATERNION qOrient;
   D3DXQUATERNION qTotal;
   D3DXQuaternionIdentity(&qZ);
   D3DXQuaternionIdentity(&qY);
   D3DXQuaternionIdentity(&qX);
   D3DXQuaternionIdentity(&qOrient);
   D3DXVECTOR3 axisZ(0,0,1);
   D3DXVECTOR3 axisY(0,1,0);
   D3DXVECTOR3 axisX(1,0,0);
   D3DXQuaternionRotationAxis(&qZ,&axisZ,ga->getRotation().z);
   D3DXQuaternionRotationAxis(&qY,&axisY,ga->getRotation().y);
   D3DXQuaternionRotationAxis(&qX,&axisX,ga->getRotation().x);
   D3DXQuaternionNormalize(&qZ,&qZ);
   D3DXQuaternionNormalize(&qY,&qY);
   D3DXQuaternionNormalize(&qX,&qX);
qTotal=qY*qX*qZ;
   D3DXMATRIX rotation;
   D3DXMatrixRotationQuaternion(&rotation,&qTotal);
   world=scale*rotation*move;
Advertisements