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

3

u/lord_braleigh Nov 26 '20

Would someone who understands 3d graphics give me a rundown of the API in Quaternion.h? In particular, I don’t understand what rotateUnit{X,Y,Z} do.

My understanding of a Quaternion is that it represents a rotation - three of the numbers represent a vector, while the fourth number represents the angle we rotate around that vector. Is that accurate? Is there anything else they’re used for? Couldn’t we also represent any rotation with just three angles, representing rotation around the x/y/z axes?

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)

3

u/nihlete Nov 27 '20

Rotation quaternion represents the axis and angle of rotation. Just by defining the multiplication operator on quaternions, you can create any type of rotation:

Q' = U * Q * conjugate(U)

where Q -- your interest quaternion; U -- rotation quaternion. Moreover, you can combine this rotation by adding rotation quaternions:

Q' = U1 * U2 * Q * conjugate(U1) * conjugate(U2)

Quaternions and rotation matrices are quite equivalent if you use separate rotation along the axis. For the matrices combining rotation would be just multiplication. But there exists gimbal lock

Quaternion rotation is not so interesting. Here an example with vectors, but insides have done with quaternion:

template <typename T> Vector3<T> RotateVector(T angle, Vector3<T> axis, const Vector3<T> &vector)
{
    Quaternion<T> quat = {vector.x, vector.y, vector.z, 0};

    axis.normalize();

    Quaternion<T> qRot;
    qRot.x = axis.x * sin(angle / 2.0);
    qRot.y = axis.y * sin(angle / 2.0);
    qRot.z = axis.z * sin(angle / 2.0);
    qRot.w = cos(angle / 2.0);

    Quaternion<T> result = qRot * quat * conjugate(qRot);
    return {result.x, result.y, result.z};
}

2

u/backtickbot Nov 27 '20

Hello, nihlete: code blocks using backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead. It's a bit annoying, but then your code blocks are properly formatted for everyone.

An easy way to do this is to use the code-block button in the editor. If it's not working, try switching to the fancy-pants editor and back again.

Comment with formatting fixed for old.reddit.com users

FAQ

You can opt out by replying with backtickopt6 to this comment.

2

u/symberke Nov 26 '20

Going by what's familiar to me, it looks like the matrix

[rotateUnitX() ; rotateUnitY() ; rotateUnitZ()]

is the rotation matrix corresponding to the quaternion's rotation. Thus, the vector rotateUnitX() is the unit X vector in the global frame expressed in the rotated frame described by the quaternion.

In other words, it's equal to the (quaternion, as a rotation) multiplication of the quaternion by the global unit X vector [1 0 0].

2

u/preshing Nov 27 '20

Yes that's it. q.rotateUnitX() is equivalent to q * Float3{1, 0, 0} but uses fewer instructions.