0
\$\begingroup\$

I am trying to translate book: Game Physics Engine Development by Ian Millington to DirectXMath and faced with the code wich transform inertia tensor. Is it right to convert code from book like that: from book:

static inline void _transformInertiaTensor(glm::mat3 &iitWorld, const glm::quat &q, const glm::mat3 &iitBody, const glm::mat4 &rotmat) {
    float t4 = rotmat[0][0] * iitBody[0][0] + rotmat[0][1] * iitBody[1][0] + rotmat[0][2] * iitBody[2][0];
    float t9 = rotmat[0][0] * iitBody[0][1] + rotmat[0][1] * iitBody[1][1] + rotmat[0][2] * iitBody[2][1];
    float t14 = rotmat[0][0] * iitBody[0][2] + rotmat[0][1] * iitBody[1][2] + rotmat[0][2] * iitBody[2][2];
    float t28 = rotmat[1][0] * iitBody[0][0] + rotmat[1][1] * iitBody[1][0] + rotmat[1][2] * iitBody[2][0];
    float t33 = rotmat[1][0] * iitBody[0][1] + rotmat[1][1] * iitBody[1][1] + rotmat[1][2] * iitBody[2][1];
    float t38 = rotmat[1][0] * iitBody[0][2] + rotmat[1][1] * iitBody[1][2] + rotmat[1][2] * iitBody[2][2];
    float t52 = rotmat[2][0] * iitBody[0][0] + rotmat[2][1] * iitBody[1][0] + rotmat[2][2] * iitBody[2][0];
    float t57 = rotmat[2][0] * iitBody[0][1] + rotmat[2][1] * iitBody[1][1] + rotmat[2][2] * iitBody[2][1];
    float t62 = rotmat[2][0] * iitBody[0][2] + rotmat[2][1] * iitBody[1][2] + rotmat[2][2] * iitBody[2][2];

    iitWorld[0][0] = t4 * rotmat[0][0] + t9 * rotmat[0][1] + t14 * rotmat[0][2];
    iitWorld[0][1] = t4 * rotmat[1][0] + t9 * rotmat[1][1] + t14 * rotmat[1][2];
    iitWorld[0][2] = t4 * rotmat[2][0] + t9 * rotmat[2][1] + t14 * rotmat[2][2];
    iitWorld[1][0] = t28 * rotmat[0][0] + t33 * rotmat[0][1] + t38 * rotmat[0][2];
    iitWorld[1][1] = t28 * rotmat[1][0] + t33 * rotmat[1][1] + t38 * rotmat[1][2];
    iitWorld[1][2] = t28 * rotmat[2][0] + t33 * rotmat[2][1] + t38 * rotmat[2][2];
    iitWorld[2][0] = t52 * rotmat[0][0] + t57 * rotmat[0][1] + t62 * rotmat[0][2];
    iitWorld[2][1] = t52 * rotmat[1][0] + t57 * rotmat[1][1] + t62 * rotmat[1][2];
    iitWorld[2][2] = t52 * rotmat[2][0] + t57 * rotmat[2][1] + t62 * rotmat[2][2];
}

to my DirectX code:

XMVECTOR orientation = XMQuaternionNormalize(XMLoadFloat4(&m_orientation));
XMMATRIX orientationMatrix = XMMatrixRotationQuaternion(orientation);

XMMATRIX inverseInertiaTensor = XMLoadFloat4x4(&m_inverse_inertia_tensor);
XMMATRIX inverseInertiaTensorInWorldSpace = XMMatrixMultiply(orientationMatrix, inverseInertiaTensor);
XMMATRIX inverseInertiaTensorWorld = XMMatrixMultiply(inverseInertiaTensorInLocalSpace, XMMatrixTranspose(orientationMatrix));
\$\endgroup\$
2
  • \$\begingroup\$ Have you tested your code? Does it appear to work? \$\endgroup\$
    – Mast
    Jun 16 at 16:19
  • 1
    \$\begingroup\$ By tests it seems that this code is fine. But I am afraid that the result is incorrect due to incorrect representation of the matrices(row major or column major and the sequence of multiplication) \$\endgroup\$
    – Greck
    Jun 16 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.