0
\$\begingroup\$

I was implementing some quaternion rotations in too my infant 3d game engine and the rotations were really strange? Excuses my cluelessness.

Anyways this is what I have:

static XMVECTOR XMConvertToQuaternion(XMFLOAT3 axis, float radian)
{
    axis = XMNormalize3(axis);
    XMVECTOR tempAxis = XMVectorSet(axis.x, axis.y, axis.z, 1);
    XMVECTOR q = XMQuaternionRotationAxis(tempAxis, radian);
    return q;
}

Then later on down when I apply the quaternion to the world matrix I have:

XMMATRIX scaling = XMMatrixScaling(scale.x, scale.y, scale.z);
XMMATRIX rotateX = XMMatrixRotationX(radianRotation.x);
XMMATRIX rotateY = XMMatrixRotationY(radianRotation.y);
XMMATRIX rotateZ = XMMatrixRotationZ(radianRotation.z);

XMMATRIX rotateQX = XMMatrixRotationQuaternion(MathHelper::XMConvertToQuaternion(axis, radianRotation.x));
XMMATRIX rotateQY = XMMatrixRotationQuaternion(MathHelper::XMConvertToQuaternion(axis, radianRotation.y));
XMMATRIX rotateQZ = XMMatrixRotationQuaternion(MathHelper::XMConvertToQuaternion(axis, radianRotation.z));
XMMATRIX translate = XMMatrixTranslation(translation.x, translation.y, translation.z);
XMStoreFloat4x4(&mWorld, scaling*rotateQX*rotateQY*rotateQZ*translate);

I did get a rotation. However, the character is rotating very strangely.

\$\endgroup\$
1

2 Answers 2

0
\$\begingroup\$

the way to create your Quaternion rotations matrices (I assume from euler angles) look funny, I would expect you use unit vectors of each axis rather than axis

XMMATRIX rotateQX = XMMatrixRotationQuaternion(MathHelper::XMConvertToQuaternion(XMFLOAT3(1, 0, 0), radianRotation.x));
XMMATRIX rotateQY = XMMatrixRotationQuaternion(MathHelper::XMConvertToQuaternion(XMFLOAT3(0, 1, 0), radianRotation.y));
XMMATRIX rotateQZ = XMMatrixRotationQuaternion(MathHelper::XMConvertToQuaternion(XMFLOAT3(0, 0, 1), radianRotation.z));
\$\endgroup\$
0
\$\begingroup\$

On this line:

XMStoreFloat4x4(&mWorld, scaling*rotateQX*rotateQY*rotateQZ*translate);

does changing the order to:

XMStoreFloat4x4(&mWorld, translate*rotateQX*rotateQY*rotateQZ*scaling);

make a difference? If you meant to rotate about the origin then translate outwards then you need to be careful with your ordering, though I'm making assumptions about the XM library.

\$\endgroup\$
4
  • \$\begingroup\$ No, that just made it worse. Haha \$\endgroup\$ Sep 30, 2014 at 18:36
  • \$\begingroup\$ Try taking out the translation and scaling completely. Then test each axis separately if necessary. Results? \$\endgroup\$
    – parar
    Sep 30, 2014 at 19:27
  • \$\begingroup\$ I just got the same results. Must be how I got the quaternion. \$\endgroup\$ Sep 30, 2014 at 21:57
  • \$\begingroup\$ I don't know if you just copied your code wrong, but you are currently using the same axis for each quaternion rotation, which is not what you want to do. \$\endgroup\$
    – parar
    Oct 1, 2014 at 7:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .