Physics control of a 3D model

I was wondering if there are any examples of controlling a 3D model's skeleton using an underlying physics model.





I understand that I need to translate the world coordinates of the physics nodes to the appropriate local coordinates in the skeletal model, but my model ends up being unstable as I move further from the root.





For example, if I have a 3D model with a linear skeleton



bone(0)

      bone(1)

          bone(2)

              bone(4)



and a physics model with 3 DynamicPhysicsNodes connected by rotational joints anchored between them, then I use the following code fragment to update the translations and rotations of the bones.  The first if statement aligns the root bone to the front of the first PhysicsNode and the the rest get aligned to the locations of the joints.





for (int i = 0; i < bones.size(); i++) {
   if (i == 0) {
      Segment thisSegment = segments.get(i);
      translation = new Vector3f(0, 0, thisSegment
            .getLength());

      Quaternion rotation = thisSegment.getLocalRotation();
      translation.divideLocal(-2f);
      res.multLocal(translation);
      translation.addLocal(thisSegment.getLocalTranslation());

      prevSegment = thisSegment;
   }
   else{
      Segment thisSegment = segments.get(i-1);

      translation = new Vector3f(0, 0, thisSegment
            .getLength());

      Quaternion rotation = prevSegment.getLocalRotation();
         
      rotation = rotation.inverse().multLocal(thisSegment.getLocalRotation());

      segRotation.multLocal(translation);
      rotation.multLocal(translation);
      
      prevSegment = thisSegment;

   }
   bones.get(i).getLocalRotation().set(rotation);
   bones.get(i).getLocalTranslation().set(translation);
}
bones.get(0).propogateBoneChange(true);





It works  fine for a few in a straight line but starts to jitter the further away from the root I get.

Any help would be appreciated

Hello i would like to know if you found a solution to your problem.

Thank you.