Three.js: How to determine vector of Object3D (where it is facing)

Created on 30 Mar 2012  路  13Comments  路  Source: mrdoob/three.js

My goal is to create a ray with my object's position x,y.z as the origin and pointing ...well...out the 'front' of my object......
I know each Object3D has a "rotation vector" (x y and z are in radians I thnk) . How can I convert that to a 'directional' vector to use as the 'direction' in my ray? Or is there some other gem hidden in the Object3D that I can pull out that already has this vector?

Question

Most helpful comment

@MathiasPaumgarten

var vector = new THREE.Vector3(); // create once and reuse it!
...
camera.getWorldDirection( vector );

See this post.

All 13 comments

I think this is what you're after:

var matrix = new THREE.Matrix4();
matrix.extractRotation( mesh.matrix );

var direction = new THREE.Vector3( 0, 0, 1 );
matrix.multiplyVector3( direction );

wow..thanks for the quick response...

Stupid question 1:
What exactly is stored in the mesh(Object3d) 'matrix' attribute that the extractRotation function can pull the rotational matrix from it?

Stupid question 2:
My end result is now a Matrix4....but Ray is expecting a Vector3....am I missing something?

Stupid question 1:
What exactly is stored in the mesh(Object3d) 'matrix' attribute that the extractRotation function can pull the rotational matrix from it?

matrix contains position, rotation and scale.

Stupid question 2:
My end result is now a Matrix4....but Ray is expecting a Vector3....am I missing something?

Ray expects a origin vector and a direction vector. You can use mesh.position as the origin vector. Use the snippet from before for getting the direction vector.

duh....I didn't realize that the multipleVector3 function returned results...I assumed it modified the original matrix.
Thanks...

Indeed. It wasn't too clear. Perhaps this makes it more obvious:

var matrix = new THREE.Matrix4();
matrix.extractRotation( mesh.matrix );

var direction = new THREE.Vector3( 0, 0, 1 );
direction = matrix.multiplyVector3( direction );

no worries..thanks again for a FANTASTIC library. I haven't had this much fun coding in years....

:D

Related question... how can I set the direction an object is facing based on a Vector3? I am using cylinder primitives, making them into cones, translating them outward. I want them all to point to (0,0,0). Is there something like... geometry.applyMatrix( new THREE.Matrix4().makeRotationFromVector3(Vector3) ); ?

Also remember an example of this in the docs that seems to have disappeared. A sphere would drift through a field of cones.

As stated in the guidelines, help requests should be directed to stackoverflow. This board is for bugs and feature requests.

This is a super old and also closed issue, but as a relative 3D newcomer this was the only real answer to this same problem I found after quite a while of searching and trying. Maybe it would make sense to have a helper function on Object3D getNormals() that gives the orientation as a Vector3 as extracted in above example.

This is an old thread but I thought I'd still give a little input for everyone finding this now. A clean solution based on quaternions would be this:

var direction = new Vector3( 0, 0, -1 ).applyQuaternion( mesh.quaternion );

Comes in very handy if you want the direction the camera is looking for instance. 馃摲

@MathiasPaumgarten

var vector = new THREE.Vector3(); // create once and reuse it!
...
camera.getWorldDirection( vector );

See this post.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

akshaysrin picture akshaysrin  路  3Comments

filharvey picture filharvey  路  3Comments

boyravikumar picture boyravikumar  路  3Comments

ghost picture ghost  路  3Comments

konijn picture konijn  路  3Comments