I want to get the actual direction of a bone in the world space.
What I tried was using absolute position of child bone.
function getBoneDirection {
var childBones = bone.getChildren();
if (childBones.length == 0) {
console.log("no child bone");
return new BABYLON.Vector3(0,0,0);
}
var childBone = childBones[0];
var childBonePose = childBone.getAbsolutePosition(mesh);
var thisBonePose = bone.getAbsolutePosition(mesh);
var boneDirection = childBonePose.subtract(thisBonePose);
boneDirection = BABYLON.Vector3.Normalize(boneDirection);
}
However, in this case, it is impossible the get the direction of a bone if it doesn’t have a child.
Hi @Keunwoo_Park, I think something like this could work:
1. Getting absolute position, rotation, and scale of bone:
const scale = new BABYLON.Vector3();
const rotation = new BABYLON.Quaternion();
const position = new BABYLON.Vector3();
const matrix = mesh.skeleton.bones[0].getWorldMatrix();
const matrix2 = mesh.getWorldMatrix();
const matrix3 = matrix.multiply(matrix2);
matrix3.decompose(scale, rotation, position);
This should find the absolute position, rotation, and scale of bones[0], which is part of the skeleton assigned to mesh.
2. Getting direction of bone (similar to getForwardRayToRef in Babylon source code):
const forward = new BABYLON.Vector3(1, 0, 0);
// I think using matrix3 from above should work
const forwardWorld = BABYLON.Vector3.TransformNormal(forward, matrix3);
const direction = BABYLON.Vector3.Normalize(forwardWorld);
Ok. Then I’m confused about the ‘localAxis’. How should I set the local axis parameter?
When I set them as just Y axis, the results of getDirection is Y axis. https://playground.babylonjs.com/#9JWAWG#10
getDirection does not give the direction of a bone but the transform of a passed (local) direction to world space. It returns simply bone_world_matrix * passed_axis.
It could match the bone direction in some specific cases (depending on the bone world matrix and the passed axis), but in general it won’t.
Sorry, when getDirection was written it always returned the world direction from a local direction. The bone class has changed a lot since then and those changes seem to have broken this function in some situations. An issue should be created on github to fix this.