1

I want to rotate a bone so it is aligned with a Vector3 (directionToTarget), I have:

const directionToTarget = new THREE.Vector3(random, random, random); //pseudocode randoms
directionToTarget.normalize();

var Hand2worldQ = new THREE.Quaternion();
this._anchor['LeftHandIndex1'].getWorldQuaternion(Hand2worldQ); // gets Lefthand bone quaternion

this._mesh.skeleton.bones[ 0 ].quaternion.set( SomeFunctionThatMakesVecintoQuarternion(directionToTarget ); 
// this._mesh.skeleton.bones inherits Hand2worldQ/LeftHand rotation

SomeFunctionThatMakesVec3intoQuarternion(directionToTarget ) is what i need

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
cubesareneat
  • 302
  • 2
  • 14

1 Answers1

2

Object3D starts by looking down the 0, 0, 1 axis. You can use the Object3D.lookAt() method to make this object point towards your target vector. Then you can extract that rotation and use it for whatever else you need:

// Initialize an abstract object looking towards 0, 0, 1
const object3D = new THREE.Object3D();

// Rotate it so it looks towards the target xyz coordinates
object3D.lookAt(randomX, randomY, randomZ);

// This will now have the final rotation
const myQuaternion = object3D.quaternion;
console.log(myQuaternion);
M -
  • 26,908
  • 11
  • 49
  • 81