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
Now let's move cube a little to the right. In pseudocode:
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:
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:
- Translate cube to origin.
- Rotate.
- Translate cube back.
//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:
- We need to ranslate that point to origin and at the same time translate our object by same amount.
- Rotate.
- Translate object back.
That's all for today.
Комментариев нет:
Отправить комментарий