3
\$\begingroup\$

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?

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 14, 2012 at 22:25
  • \$\begingroup\$ @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 \$\endgroup\$
    – ChocoMan
    Commented Sep 14, 2012 at 23:14
  • \$\begingroup\$ 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). \$\endgroup\$ Commented Sep 14, 2012 at 23:35
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Sep 14, 2012 at 23:39

2 Answers 2

3
\$\begingroup\$

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".

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – ChocoMan
    Commented Sep 14, 2012 at 23:18
3
\$\begingroup\$
//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?

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .