# XYZ Rotations Without Quaternions?

## Recommended Posts

Hi guys, I'm trying to replace my quaternion rotations with matrix rotations (please don't ask why). I can't seem to get it right. Here is the code:

//	axes

float3 xAxis(1.0f, 0.0f, 0.0f);
float3 yAxis(0.0f, 1.0f, 0.0f);
float3 zAxis(0.0f, 0.0f, 1.0f);

//	create the quaternions

float4 qX, qY, qZ, qComp, qTemp;

quaternionRotationNormal(&qX, &xAxis, rot_x);
quaternionRotationNormal(&qY, &yAxis, rot_y);
quaternionRotationNormal(&qZ, &zAxis, rot_z);

//	combine (multiply) and normalize

quaternionMultiply(&qTemp, &qX, &qY);
quaternionMultiply(&qComp, &qTemp, &qZ);

quaternionNormalize(&qComp);

//	create rotation matrix from quaternions

float4x4 mR;

matrixRotationQuaternion(&mR, &qComp);

Here is what I've come up with so far:

//	axes

float3 xAxis(1.0f, 0.0f, 0.0f);
float3 yAxis(0.0f, 1.0f, 0.0f);
float3 zAxis(0.0f, 0.0f, 1.0f);

//	create x rotation matrix

float4x4 mX;
matrixRotationX(&mX, rot_x);

//	transform y and z-axes by x matrix

transformVector(&yAxis, &mX);
transformVector(&zAxis, &mX);

//	create y rotation matrix

xmfloat4x4 mY;
matrixRotationAxis(&mY, &yAxis, rot_y);

//	transform z-axis by y matrix

transformVector(&zAxis, &mY);

//	create z rotation matrix

xmfloat4x4 mZ;
matrixRotationAxis(&mZ, &zAxis, rot_z);

//	combine

xmfloat4x4 mTemp, mR;

matrixMultiply(&mTemp, &mX, &mY);
matrixMultiply(&mR, &mTemp, &mZ);

The behavior should be (almost) identical but it isn't. I hope someone can help. Thanks.

##### Share on other sites

The first thing that jumps out is that you seem to be performing fundamentally different operations in each case. In the quaternion version, you appear to be performing a standard Euler-angle conversion. In the matrix version, however, you appear to be doing something else - I'm not sure what the intent is, but you're rotating (some of) the axes themselves before building rotation matrices from them.

In any case, orientation representation aside, the two code blocks don't do the same thing, so I wouldn't expect the output to be the same (allowing for numerical error of course). There are other things that could cause the results to differ - for example, if the math library uses different multiplication order conventions for matrices and quaternions. I think most likely though it's just that the two code blocks are doing different things.

##### Share on other sites
Posted (edited)

Duh, it's just this:

matrixRotationX(&mX, rot_x);
matrixRotationY(&mY, rot_y);
matrixRotationZ(&mZ, rot_z);

matrixMultiply(&mTemp, &mX, &mY);
matrixMultiply(&mR, &mTemp, &mZ);

##### Share on other sites

Hi, I know I'm doing something wrong. I want to do what the first code block does but without quaternions. That is, build a rotation matrix from rotation angles (x, y, z) that results in the same transform as the matrix in the first code block.

It looks to me like you are doing something different though.  In the quaternion case you are multiplying the rotations together normally. In the matrix case you are rotating axis around each other.  Why don't you try just generating the X, Y, and Z rotation matrices and simply multiplying them together in order like you did with the quaternions.  I didn't sit down and try to figure out if it's the same result, but I would try at least doing the same sequence of calculations before trying to optimize it.

## Create an account

Register a new account

• 18
• 25
• 11
• 21
• 16