# Quaternion handedness issue

At least I think it is. I have the following functions:

• kmQuaternionRotationAxis - Constructs a Quaternion from an axis and angle
• kmMat4RotationQuaternion - Constructs a 4x4 matrix from a quaternion
• kmMat4RotationAxisAngle - Constructs a 4x4 matrix from an axis and angle

My problem is, when I run the following code:

kmQuaternion q;
kmMat4 initialized;
kmMat4 quaternion_rotated;

kmMat4RotationQuaternion(&quaternion_rotated, &q);


I would expect *quaternion_rotated* to match initialized, but they don't.

Initialized:

 | 0 -1  0  0 |
| 1  0  0  0 |
| 0  0  1  0 |
| 0  0  0  1 |


quaternion_rotated:

 | 0  1  0  0 |
| -1 0  0  0 |
| 0  0  1  0 |
| 0  0  0  1 |


The contents of initialized looks like I would expect, but I can't figure out how quaternion_rotated is wrong. I've included all the code below, where is my conversion code wrong?

kmQuaternion* kmQuaternionRotationAxis(kmQuaternion* pOut, const kmVec3* pV, kmScalar angle)
{
kmScalar rad = angle * 0.5f;

pOut->x = pV->x * scale;
pOut->y = pV->y * scale;
pOut->z = pV->z * scale;

kmQuaternionNormalize(pOut, pOut);

return pOut;
}

kmMat4* kmMat4RotationAxisAngle(kmMat4* pOut, const kmVec3* axis, kmScalar radians)
{

kmVec3 normalizedAxis;
kmVec3Normalize(&normalizedAxis, axis);

pOut->mat = rcos + normalizedAxis.x * normalizedAxis.x * (1 - rcos);
pOut->mat = normalizedAxis.z * rsin + normalizedAxis.y * normalizedAxis.x * (1 - rcos);
pOut->mat = -normalizedAxis.y * rsin + normalizedAxis.z * normalizedAxis.x * (1 - rcos);
pOut->mat = 0.0f;

pOut->mat = -normalizedAxis.z * rsin + normalizedAxis.x * normalizedAxis.y * (1 - rcos);
pOut->mat = rcos + normalizedAxis.y * normalizedAxis.y * (1 - rcos);
pOut->mat = normalizedAxis.x * rsin + normalizedAxis.z * normalizedAxis.y * (1 - rcos);
pOut->mat = 0.0f;

pOut->mat = normalizedAxis.y * rsin + normalizedAxis.x * normalizedAxis.z * (1 - rcos);
pOut->mat = -normalizedAxis.x * rsin + normalizedAxis.y * normalizedAxis.z * (1 - rcos);
pOut->mat = rcos + normalizedAxis.z * normalizedAxis.z * (1 - rcos);
pOut->mat = 0.0f;

pOut->mat = 0.0f;
pOut->mat = 0.0f;
pOut->mat = 0.0f;
pOut->mat = 1.0f;

return pOut;
}

kmMat4* kmMat4RotationQuaternion(kmMat4* pOut, const kmQuaternion* pQ)
{
double xx = pQ->x * pQ->x;
double xy = pQ->x * pQ->y;
double xz = pQ->x * pQ->z;
double xw = pQ->x * pQ->w;

double yy = pQ->y * pQ->y;
double yz = pQ->y * pQ->z;
double yw = pQ->y * pQ->w;

double zz = pQ->z * pQ->z;
double zw = pQ->z * pQ->w;

pOut->mat = 1 - 2 * (yy + zz);
pOut->mat = 2 * (xy - zw);
pOut->mat = 2 * (xz + yw);
pOut->mat = 0;

pOut->mat = 2 * (xy + zw);
pOut->mat = 1 - 2 * (xx + zz);
pOut->mat = 2 * (yz - xw);
pOut->mat = 0.0;

pOut->mat = 2 * (xz - yw);
pOut->mat = 2 * (yz + xw);
pOut->mat = 1 - 2 * (xx + yy);
pOut->mat = 0.0;

pOut->mat = 0.0;
pOut->mat = 0.0;
pOut->mat = 0.0;
pOut->mat = 1.0;

return pOut;
}


Your problem is that you didn't implement kmMat4RotationAxisAngle like this:

kmMat4* kmMat4RotationAxisAngle(kmMat4* pOut, const kmVec3* axis, kmScalar radians)
{
kmQuaternion quat;