More advanced lookAt?

I want to animate one of my animation models to turn and look at another object, but I want to do it over a certain amount of time (e.g. - 1 second), instead of using the lookAt() function to do it immediately.

I found these two posts on the subject:

If it’s a reasonable idea, and please tell me if there is a better way, I’m going to use the quarternion rotateTowards() function to achieve this:

https://threejs.org/docs/index.html#api/en/math/Quaternion.rotateTowards

The piece I’m missing is how to find the quarternion that is the vector that the animation model should end up pointing towards when the rotation is done, so I can pass it to the rotateTowards() function. How can I calculate that quarternion?

There is an official example that explains the usage of Quaternion.rotateTowards():

https://threejs.org/examples/webgl_math_orientation_transform

2 Likes

@Mugen87 Thanks! I believe these two lines are the “magic” I was looking for?:

				rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
				targetQuaternion.setFromRotationMatrix( rotationMatrix );

So the rotationMatrix object allows me to execute a lookAt() function without changing anything in my world, and now that I have that value, I can use the rotationMatrix object to get the needed values as a quarternion?

I’ve never used the rotationMatrix object for so I want to make sure I understand the mechanics of this solution.

Yes. You have to make the bypass over a matrix since Quaternion does not have a lookAt() method.

1 Like