I'm trying to do the exact same thing in Unity. I think I'm close, but the bones always end up translated some fixed (but seemingly random) distance from the physics bounding box. I tried to reproduce some of the code/formulas from this thread. Unity has a built-in Matrix4x4 class, so I used that to build the matrix.
void UpdateBones(SkeletonAnimation skeletonAnimation) {
for(int i=0; i < bones.Count; i++) {
Bone bone = skeleton.bones[i];
Bone boneParent = skeleton.bones[i].parent;
if (boneParent == null)
boneParent = bone;
Matrix4x4 matrix = new Matrix4x4();
matrix.SetRow (0, new Vector4(boneParent.M00, boneParent.M01, boneParent.worldX, 0));
matrix.SetRow (1, new Vector4(boneParent.M10, boneParent.M01, boneParent.worldY, 0));
matrix.SetRow (2, new Vector4(0, 0, 1, 0));
matrix.SetRow (3, new Vector4(0, 0, 0, 1));
float boundingBoxX = bones[i].transform.position.x; // This is the x coord of the GameObject that this bone corresponds to
float boundingBoxY = bones[i].transform.position.y;
bone.x = boundingBoxX * matrix.inverse[0,0] + boundingBoxY * matrix.inverse[0,1] + matrix.inverse[0,2];
bone.y = boundingBoxX * matrix.inverse[1,0] + boundingBoxY * matrix.inverse[1,1] + matrix.inverse[1,2];
}
}
Does anyone see what I'm doing wrong? I'm really wishing I paid more attention in Linear Algebra now.