I have an avatar with a skeleton and I am trying to find the location of each joint in reference to the “root” bone. To start out I am using the skeleton in the default bind position and it’s unclear how the forward kinematics for the bone locations are being calculated. This is how I am starting with a chain:
public Vector3f testForwardKinematics(String boneString) {
Bone bone = skeleton.getBone(boneString);
List<Vector3f> vectors = new ArrayList<>();
List<Quaternion> rotations = new ArrayList<>();
Bone parent;
while((parent = bone.getParent()) != null) {
Vector3f vector = new Vector3f(bone.getBindPosition());
Quaternion rotation = new Quaternion(bone.getBindRotation());
// Insert data at the front of the list
vectors.add(0, vector);
rotations.add(0, rotation);
bone = parent;
}
// Add extra quaternion for "parent" of root.
rotations.add(0, Quaternion.IDENTITY);
Vector3f result = new Vector3f();
Quaternion totRot = new Quaternion();
for(int i=0; i<vectors.size(); i++) {
totRot = rotations.get(i).inverse().mult(totRot);
Vector3f nextVec = totRot.mult(vectors.get(i));
result.addLocal(nextVec);
}
return result;
}
Basically, what this code does is it takes a bone and cycles backwards through all of the bones parents to make a list of vectors and quaternions from the root bone to the last bone in question. It the marches through this list and keeps a running “total rotation” of all the bones as they compound down the chain.
The problem that I am having is that this code works great for the spine of my avatar, but breaks down whenever there is more than one bone coming out of a junction. Basically, this does not correctly compute the forward kinematics of the arms or legs because these chains “branch off” the spine chain.
I tried looking in the jMonkey source to see how the forward kinematics were being computed, but I can’t figure out exactly where this is being computed. Could someone give me some insight into how I located the position of each bone in relation to the root node attachment?