Hey @slin, if your troll model has (1, 1, 1) scaling, attachToBone
is a good way to attach your weapon to the troll’s hand bone. (Without (1, 1, 1) scaling, attachToBone
seems to change the weapon’s scale to that of the parent, which can be tricky to work with, especially if you plan on the troll picking up and dropping weapons)
The Playgrounds in ArcRotateCamera's Radius affects the Rotation/Position of attachToBone Meshes may be a good reference
However, it sounds like you already made the weapon move relative to its parent bone. I’m guessing that you achieved this through setting the position and rotation(Quaternion) of the weapon to that of the parent bone before every frame render? If so, all you would need is to apply an additional position and rotation offset to move the weapon inside the troll’s hand
Currently, I’m doing something like this that works fine, though there may be a more efficient way to accomplish this:
// You need to find (perhaps by trial and error) a, b, c, x, y, z such
// that the weapon is placed directly in the troll's hand. I'm currently
// using dat.gui to tune these values in real-time to quickly find these
const offsetPosition = new BABYLON.Vector3(a, b, c);
const offsetRotation = new BABYLON.Vector3(x, y, z);
// Update location of weapon before every frame render
scene.registerBeforeRender(() => {
// Find absolute position and rotation of the trollMesh hand
trollMesh.computeWorldMatrix(true);
const scale = new BABYLON.Vector3();
const rotation = new BABYLON.Quaternion();
const position = new BABYLON.Vector3();
const matrix = trollMeshSkeleton.bones[handBoneIndex].getWorldMatrix();
const matrix2 = trollMesh.getWorldMatrix();
const matrix3 = matrix.multiply(matrix2);
matrix3.decompose(scale, rotation, position);
// Set weaponMesh's absolute position and rotation to those of the
// trollMesh hand
weaponMesh.setAbsolutePosition(position);
weaponMesh.rotationQuaternion = rotation;
// Apply the position and rotation offsets found above (a, b, c, x, y, z)
const rotationMatrix = new BABYLON.Matrix();
rotation.toRotationMatrix(rotationMatrix);
const translation = BABYLON.Vector3.TransformCoordinates(offsetPosition, rotationMatrix);
weaponMesh.position.addInPlace(translation);
weaponMesh.rotationQuaternion.multiplyInPlace(offsetRotation.toQuaternion());
});
The above code snippet allows for arbitrary initial scaling of the troll and weapon