sábado, 22 de agosto de 2015

World and local coordinates

The 3D space is represented by three axes: X, Y and Z. There are different ways to organize these axes. The Unreal Engine uses this type:

This can be seen in the "Translation Widget" of the actors:

Click to enlarge

Any position in the 3D space can be represented using a set of values X, Y and Z, indicating the position on each axis. These values are stored in the "Location" variable of an actor and are known as world coordinates:

An Unreal Unit equals one centimeter. To use the value of "Location" in a Blueprint, the following functions can be used:

The "GetActorLocation" function returns the current position of the Actor. The "SetActorLocation" function set a new position for the Actor. The "AddActorWorldOffset" uses the values of the "Delta Location" parameter to modify the current position of the actor. The example below adds 1000cm on the X axis of the current position of the Actor and does not modify the values of the axes Y and Z.

There is another concept known as local coordinates. To understand this concept we will use a Blueprint which has two "Static Mesh" components.

Create a new blueprint of the "Actor" type. On the "Components" tab add a new component of the "StaticMesh" type and put the name "Base". Drag the name "Base" and drop upon the component called "DefaultSceneRoot":

We did this so that the position of "Base" StaticMesh represents the Blueprint position. This StaticMesh became the "Root" of the Blueprint.

For this component, select the Static Mesh "SM_AssetPlatform" and as Material use the "M_Tech_Hex_Tile" that are part of the "Starter Content":

Add another "StaticMesh" component, put the name "PowerPill" and make the following settings:

In the variable "Location" of the "PowerPill" put the value "70cm" in the Z axis:

This position of the "PowerPill" is relative to the "Root", which is the "Base" component, so when we move the "Base", the "PowerPill" will follow this movement as it always has to be at a distance from the "Base" of 70cm in the Z axis. This relative position is known as local coordinates.

You can see how became the Blueprint on the "Viewport" tab:

The position of the component "PowerPill" can be obtained in two ways. We can get its relative position which in this example would be ( X=0, Y=0, Z=70 ), or we can get its world position that will be the world position of the component "Root" plus the relative position of the component "PowerPill ". The image below shows these two options:

The position of the component can also be defined in two ways using the functions "SetRelativeLocation" and "SetWorldLocation". The "SetRelativeLocation"  function defines a new position of the component relative to the "Root" and the "SetWorldLocation" function receives as input parameter a world coordinate and defines the position of the component so that the sum of the position of the "Root" with the position of the component is equal to the world coordinate informed.

The "AddRelativeLocation" function can be used to modify the relative position of the component: