In general, game engines deal a lot with geometry. Everything like point, vector, line or 3D model can be represented as a set of Coordinates. Each Coordinate must be related to some Coordinate Grid.
Unity supports two types of Coordinate Grids (also called Spaces):
- World – single for the whole scene with the fixed origin bound to the scene center.
- Local – one per each object within the scene but with the origin bound to the object pivot.
Very often it is required to transform a coordinate from one space to another. To do this operation Unity provides a set of methods:
The first 3 methods do a transformation from the local space to the world space and the last 3 do the opposite operation. The reason why there is not only one method but a set is following: different types of transformation can be dependent on different factors like rotation, position or scale of the coordinate’s owner (the owner of the local space). The documentation for each method is quite short but the transformations behind are not so trivial.
The goal of this set of articles is a detailed explanation with illustrations of each transformation. This should give a clear understanding of each method and allow to do a wise choice during development.
For better comprehension, the explanation of all three transformation types is divided into parts: one for each type. Current post is about TransformDirection method 1 .
The understanding of ‘Direction’ definition will help to understand the related transformation.
In Unity, the direction is mostly represented by one coordinate, thus the starting point is always in the center of the coordinate grid. But since there can be two centers: world and local, it is a purpose of transformation functions to find a point that will represent the same direction but within another space. This sentence briefly explains the logic of the transformation behind. Below there is a detailed and illustrated explanation of the transformation step-by-step.
Transformation in details
First, let’s start with a local space. For example, there is an object (that represents an origin of the space) and a point (0, 0, 1) located in the same space.
Now this object together with the local point (A) can be placed somewhere in the world space. There, this point A has a coordinate (2, 3, 1). But since the related object was only moved from the world origin it is very easy to find a point A’ in world space that will represent the same direction. To do this the object’s origin (or the object itself) has to be aligned with world origin (simply moved to the world space center) and this will give a coordinate (0, 0, 1).
As long as the object with the related local point is only moved in the world space the direction coordinate will be the same for both spaces. Here is one additional requirement that must be satisfied to make the transformation so easy: the rotation of both spaces must be equal.
Let’s try to rotate the local space (or simply the object itself) by 90 degrees along the X-axis:
Currently, the local Z-axis is looking at the viewer but after the rotation it will look down.
In the world space this rotation will look like this:
Now the rotation is done and the situation is following:
- X-axis was untouched since the rotation was done along it;
- Y-axis is now looking at the viewer, but not at the top as it was before;
- Z-axis is now looking at the bottom;
- The coordinate of the Point A was unchanged since it was bound to the axis.
So world rotation does not affect local points. But in the world space the coordinate was changed, and now it is (2, 2, 0). To find the direction in the world space the same operation must be performed: the object must be moved to the world space origin. And as it is visible on the picture, the direction is different from the previous example, it is (0, -1, 0).
How to align the local space with the world space?
This operation* is done in a few steps:
- The local space origin is moved to the world space origin;
- Each point from the local space is moved in the same direction by the same distance as the local origin.
* This operation must be done in world coordinates.
From the last example there are following inputs:
- Object coordinate (local space origin): (2, 3, 0);
- World space origin: (0, 0, 0);
- Point A coordinate: (2, 2, 0).
To move the object from source coordinate (2, 3, 0) to the target coordinate (0, 0, 0) its coordinate has to be substructed as follows:
(2, 3, 0) – = (0, 0, 0)
= (2, 3, 0).
The substraction here is done per coordinate component: )
And now the A point can be moved as it is described in step 2 (in the same direction by the same distance so using ):
(2, 2, 0) – = (2, 2, 0) – (2, 3, 0) = (0, -1, 0)
Consequently the formula behind TransformDirection is following
7,087 total views, 50 views today