Low-Level Quaternion Rotation

If you want to write code yourself that performs rotations, you can use the Quaternion structure with the rotation formulas I've shown you. For example, suppose you want to rotate the variable point of type Point3D by angle degrees around the axis vector. You first need to get a rotation quaternion and its conjugate:

Quaternion qRotate = new Quaternion(axis, angle);
Quaternion qConjugate = qRotate;
qConjugate.Conjugate();

Now convert the Point3D object to a pure quaternion:

Quaternion qPoint = new Quaternion(point.X, point.Y, point.Z, 0);

Multiply that point by the rotation quaternion on the left and its conjugate on the right:

Quaternion qRotatedPoint = qRotate * qPoint * qConjugate;

And convert back to a Point3D:

pointRotated ...

Get 3D Programming for Windows® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.