Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to determine vector of Object3D (where it is facing) #1606

Closed
casperjeff opened this issue Mar 30, 2012 · 13 comments
Closed

How to determine vector of Object3D (where it is facing) #1606

casperjeff opened this issue Mar 30, 2012 · 13 comments
Labels

Comments

@casperjeff
Copy link

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?

@mrdoob
Copy link
Owner

mrdoob commented Mar 30, 2012

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 );

@casperjeff
Copy link
Author

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?

@mrdoob
Copy link
Owner

mrdoob commented Mar 30, 2012

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.

@casperjeff
Copy link
Author

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

@mrdoob
Copy link
Owner

mrdoob commented Mar 30, 2012

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 );

@casperjeff
Copy link
Author

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

@mrdoob
Copy link
Owner

mrdoob commented Mar 30, 2012

:D

@timstutts
Copy link
Contributor

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) ); ?

@timstutts
Copy link
Contributor

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

@mrdoob
Copy link
Owner

mrdoob commented Jul 25, 2014

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

@kontur
Copy link

kontur commented Oct 12, 2014

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.

@roboshoes
Copy link

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. 📷

@WestLangley
Copy link
Collaborator

@MathiasPaumgarten

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

See this post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants