I am trying to create a rag-doll physics to my skinned meshes. And, I meet some problems... Hope you can help me.
Here is how I setup rigid bodies for rag-doll:
final_matrix = parentJoint->final_matrix; // this is bone matrix
final_matrix.SetInvert(final_matrix); // invert it
final_matrix = model_transformation * final_matrix; // multiplay by model transformation
rigid_body->setRotation(final_matrix); // it's 3x3 matrix
rigid_body->setPosition(…) // I get this by bone_pos in world + bone_length * 0.5
all rigid bodies is in place with correct rotation (according current animation's bones)
Now the question is how to convert rigid body transformation back to joints? As I understood that is how rag doll works: skinned mesh joints are controlled by rigid bodies which are connected with physics joints. My animation code is like this:
transform.setRotationRadians( rotVec );
transform.setTranslation( transVec );
Matrix relativeFinal( pJoint->relative ); // joint's relative matrix
relativeFinal.postMultiply( transform );
if ( pJoint->parent == -1 )
pJoint->final.set( relativeFinal.getMatrix());
else
{
pJoint->final.set( m_pJoints[pJoint->parent].final.getMatrix()); // get parent's joint final matrix
pJoint->final.postMultiply( relativeFinal ); // and multiplay it by current animation keys (translates and rotations)
}
I have these from rigid body:
rigid_body->GetRotationMatrix()
rigid_body->GetPosition()
and I guess they should be converted to bone space (because they are in world space), right?
and what about apply to bone, should this be enough: pJoint->final.set(correct_matrix_from_rigid_body); ?
What should I do with bones which doesn't have rigid body? Should they still get bone matrix from parent?