Quaternions and more

So. need to post something. So I decided to explain how I managed to "stabilize" the camera in my 3D scene.

The problem was pretty simple. I receive a stream of transformation matrices from Organic Motion stage. These matrices represent 3D positions of body parts. Unfortunately, there is an instability and all the bones move constantly even if a person tries to be calm and not to move (I even tried to hold my breath).

The solution that I chose is to take the previous transformation matrix and the current and interpolate them.

At the beginning I tried to implement interpolation of the three angles of a rotation (x, y, and z axis) manually. For example, the previous angle was 0.5 radian, the current angle is 0.7 radian. I take an everage value (0.5 + 0.7) / 2.0 == 0.6. So the actual rotation should be 0.6.

After I tried this method, I found out that the way an angle is represented creates problems to use my implementation of a interpolation. The angle is represented as a value from -2*PI to 2*PI and a rotation -2*PI equals to a rotation 2*PI. In this case I cannot easily interpolate two angles when there is a sign change. Lets take for example -3.1 and 3.1 angles in radians. These angles are actually almost the same for the camera, yet the everage value is 0 and that rotation is completely wrong.

To fix this problem I decided to work with cases when a the previous and current angles change the sign individually using a condition block. Then I found out that there is another point where an everaging algorithm does not return a correct value.

Then I recalled quaternions. They can help to implement rotations. I tried to understand how to interpolate a pair of quaternions and... That became too complecated for me.

And only at this moment I realized that JMonkeyEngine API and it's implementation of quaternions (and transformation matrices) has alreay implemented method "interpolate"!!.
So I just used it and fixed my problem.

I think it is a good idea to read a little bit about quaternions. It might help in the future if you are going to deal with 3D graphics.

The end.

Comments

Popular Posts