I'm back working on my ragdoll system and I have problems.
I have a ragdoll made of ragdoll parts.
Each ragdoll part is just a parallelepiped entity which visually simulates the bone.
Each ragdoll part is in world space (they are parented only with constraints).
I have a characterEntity and its armature.
The armature should "follow" the ragdoll (and this is what is not quite working).
First, at the start of the game, I position and rotate each ragdoll part, copying the position and rotation from the bones.
Then, I let the physics run and each step I do the inverse: I rotate every bone to match the ragdoll part rotation, so the mesh should animate and fall down to the floor.
It works almost fine but there is some kind of offset, e.g. the leg's bone is ~15° off on the X axis in respect to the ragdoll part, the arm's bone ~ the same, etc.
The ragdoll is currently only about the skeleton, so it's not about e.g. the arm not being perfectly in line with the bone or uncorrectly colliding: I'm making a ragdoll of the skeleton only. Only when this works, I will attach actual body parts for the collisions.
My code is actually pretty long, complex, and separated into classes and files, I just can't post it all. But I'm pretty sure the problem is only about matrices and rotation, and lies here:
[...]
//Ragdoll's part copys rotation from bone (works perfectly fine)
MMatrix4x4 boneWorldMatrix = (*characterEntity->getMatrix()) * (*bone->getMatrix());
ragdollPart->setEulerRotation(boneWorldMatrix.getEulerAngles());
[...]
//Bone copys from Ragdoll's part (DOESN'T WORK)
MMatrix4x4 myMatrix = characterEntity->getMatrix()->getInverse() * (*ragdollPart->getMatrix());
MObject3d* parentBone = bone->getParent();
if(parentBone)
myMatrix = parentBone->getMatrix()->getInverse() * myMatrix;
bone->setEulerRotation(myMatrix.getEulerAngles());
[...]
The grey lines are the ragdoll parts. They are in "x-ray mode".
The red little squares are the bones (the head of every bone).
In the image on the left, the game is just started, and everything is right. The ragdoll parts are positioned and rotated copying the bones position and rotation from the armature.
As soon as I activate the physics (only one step of physics), you can see how the bones don't match the ragdoll parts correctly.