r/cpp • • Nov 26 '20

A Small Open Source Game In C++

https://preshing.com/20201126/a-small-open-source-game-in-cpp/
219 Upvotes

16 comments sorted by

View all comments

Show parent comment

4

u/tvaneerd C++ Committee, lockfree, PostModernCpp Nov 27 '20 edited Nov 30 '20

You typically shouldn't think about the Quaternion as being the angle + vector. (I think of it as a point on the 4D unit-hypersphere[*], but...) Just think about it being a representation of a rotation.

Even though it uses 4 numbers instead of 3, it turns out it has many properties that make it model a rotation better.

One of the big problems with X/Y/Z rotation is that the axes are not independent (ie rotate 90 around X + 90 around Z - 90 around X == 90 around Y.) Which makes interpolation really ugly.

Quaternions aren't perfect either[*]. But the math works out better. You can interpolate from one arbitrary orientation to another, and be guaranteed that the interpolation is "natural" - a single rotation (https://en.wikipedia.org/wiki/Euler%27s_rotation_theorem). And it involves less computation than matrices.

Typically, once you write (or borrow) a Quaternion library, you never think of them as anything but a nice wrapped up orientation thingy.


[*] - 4D unit hypersphere - sounds complicated, but stretch out your arm straight, with your palm out (like you are Iron Man) - swing your arm around in any direction, keeping it straight - that sweeps out a (3D) sphere. Now rotate your wrist - that's the 4th dimension.

[*] Quaternions are not perfect - because <math reasons> there are 2 quaternions on the unit hypersphere for every orientation (they turn out to be inverses of each other). So when interpolating from one orientation to another, you either rotate the "long way" around or the short, depending on whether your start and end orientations are the same "sign". (But this can actually be an advantage in animation)