4 ms·
A matrix-vector multiplication (or any other linear operation) can be understood as a change of basis, with the matrix rows or columns as the new basis vectors.
by tripletao 6y ago
A matrix-vector multiplication (or any other linear operation) can be understood as a change of basis, with the matrix rows or columns as the new basis vectors. A rotation[1] changes to a new set of basis vectors that are (a) all unit length and (b) all perpendicular to each other.
In the code, direction is assumed to be a unit vector, and that's your new z. x = up.cross(direction) is perpendicular to both up and direction, but may have arbitrary length since up and direction aren't generally perpendicular to each other; so x must also be normalized to length of one. y = z.cross(x) is perpendicular to both z and x, and doesn't need normalizing since they were already perpendicular (though extra normalizing doesn't hurt and sometimes--probably not here for most purposes--helps to clean up rounding error).
It's almost never a good idea to use Euler angles unless you're modeling a system where they have physical meaning (e.g., robot arm, gimbal, etc.). The math is almost always simpler when you work in terms of the rotation matrix directly (or quaternions, or axis-angle rotation, or pretty much anything else).
1. Or mirroring, which is the same as a rotation but with any two of the basis vectors swapped or any one negated.