Rotate around an arbitrary axis with quaternions - Meshola - web based mesh content creation ;

Meshola -- web based mesh content creation

Home Log In Facebook Contact Keep Me Posted Public Meshes Source Code Articles API & Applications

Rotate around an arbitrary axis with quaternions

If you're around graphics stuff for any length of time you will have learned how to use Matrices to perform rotations and translations. This is an invaluable tool in the graphics world, but it suffers from the limitation that it only works if the X,Y and Z axis are the normal global axis; i.e. X is (1,0,0), Y is (0,1,0) and Z is (0,0,1). That's great, but what if you want to rotate an object around the vector (1,1,1)? It can be done in steps, first you do the rotation around whatever local axis you want and then you transform the entire object so that it points to 1,1,1, of course paying attention to the order of rotation. Rotation about X, then Y, then Z produces different results than rotation around Y, then X, then Z.

If you're building a world of objects it is very common to have vector that represents a direction where something is pointing, and you want to rotate around that vector. Imagine a ship flying through space and you want that ship to rotate about the line that is the center of the ship because its got artificial gravity, but the ship is always changing direction. All you have is a vector that represents the direction.

Quaternions are a mathematical construct that excel at managing this kind of rotation. There is actually a lot that you can do with them, but I haven't needed them often enough to really dig into them. All the cool kids use quaternions, though.

As I struggled to sort of grok what they were all about, I found some sample code that pinpointed with precision one of their features that solves this precise problem: they let you rotate a point around an arbitrary axis. The axis needs to be a normalized vector, so you may need to do a translation on the end points of the line in space, but you can do that translation, perform the rotation, and then translate back.

The code I found was from a project on the '' web site called Quaternion Mathematics and 3D Library with C# and GDI+. Contained in this program is a Quaternion library that wraps the functionality for the axis rotation quite well. Here is a code snippet that demonstrates the call. In this sample code I'm rotating around the +Z axis so that I can verify the results, but any axis will work.

            Quaternion q = new Quaternion();
             * Indicate the axis you want to rotate around and the amount of rotation you want around that axis. 
             * In this case we want a positive 90 degree rotation around the Z axis. Visualize the 2D x,y plane,
             * we're doing a rotation around the invisible Z axis that goes out from the page.
            q.FromAxisAngle(new Vector3d(0, 0, 1), 90 * Math.PI / 180.0);

            Point3d node = new Point3d(20, 0, 0);  

            // the result is 0,20,0 (within tolerance), so 90 degrees is a counter clockwise rotation, or right-handed, rotation
            node = q.Rotate(node);