# Rotate model using quaternion

Currently I have this to rotate my 3D model that rotates on it's local axis independent from the world's axis:

// Rotate model with Right Thumbstick
modelRotation -= pController.ThumbSticks.Right.X * mRotSpeed; // float value


What I'm trying to do is rotate the model using quaternion and not by a matrix.

I've searched for tutorials, but have found none that explains thoroughly on how to achieve this. Does anyone know how to I can use quaternions to rotate my model or a complete tutorial?

• There are tons of quaternion tutorials out there, so I'm not sure why you wouldn't be able to find something. Are there specific points that you need clarified? If so, a more focused question might be more useful. – Nathan Reed Sep 14 '12 at 22:25
• @NathanReed Im trying to rotate a model around it's Y-axis in quaternions and NOT matrix. Yes there are many tutorials out there, but they are either incomplete or too general. There aren't many well explained that are XNA-specific – ChocoMan Sep 14 '12 at 23:14
• Expecting an XNA-specific one may be asking too much. Programmers are expected to be able to translate general algorithms and formulas into their own particular language/library/engine/whatever. It's a pretty basic coding skill. Do you need help understanding how the XNA quaternion class works? That's fine, but then you should read the docs on that class (and ask specific questions if there are things you don't understand). – Nathan Reed Sep 14 '12 at 23:35
• @ChocoMan You use Matrix.CreateFromQuaternion to create a matrix from the quaternion to apply as a transformation. And your question is worded very poorly making it difficult to understand id you don't know how to use the quaternion or apply it as a transformation so stop being a dick to people trying to help you. – ClassicThunder Sep 14 '12 at 23:39

A quaternion has a vector part and a scalar part. To represent a rotation, a quaternion has to be of unit length. A quaternion that rotates by angle alpha around an axis represented by a normalized vector v is calculated like this:

q = [v*sin(alpha/2), cos(alpha/2)]


The rotation follows the right hand rule.

Now to apply this quaternion to a vector or a point you take your x, y and z and write it as a quaternion. Given a vector v, you can write it in quaternion form as such:

qv = [vx, vy, vz, 0]


Now to transform this vector by a quaternion you premultiply it by the rotation quaternion and postmultiply it by the inverse of the rotation quaternion, as such:

v_rotated = q*qv*q(-1), the (-1) means the quaternion is inversed.

To get an inverse of a quaternion you have to calculate it's conjugate and divide it by the quaternion's length squared, but since our quaternion is of unit length, this just means calculating the conjugate. You calculate the conjugate by negating the vector part of the quaternion.

You do this for every vertex like you would with a matrix.

This is from Jason Gregory's "Game Engine Architecture".

• You have to take into account that I'm using XNA as stated by in the tag and that its not as simple as writing raw formulas. – ChocoMan Sep 14 '12 at 23:18
//in the fields section:
Quaternion modelQuat;

//in the update method:
// Rotate model with Right Thumbstick
modelRotation -= pController.ThumbSticks.Right.X * mRotSpeed; // float value

modelQuat = Quaternion.CreateRotationY(modelRotation);

//Later, in the draw call when setting the effect:
effect.World = Matrix.CreateFromQuaternion(modelQuat);// (and if needed) * Matrix.createTranslation(someLocationVector);


Is this something like what your looking for?