Points and vectors

There is a structure in Unreal Engine named Vector, which has three variables of the float type: X, Y, and Z. These values can be used to represent a point (or location) in 3D space. They can also be used as a mathematical vector and represent movement.

Let's first look at an example of using Vector as a point. The following screenshot has two Actors. One Actor represents a character and the other represents a couch:

The next screenshot shows the character's Location. The Location variable of the Transform structure is of the Vector type, and one Unreal unit equals 1.0 cm by default:

We can represent the character's Location simply as (50.0, 0.0, 20.0). The couch's Location is (450.0, 0.0, 20.0), which can be seen in the next screenshot:

Now, let's see how to use a vector to represent movement. We are going to instruct our character on how to get to the couch. They need to know the direction and distance in which they must move. The next screenshot shows that we are instructing the character to move 400 cm on the x axis:

Both the direction and distance are represented by a single vector, using X, Y, and Z values. In the previous screenshot, the value of the vector that describes the movement is (400, 0, 0).

If we take the character location vector and add it to the vector that represents this movement, then the result is the couch location vector. To add two vectors, add each of their elements:

couch_location = character_location + vector_movement
couch_location = (50, 0, 20) + (400, 0, 0)
couch_location = (50 + 400, 0 + 0, 20 + 0)
couch_location = (450, 0, 20)

If we have a start point and a destination point, and we want to find out the movement vector, then we just need to get the destination point and subtract the start point.

For example, if we want to know the vector that leads from the start point of (25, 40, 55) to the destination point of (75, 95, 130), then we need to solve this expression:

vector_movement = destination_point - start_point
vector_movement = (75, 95, 130) - (25, 40, 55)
vector_movement = (75 - 25, 95 - 40, 130 - 55)
vector_movement = (50, 55, 75)

A vector has a magnitude (or length) and a direction and is usually represented by an arrow. Vectors are widely used in game programming; they can be used to indicate directions and to represent speed, acceleration, and a force acting on an object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset