title: Calculating the final bone transformation for a skeletal animation
body: <p>I have a simple model with a simple skeletal structure that I made in blender. Here's how it looks: </p>
  
  <p><img src="https://i.imgur.com/DVYdJOK.gif" alt="img"></p>
  
  <p>And here's the hierarchy in blender:</p>
  
  <p><img src="https://i.imgur.com/OGpEMOJ.png" alt="img"></p>
  
  <p>As you can see it has two bones: One that goes halfway up the rectangular box ("Bone"), that is completely stationary. And another bone ("Bone.001") that goes from the halfway point and up to the top, that rotates.</p>
  
  <p>I've imported the mesh using AssImpNet, and extracted the rotation, scaling and position keys from the animation node channels. When I apply those transformations to the mesh, I get this result (colored by bone weight): </p>
  
  <p><img src="https://i.imgur.com/yspaZa7.gif" alt="img"></p>
  
  <p>The motion/animation seems to play correctly, so I believe this part works correctly. Now this is where my understanding starts to break down, but I believe the crucial part I'm missing is calculating the "inverse bind pose" (I've seen a few names for it), and applying that to the bone transformations that I feed into my shader as well. But so far I haven't been able to find exactly what it is that I need to extract from AssImp's format, and multiply together to get the correct final transformation. I've only found vague explanations about traversing the node tree and "undoing" transformations from each parent node, or something.</p>
  
  <p>Here's what I tried, which doesn't seem to be working:</p>
  
  <pre><code>i thought that, for the base bone ("Bone"), i would need:
  - the global inverse transform
  - the node transform of the node with name "Bone"
  - the rotation/position/scale keys from the animation channel with name "Bone"
  - the bone offset from Meshes.Bones.OffsetMatrix from the bone named "Bone"
  </code></pre>
  
  <p>and then multiply them together in that order.</p>
  
  <pre><code>similarly for "Bone.001":
  - the global inverse transform
  - the node transform of the node with name "Bone"
  - the rotation/position/scale keys from the animation channel with name "Bone"
  - the node transform of the node with name "Bone.001"
  - the rotation/position/scale keys from the animation channel with name "Bone.001"
  - the bone offset from Meshes.Bones.OffsetMatrix from the bone named "Bone.001"
  </code></pre>
  
  <p>My attempt at implementing this (hard coding the indexes for now just to try to get things working), note that it's using C#/AssImpNet, so the naming conventions are a bit different from C++/AssImp:</p>
  
  <pre><code>        // "Bone"
          public Matrix4 Bone0Transform(double time)
          {
              var bone0 = MathUtils.ConvertMatrix(Scene.RootNode.Children[1].Children[0].Transform);
  
              var frame0 = GetTransformedFrame(1, TimeToFrame(1, time));
  
              var global = MathUtils.ConvertMatrix(Scene.RootNode.Transform).Inverted();
  
              var offset0 = MathUtils.ConvertMatrix(Scene.Meshes[0].Bones[0].OffsetMatrix);
  
              var total = global * bone0 * frame0 * offset0;
  
              return total;
          }
  
          // "Bone.001"
          public Matrix4 Bone1Transform(double time)
          {
              var bone0 = MathUtils.ConvertMatrix(Scene.RootNode.Children[1].Children[0].Transform);
  
              var bone1 = MathUtils.ConvertMatrix(Scene.RootNode.Children[1].Children[0].Children[0].Transform);
  
              var frame0 = GetTransformedFrame(1, TimeToFrame(1, time));
              var frame1 = GetTransformedFrame(2, TimeToFrame(2, time));
  
              var global = MathUtils.ConvertMatrix(Scene.RootNode.Transform).Inverted();
  
              var offset1 = MathUtils.ConvertMatrix(Scene.Meshes[0].Bones[1].OffsetMatrix);
  
              var total = global * bone0 * frame0 * bone1 * frame1 * offset1;
  
              return total;
          }
  </code></pre>
  
  <p>GetTransformedFrame returns a Matrix4 combining the scale, rotation and position keys for the frame that corresponds to the current time, and on its own gives the result you can see in the gif where the box is colored red/green.</p>
  
  <p>All this gives me is an obviously incorrect result: 
   <img src="https://i.imgur.com/yMcHCtZ.gif" alt="img"></p>
  
  <p>So my question is this: Is my understanding of how to calculate the final bone transformations wrong? If so, what is the correct way of doing it?</p>