Rotating an object with a quaternion

Started by
0 comments, last by Nacho 22 years, 2 months ago
Hi there! I´m reading Beggining Direct3D Game Programming and I´ve got a little problem trying to rotate an object using quaternions. The author explained how to rotate a camera using quaternions. Since I´m still in High School I´m not skilled at 3D math at all (altough I understand vectors for solving electrical problems), so try to keep simple, please =). Ok, as far as I understood, to rotate a camera using quaternions you have to make a matrix from the last change/s in its position (translation matrix), concatenate it with their "old" coordinates and build the new translation matrix. Then you have to build a quaternion using the rotations acquired in the last frame and convert it into a matrix (or tell Direct3D to do it! =) Then concatenate it with the position, invert it and set it as the view matrix and that´s is correct, I guess, because it´s the camera. This is my idea on how to rotate an object on using a quaternion (Note: I don´t want my object to translate, just to rotate on its own axis) Acquire user input and convert it using the elapsed time and the smooth factor in the angular velocity for the rotation. Set a translation matrix out of the object´s coordinates. Fill the quaternion with the rotation for each axis, convert it into a matrix, concatenate it with the translation matrix, invert it and make the final matrix the object´s own matrix. In the render() method that matrix will be set as D3DTS_WORLD before rendering the object. Well ok, now this is my problem: My object appears on the screen but it rotates in a circular manner. I can post the code if you want. So please, somebody help me!
Advertisement
Well... I am not quite sure whether I understood you correctly, and I am not that experienced in quaternion math either, but...
1) If you want to rotate an object with a quaternion, you do not need to do all that stuff (what you indeed did was rotating the camera around the object). You just convert the quaternion to a matrix (without any translation matrix and without inverting anything) and set it as D3DTS_WORLD. You also need to set the D3DTS_VIEW correctly aswell, e.g. with D3DXLookAtLh (I think it is called that way).
Inverting a matrix means doing the inverse of what is "said" by the matrix. If you want to create a camera matrix, you create a matrix that does the required transformation on the the camera. But then you do not set this as the camera matrix, because you do not want to transform the camera, but you want to do the inverse transformation on the world. If you rotate your camera to the left, it is the same as rotating the whole world to the right... And that´s why you invert the matrix.
2) You wrote "Fill the quaternion with the rotation for each axis". I am not sure what you mean with this, but if you want to create a quaternion for a rotation, you normally calculate the rotation angle a and the axis v (length 1) and then
q = (cos(a/2), v1*sin(a/2), v2*sin(a/2), v3*sin(a/2))
You can also create random rotations by creating a 4d-Vector with length 1. And you can also multiply serveral quaternions to get a new rotation quaternion.

This topic is closed to new replies.

Advertisement