Updates:
- Added quaternion class for camera rotation
After making my camera rotation code through Euler Angles, I realized that real modeling engines and game engines donât use Euler Angles fully as they have many disadvantages such as, computational heavy, gimbal, lock, etc. With research, I came across Quaternions, which are basically a 4-dimensional numerical system* (imp), with a real part (a) and an imaginary part (bi + cj + dk).
To explain briefly, just like how imaginary number i extends the numberline and shows rotation of a vector around an axis* (imp), quaternions entend that concept to bring that in 3d rotations (3-imaginary components which all represent orientation/position.) Just like rotation for the complex plane where we use exponents for faster and efficient math, (e^ix = cosx + isinx), we can extend this by taking the 3 components to represent 3D space, thus a quaternion rotation can be represent as: cos(x/2) + sin(x/2)(xi + yj + zk), where (xi + yj + zk) is one of the vector which we want to rotate around and cos(x/2) the amount we want to rotate. Multiplying two unit quaternions (like basis vectors) results in rotation from A to B, or B to A depending on order of multiplication. This can be calculated through cross product which gives us the perpendicular vector to the plane formed by A and B which is the same as axis, and the rotation amount can calculated as dot product (geometrically) between A and B. After knowing the point in quaternion number basis, we have to convert it into our 3D world, which is done through a change of basis, which I am still wrapping my head around.
P.S. the x/2 found in the rotation quaternion is there because due to the rules that define quaternions, after multiplying you find out that the end result is actually 2 times the rotation you wanted, thus the divide by 2.