What is a quaternion, and do how they work? Also, what advantages do you gain using three points on a 2D plane? Finally, when is it considered good practice to use quaternions?
Answer
Mathematically, a quaternion is a complex number with 4 dimension. But in game development, Quaternions are often used to describe a rotation in 3d space by encoding:
- a rotation axis (in form of a 3-dimensional vector)
- how far to turn around that axis
Note that this information is encoded with sines and cosines inside the quaternion, so in general you shouldn't try to explicitly set or read the quaternion's internal components (x y z w) individually. It's easy to make a mistake that way and get a non-meaningful result. A quaternion math library will usually provide functions to operate on quaternions (eg. converting them to & from Euler angles or axis-angle), which ensures the math is correct and has the side benefit of making your code easier to read and understand.
An alternative way to describe rotations is by describing how far to turn around the 3 fixed axis' x, y, and z (aka Euler angles) which only requires 3 numbers instead of 4 and is usually more intuitive to use. However, euler-angles are subject to a problem called gimbal-lock: When you rotate 90° around one axis, the other two axis become equivalent. With quaternions, this problem does not occur.
Another way to express rotations in 3d space is with a 4x4 transformation matrix. But with a transformation matrix you can not just rotate, but also scale, translate and skew. When you want only rotation, a matrix would be overkill and a quaternion a much quicker and simpler solution.
This problem is only relevant in 3d space. In 2d space, you only have one rotation axis. Any rotation can be expressed with a single floating-point number or a single complex number, so you don't have this problem. While you can theoretically express a rotation on a 2d plane with a quaternion where the axis points into (or out of) the plane, it is usually overkill.
No comments:
Post a Comment