• Create Account

Member Since 12 Mar 2009
Offline Last Active Sep 10 2013 02:06 AM

### Skeletal Animation Math

01 September 2013 - 02:58 AM

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.

### Lighting For 2D OpenGL Game

13 March 2009 - 12:10 AM

I've been using OpenGL for a while to do 2D graphics. I'm doing a game conversion at the moment. The original version was all software rendering and used some nasty techniques for this effect - copying the frame buffer to a texture. I still don't know a great deal about OpenGL, and I've come across a problem with this conversion. This is how the original software version looks. I'll do my best to explain this. At times the game turns to night with a gradual blue tint over the background and other sprites. There are a number of spot lights around the map. Some of the spot light rectangles overlap. At the centre of the lights, the original background coloring is seen, graduating outwards to the blue tint. I hope that is clear. Now I need to get this working in OpenGL. The light images are circular, graduating from solid white to completely transparent, and there is a semi-transparent (alpha varying over time, as it gets darker) blue rectangle all over the play area. As it is, it doesn't work in OpenGL. I'm thinking I can do this with alpha blending, but I've not managed to get it working. Copying anything back from the frame buffer certainly isn't an option. Any ideas?

PARTNERS