пятница, 7 сентября 2012 г.

Rotations in 3D. Part 1 - Euler Angles.

Hello. Today I want to talk about rotations in 3D. I hope to cover several possibilities to rotate an object, so stay tuned. In first part I'll try to explain what the Euler Angles are.

Althout that article should be about Euler angles, I'll start with Matrix Form.

As you may be know, in order to rotate vector, we need to multiply it by some matrix called rotation matrix. In Stage3D API we can use Matrix3D class (I choose different approach - I use my own math classes. You can read more here - I don't need flash.geom.Matrix3D class anymore)  that already have all nessesary methods to tune various transformations. So, for example, you can tell your matrix to tune it's rotation like this:

var m:Matrix3D = new Matrix3D();
m.appendRotation(45, Vector3D.Y_AXIS);

This code just setups entries inside matrix m. And here're this entries for three cardinal axes:




Here sin and cos are just sine and cosine of some angle (in radians) and they of course can be different for each matrix. If we want to combine several rotations we just multiply matrices.

And here Euler Angles comes. These angles - is just values of rotation about some axes (althought any axis is proper, often cardinal axes XYZ is used). More of this, it's not nessesary to use XYZ axis - you can use XYX or ZXZ etc. Also there can be different order of rotations like XYZ and YXZ. More famous are so called combinations Roll, Pitch, Yaw or Heading, Pitch, Bank that uses X, Y and Z axis (but order can be different - best to think of it in object's space - like right, forward, up). So all you need to do - is to set angles in appropriate matrix and multiply all matrices together. And you'll get final rotation.

Here is an example:

                 

You can use sliders to rotate object about different axes.

Tha beauty of Euler Angles is in their simplicity. For example, it's easy to visualise. Also it's the most compact format - you need only 3 numbers to store orientation. It's easy to convert to Matrix Form and back.

But there're some problems with them. For example, take a look again to the previous example. Try to move bank slider. You see that object rotates along axis that passes through it's front face. Now set it to 0. Next, set pitch to 90. Next, move heading slider. Did you see that???!!! Object rotates again about front face (althought it not see in the screen)! This is so called Gimbal Lock and we loose one degree of freedom. Avoid such situation is posiible - just use angles in proper range - in my example simply don't use pitch more than 90. Also you can different approach - such as Quaternions (wait for later post).

Next problem arises when you want interpolate from one set of Euler Angles to another. Take a look:

                 

Here buttons set1 and set2 just sets diferent Euler Angles to object. Next., on animate button click I just interpolate linearly between angles of set1 and set2. You can see that animation is not what you want to see. Usually you want rotate object by smallest path.

That's all. If you have questions or notice a mistake you're welkome in comments!

Комментариев нет:

Отправить комментарий