понедельник, 10 декабря 2012 г.

Rotations in 3D. Part 2 - Rotation in arbitrary point in space

Hi. Today I'll show you how to rotate 3D object about cardinal axis in arbitrary point in space.

From previous tutorial you know how to rotate about one of the main axes. Let's refresh:

                 

In this demo cube is in it's origin. Applying rotation matrix to it cause it to spin about y axis. In pseudocode this looks like this

obj * R; // multiply every vertex in cube by rotation matrix

Now let's move cube a little to the right. In pseudocode:

obj * T; // T - translation matrix

After applying same matrix R we'll get strange result:

                 

Cube rotates about origin, not about it's local axe. In order to rotate about local y we need several steps:
  1. Translate cube to origin.
  2. Rotate.
  3. Translate cube back.
In pseudocode:

//T - translation matrix of the cube
//M - final matrix to apply
M = inv(T) * M * T;
obj * M;

Here inv(T) - is inverse matrix of T - simply negate of current x, y, z of the cube. Recal that before applying this matrix cube have transformation T. Here's a demo:

                 

As you can see, all works fine.
Same steps required in order to rotate object about any chosen point in space:
  1. We need to ranslate that point to origin and at the same time translate our object by same amount.
  2. Rotate.
  3. Translate object back.
Final demo shows this in action:

                 

That's all for today.

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

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