Jump to content
  • Advertisement
Sign in to follow this  

Skeletal Animation Math

This topic is 2145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I developed a 2D skeletal animation system which was working until recently. We added a couple of new animations which did not work. I found that the quaternions output from 3DS Max gave rotations on two axes instead of one - one rotation of 90 degrees, followed by the rotation I need. So I set about fixing the problem.

My suspicions lead me to the quaternion to matrix conversion, which I believed was now fixed, but I am not sure. The output now matches the output of a calculator I found online, and my original problem seems to be solved. However, the animation system is broken and no animations work correctly. The character looks almost correct, except that bones are offset from their correct positions and at least some of them are rotating in the opposite direction to what they should.

The .X file output from the Max plugin is for a left handed coordinate system. I then convert this output to a smaller file format of my own. This is just to reduce the file size and improve loading times. No conversion of the coordinates or quaternions is done.

The matrix to quaternion conversion is as follows (matrix is in row column format)...

m_fElement[0][0] = 1 - 2 * ((q.y * q.y) + (q.z * q.z));
m_fElement[0][1] = 2 * ((q.x * q.y) - (q.z * q.w));
m_fElement[0][2] = 2 * ((q.x * q.z) + (q.y * q.w));
m_fElement[0][3] = 0;

m_fElement[1][0] = 2 * ((q.x * q.y) + (q.z * q.w));
m_fElement[1][1] = 1 - 2 * ((q.x * q.x) + (q.z * q.z));
m_fElement[1][2] = 2 * ((q.y * q.z) - (q.x * q.w));
m_fElement[1][3] = 0;

m_fElement[2][0] = 2 * ((q.x * q.z) - (q.y * q.w));
m_fElement[2][1] = 2 * ((q.y * q.z) + (q.x * q.w));
m_fElement[2][2] = 1 - 2 * ((q.x * q.x) + (q.y * q.y));
m_fElement[2][3] = 0;

m_fElement[3][0] = 0;
m_fElement[3][1] = 0;
m_fElement[3][2] = 0;
m_fElement[3][3] = 1;

Then the bone update which is called recursively...

***SFrame contains a translation vector and quaternion. All frames for every bone currently have both. Hopefully the rest is self explanatory, although something is clearly wrong here.

void CAnimation::SBone::Update(CAnimation *pAnimation, const SMatrix sParentMatrix)
    const SFrame *psFrame = pAnimation->GetCurrentKeyFrame(m_sIdentifier);
    if (psFrame->m_uiComponents & ANIMATION_COMPONENT_TRANSLATION)
        SMatrix sParentRotationMatrix = sParentMatrix;
        sParentRotationMatrix.m_fElement[3][0] = 0;
        sParentRotationMatrix.m_fElement[3][1] = 0;
        sParentRotationMatrix.m_fElement[3][2] = 0;
        SPoint3 sTranslation = psFrame->m_sTranslation * sParentRotationMatrix;
        m_sWorldMatrix.m_fElement[3][0] = -sTranslation.x + sParentMatrix.m_fElement[3][0];
        m_sWorldMatrix.m_fElement[3][1] = -sTranslation.y + sParentMatrix.m_fElement[3][1];
        m_sWorldMatrix.m_fElement[3][2] = sTranslation.z + sParentMatrix.m_fElement[3][2];
    SMatrix sLocalMatrix;
    if (psFrame->m_uiComponents & ANIMATION_COMPONENT_ROTATION)
        SMatrix sLocalRotation = SMatrix(psFrame->m_sRotation);// Create matrix from the rotation quaternion (see code above)
        sLocalMatrix = sParentMatrix * sLocalRotation;
    sLocalMatrix.m_fElement[3][0] = m_sWorldMatrix.m_fElement[3][0];
    sLocalMatrix.m_fElement[3][1] = m_sWorldMatrix.m_fElement[3][1];
    sLocalMatrix.m_fElement[3][2] = m_sWorldMatrix.m_fElement[3][2];
    SBone *psSibling = m_psSibling;
    if (psSibling)
        psSibling->Update(pAnimation, sParentMatrix);
    if (m_psChild)
        m_psChild->Update(pAnimation, sLocalMatrix);
    m_sWorldMatrix.m_fElement[0][0] = sLocalMatrix.m_fElement[0][0];
    m_sWorldMatrix.m_fElement[0][1] = sLocalMatrix.m_fElement[0][1];
    m_sWorldMatrix.m_fElement[0][2] = sLocalMatrix.m_fElement[0][2];
    m_sWorldMatrix.m_fElement[1][0] = sLocalMatrix.m_fElement[1][0];
    m_sWorldMatrix.m_fElement[1][1] = sLocalMatrix.m_fElement[1][1];
    m_sWorldMatrix.m_fElement[1][2] = sLocalMatrix.m_fElement[1][2];
    m_sWorldMatrix.m_fElement[2][0] = sLocalMatrix.m_fElement[2][0];
    m_sWorldMatrix.m_fElement[2][1] = sLocalMatrix.m_fElement[2][1];
    m_sWorldMatrix.m_fElement[2][2] = sLocalMatrix.m_fElement[2][2];
    SPoint3 sOffset = SPoint3(-m_sFrame.m_fElement[3][0], -m_sFrame.m_fElement[3][1], m_sFrame.m_fElement[3][2]) * sLocalMatrix;
    m_sWorldMatrix.m_fElement[3][0] += sOffset.x;
    m_sWorldMatrix.m_fElement[3][1] += sOffset.y;
    m_sWorldMatrix.m_fElement[3][2] += sOffset.z;
    // HACK - Not sure why this was necessary
    m_sWorldMatrix.m_fElement[3][0] = -m_sWorldMatrix.m_fElement[3][0];

Finally, the bones (images) are rendered with the following rotation ...

***sMatrix is the bone's m_sWorldMatrix

-atan2(-sMatrix.m_fElement[2][0], sMatrix.m_fElement[0][0]);

I would really appreciate any help with fixing this problem because my math skills are not great and I can't get my head around what the issue is. Please let me know if anything is unclear.  Due to contractual obligations, I cannot post screenshots or more complete code.  I think I have posted all the important bits of code.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!