quarta-feira, 9 de setembro de 2015

Points and Vectors

We saw in the article "Using structs in Blueprints" that a vector is represented in Unreal Engine as a structure containing the variables X, Y and Z that are of "float" type.

These values can be interpreted in various ways. One of its uses is to represent a point (or position) in 3D space. For example, every Actor has a variable called "Location" that is of "Vector" type. This variable keeps the world coordinate of the Actor.

To illustrate, we have a chair and a table in the image below, which are two Actors:

This image shows the position of the chair:

In a simplified way we can represent this position as follows: (100, 0, 20).

The table position is (400, 0, 20):

Now imagine that we need to guide a robot to go from the chair to the table. It needs two pieces of information, the direction in which should move and the distance. The required statement would be something like:

"Move 300 cm to the right."

Both the direction and distance can be gathered into a single vector that can be represented in the same way that a position is represented by using X, Y, and Z values.

Assuming in this example that the movement to the right happens along the X axis. The vector above would look like this: (300, 0, 0).

If we take the chair position and add to the vector that represents this movement, the result is the table position. To add a point with a vector, just add each of its elements:

table_position = chair_position + vector_movement
table_position = (100, 0, 20) + (300, 0, 0)
table_position = (100 + 300, 0 + 0, 20 + 0)
table_position = (400, 0, 20)

So if we have a origin point and a destination point and we want to find out the movement vector, just get the destination point and subtract the origin point.

For example, if we want to know the vector that leads from the origin point (50, 30, 45) to the destination point (120, 80, 110), just calculate:

vector_movement = destination_point - origin_point
vector_movement = (120, 80, 110) - (50, 30, 45)
vector_movement = (120 - 50, 80 - 30, 110 - 45) 
vector_movement = (70, 50, 65)

Now that we have seen an example, we will see a more formal definition of a vector.

"A vector is a geometric object that has a magnitude (or length) and a direction. A vector is usually represented by an arrow with a definite direction."

Vectors are widely used in game programming. They can be used to indicate directions and to represent speed, acceleration and force acting on an object.

In the next article we'll look at various operations that can be done with vectors.