Converting between quaternions and matrices
Since both matrices and quaternions can be used to encode rotation data, it will be useful to be able to convert between them. To make converting between the two easier, you have to start thinking about rotation in terms of basis vectors, which are the vectors that represent the x, y, and z axes.
The upper 3 x 3 sub-matrix of a 4 x 4 matrix contains three basis vectors. The first column is the right
vector, the second is the up
vector, and the third is the forward
vector. Using only the forward
and up
vectors, the lookRotation
function can be used to convert a matrix into a quaternion.
To convert a quaternion into a matrix, simply multiply the world basis vectors, which are the x, y, and z axes of the world, by the quaternion. Store the resulting vectors in the appropriate components of the matrix:
- Implement the
quatToMat4
function inquat.cpp
. Don't forget to add the function declaration toquat.h
:mat4 quatToMat4(const...