# Retrieving rotation after mesh.lookAt()

## Recommended Posts

Hey guys,

I wanted to use the lookAt function and then get the rotation of the mesh. But it seems the rotation doesn't actually change when using lookAt. Why is that? And is there a way to get the new rotation somehow?

http://www.babylonjs-playground.com/#1QM99D

##### Share on other sites

That's because this function switches the mesh into "quaternion mode"! So the rotation vector is ignored altogether, and instead, rotationQuaternion is used!

http://www.babylonjs-playground.com/#1QM99D#1

Welcome to the wonderful world of quaternions, where there are many weird numbers and none of them make any sense.

##### Share on other sites

Welcome to the wonderful world of quaternions, where there are many weird numbers and none of them make any sense.

A workaround in your case would be to use the RotationFromAxis() static method here :

- define the view direction axis from your mesh to its target

- define two other axis, say Y axis and dot product view axis ^ Y axis

this is your axis target system in which you want to align your mesh (so to rotate it)

RotationFromAxis(axis1, axis2, axis3) will give you the rotation angle triplet.

Then you know the rotation values in terms of euler angles and you can just apply them to your mesh so it will look at its target

##### Share on other sites

Yeah, the quaternion stuff is confusing indeed. But jeromes suggestion seems to work for me. Thanks!

http://www.babylonjs-playground.com/#1QM99D#2

Edit: Hmm, I spoke too soon. It seems there are points that don't work. If I try to make the mesh look at those points with jeromes method the cube disapears because the new rotation is NaN for x (for example I get something like that as the new rotation: t {x: NaN, y: -0.7783801156008564, z: -0.5008333412587954} ).

Click around on the plane and sometimes the cube disappears: http://www.babylonjs-playground.com/#1QM99D#3

And here an example with a fixed point where it causes the cube to disappear: http://www.babylonjs-playground.com/#1QM99D#5

So.... is that a bug or is there a problem with how I do it?

##### Share on other sites

well, it happens when your axis1 and axis2 are collinear,  I guess, or at least not orthogonal....

You must pass three orthogonal left-handed oriented axis to RotationFromAxis().

In my explanation, I was too fast. In your case, you shouldn't directly choose the Y axis.

Well, the mesh direction view (axis1) is mesh-pickedPoint : ok

You could then choose axis3 = axis1 ^ Y  , axis3 is now orthogonal to axis1

and then axis2 = axis1 ^ axis3, axis2 is orthogonal to axis1 and to axis3

Just check they are well left handed oriented (else just change the cross product factors order)

##### Share on other sites

Hmm.. sorry, math class is too long ago. I was not sure what you meant, so I just assume with Y you mean the vector (0, 1, 0) and with ^ the cross product? I played around and tried to do what you told me (according to my interpretation) and it sees to work like that:

Click around version (couldn't reproduce the issue with this version): http://www.babylonjs-playground.com/#1QM99D#6

Version with fixed point that didn't work before: http://www.babylonjs-playground.com/#1QM99D#7

Sooo.. it's looking good but I am totally unsure if I actually did what you tried to tell me or if I am just making stuff up here

If I did it right: thank you! (if I missed the point, please show me in a playground, it's easier to understand there )

##### Share on other sites

yep Y is 0,1,0 and ^ is cross product

your PG seems to be right now

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.