segunda-feira, 5 de outubro de 2015

Vector Operations

There are a number of mathematical operations that can be done with vectors. Understanding these basic operations is fundamental for manipulating objects in 3D space.

  • Vector Addition:

The sum of two vectors is done by adding each of its elements.


V1 = ( 5, 0, 9 ) and V2 = ( 4, 0, 2 )
V1 + V2 = ( 5 + 4, 0 + 0, 9 + 2 )
V1 + V2 = ( 9, 0, 11 )

The image below shows the Blueprint operator of the sum of vectors:

  • Vector Subtraction: 

The subtraction between two vectors is done by subtracting each of its elements.


V1 = ( 12, 0, 14 ) and V2 = (4, 0, 8 )
V1 - V2 = ( 12 - 4, 0 - 0, 14 - 8 )
V1 - V2 = ( 8, 0, 6 )

The image below shows the Blueprint operator of the subtraction of vectors:

  • Length of a Vector:

The length or magnitude of a vector can be calculated using the blueprint action below. This value can be used to represent the distance between two points.

  • Normalizing Vectors:

We use the vector normalization to find a unit vector. The unit vector has a length equal to 1. It is often used when we only need to indicate a direction. In some calculations we should only use a normalized vector.

  • Scalar Vector Multiplication:

Multiplication of a vector by a scalar value is done by multiplying each of its elements by the scalar value. This operation changes the length of the vector.

We'll see all these operations being used together in an example. In this example we'll move a Blueprint in the scenario toward a random point. When the Blueprint reach its destination, a new random destination is set.

We could use the movement components already defined by the Unreal Engine, but let's calculate the movement step by step to understand the different uses of vectors.

We will use the following variables in the movement calculation:

  • DeltaSeconds: This variable stores the value passed by the "Tick" event that represents the time that has passed since the last "Tick".
  • Speed: Stores the speed value of the Blueprint. The default value is 100 (cm/s).
  • vDestination: Vector that stores the position where the Blueprint has to move.
  • vDistance: Vector indicating the distance from the current position of the Blueprint to its destination.
  • vDirection: Unit vector that indicates the direction that the Blueprint should follow to reach its destination.
  • vVelocity: Vector representing the speed and direction of the Blueprint in cm/s.
  • vStep: Vector with the final result of the calculation that indicates the movement that the Blueprint should do at the current "Tick".

A macro named "SetRandomDestination" was created to set a random destination. In this example the value of the Z axis is fixed at 300 and the values of the X and Y axes can range from -1000 to 1000. The vector created is stored in the vector "vDestination".

Click to enlarge

In this example it is not being taken into account any type of collision. Then the test needs to be done in an area without obstacles.

The Macro "SetRandomDestination" must be called in the "BeginPlay" event to set the starting destination.

The image below shows the first part of the "Tick" event, where each of the vectors is calculated until find the value of "vStep". All this calculation could be unified in a single mathematical expression, but I kept separate each part of the calculation to facilitate the understanding.

Click to enlarge

The second part of the "Tick" event move the Blueprint using the Action "AddActorWorldOffset" and test if the Blueprint has arrived at its destination. This test is done by comparing if the current distance is smaller than the value of the variable "Speed". If true, a new destination is set.

Click to enlarge

In my example I used a flying table to represent my Blueprint: