Look rotation
Given a direction and a reference for which way is up, it's possible to create a quaternion that looks in that direction with the correct orientation. This function will be called lookRotation
—not lookAt
, to avoid any confusion with the matrix lookAt
function.
To implement the lookRotation
function, find a quaternion that rotates to the desired direction. To do this, create a quaternion between the world forward
vector (0, 0, 1) and the desired direction
. This quaternion will rotate to the right
target, but with no regard for what direction up
might be.
To correct the up
direction of this quaternion, you first have to find a vector that is perpendicular to the current forward direction and the desired up
direction. This can be done by taking the cross product of the two vectors.
The result of this cross product will be used to construct three orthogonal vectors—the forward vector, this new vector, and a vector that points up. The one you just...