4
votes

My object3D has an up vector (0,1,0). The object3D moves its origin around yawing, pitching and rolling.

My Question: How do I find the vector which shows the direction of the object3D's up vector in world coordinates?

Example of coding context:-

var v1 = new THREE.Vector3();

v1.add( givenObject3D.up ); // now v1 = (0,1,0)

givenObject3D.updateMatrixWorld();

FunctionXXX (v1, givenObject3D.matrixWorld  ); 

//... now v1 is a vector in world coordinates 
//... v1 points in the same direction as the givenObject3D's up vector.

v1.normalize();
1

1 Answers

6
votes

You want to get the world orientation of an object's up vector. To do so, apply the same rotation to the up vector that is applied to the object.

If the object is a child of the scene directly, then you can do this:

var v1 = new THREE.Vector3(); // create once and reuse it
...
v1.copy( object.up ).applyQuaternion( object.quaternion );

If the object has a rotated parent, then you need to use this pattern:

var v1 = new THREE.Vector3(); // create once and reuse it
var worldQuaternion = new THREE.Quaternion(); // create once and reuse it
...
object.getWorldQuaternion( worldQuaternion );
v1.copy( object.up ).applyQuaternion( worldQuaternion );

three.js r.74