Using Quaternions to rotate(3D)


In my project I have used a complimentary filter to generate yaw, roll, and pitch angles. I used the rotateX, rotateY, and rotateZ functions to rotate a box in 3d. I ran into problems like gimbal lock when using this method.

Now I’m using a quaternion based complimentary filter to generate quaternions that represent the orientation of the box. My question is, now that I have the quaternion representing my estimated orientation, how can I implement it to generate rotations? Are there any functions that I can use to do this?


You might use the Apache Math Commons library. It has the rotation class which internally uses a quaternion implementation but provides simple constructors to create Rotations that can then be applied to vectors. It uses its own immutable 3D vector class with the x,y,z attributes stored as doubles rather than floats. The PeasyCam library uses it and in Shapes3D I created a simple port using the PVector class.


Convert the quaternion to a rotation matrix (see and use it with applyMatrix().


Converting a quaternion to a rotation matrix would be computationally expensive. Are there any functions for quaternions?


For my ruby-processing projects, I have created arc ball functionality using quaternions, after code by Ariel Malka. As I understand it use of quaternions does not guard against gimbal lock (unless properly implemented), but I have never experienced with my Arcball implementation.


You may find a good introduction and same sample code for quaternions here


The PeasyCam processing library for Processing is implemented using quartonions, I believe. Consulting the code might be useful.

See class Rotation