Handmade Hero»Forums»Code
1 posts
Dot product and cross product as quaternions
You were saying cross product isn't as natural as the dot product in your last video.

However, if you use quaternions (invented by Hamilton), then both dot and cross product are on equal footing.

Multiplying two quaternions performs both dot and cross product at the same time.

A quaternion is represented by h = A + Bi + Cj + Dk

Example, 2 vectors in 3D space :

h1 = 5i + 6j + 7k

h2 = 8i + 9j + 10k

then the product h1 * h2 is given by

h1 * h2 = -164 - 3i + 6j - 3k

-164 is the dot product (a scalar)

(-3,6,-3) is the cross product (a 3-vector)

Quaternions are an extension of complex numbers

1-D Real number line ---> 2-D Complex plane (loss of ordering) ---> 4-D Quaternions (loss of commutativity under multiplication)
504 posts
Dot product and cross product as quaternions
A little underappreciated fact is that you can build a quaternion from the dot and cross product of 2 vectors directly:

The dot product of 2 vectors is the cosine of the angle between them and the cross product is the axis of the rotation with length equal to the sine of the angle between them.

ergo quat(dot(v1, v2), cross(v1, v2)) is the quaternion of the double angle You can get the proper quaternion by nlerping it with the unit quaternion.

Or in other words normalized(quat(1+dot(v1, v2), cross(v1, v2))) is the proper quaternion of the rotation of v1 to v2. Assuming v1 and v2 are unit length.

You can often delay the normalization step even, and let most operations just accumulate the length disparity and divide the squared length after converting to a matrix or applying it to a point. The only place you can't (to my knowledge) is when interpolating.
1 posts
Dot product and cross product as quaternions
Isnt -164 the negative of the dot product?